This file is indexed.

/usr/share/pyshared/gluon/restricted.py is in python-gluon 2.12.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
| This file is part of the web2py Web Framework
| Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
| License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)

Restricted environment to execute application's code
-----------------------------------------------------
"""

import sys
try:
    import cPickle as pickle
except:
    import pickle
import traceback
import types
import os
import logging

from gluon.storage import Storage
from gluon.http import HTTP
from gluon.html import BEAUTIFY, XML
from gluon.settings import global_settings

logger = logging.getLogger("web2py")

__all__ = ['RestrictedError', 'restricted', 'TicketStorage', 'compile2']


class TicketStorage(Storage):

    """
    Defines the ticket object and the default values of its members (None)
    """

    def __init__(
        self,
        db=None,
        tablename='web2py_ticket'
    ):
        Storage.__init__(self)
        self.db = db
        self.tablename = tablename

    def store(self, request, ticket_id, ticket_data):
        """
        Stores the ticket. It will figure out if this must be on disk or in db
        """
        if self.db:
            self._store_in_db(request, ticket_id, ticket_data)
        else:
            self._store_on_disk(request, ticket_id, ticket_data)

    def _store_in_db(self, request, ticket_id, ticket_data):
        self.db._adapter.reconnect()
        try:
            table = self._get_table(self.db, self.tablename, request.application)
            table.insert(ticket_id=ticket_id,
                         ticket_data=pickle.dumps(ticket_data, pickle.HIGHEST_PROTOCOL),
                         created_datetime=request.now)
            self.db.commit()
            message = 'In FILE: %(layer)s\n\n%(traceback)s\n'
        except Exception:
            self.db.rollback()
            message =' Unable to store in FILE: %(layer)s\n\n%(traceback)s\n'
        self.db.close()
        logger.error(message % ticket_data)

    def _store_on_disk(self, request, ticket_id, ticket_data):
        ef = self._error_file(request, ticket_id, 'wb')
        try:
            pickle.dump(ticket_data, ef)
        finally:
            ef.close()

    def _error_file(self, request, ticket_id, mode, app=None):
        root = request.folder
        if app:
            root = os.path.join(os.path.join(root, '..'), app)
        errors_folder = os.path.abspath(
            os.path.join(root, 'errors'))  # .replace('\\', '/')
        return open(os.path.join(errors_folder, ticket_id), mode)

    def _get_table(self, db, tablename, app):
        tablename = tablename + '_' + app
        table = db.get(tablename)
        if not table:
            table = db.define_table(
                tablename,
                db.Field('ticket_id', length=100),
                db.Field('ticket_data', 'text'),
                db.Field('created_datetime', 'datetime'))
        return table

    def load(
        self,
        request,
        app,
        ticket_id,
    ):
        if not self.db:
            try:
                ef = self._error_file(request, ticket_id, 'rb', app)
            except IOError:
                return {}
            try:
                return pickle.load(ef)
            finally:
                ef.close()
        else:
            table = self._get_table(self.db, self.tablename, app)
            rows = self.db(table.ticket_id == ticket_id).select()
            return pickle.loads(rows[0].ticket_data) if rows else {}


class RestrictedError(Exception):
    """
    Class used to wrap an exception that occurs in the restricted environment
    below. The traceback is used to log the exception and generate a ticket.
    """

    def __init__(
        self,
        layer='',
        code='',
        output='',
        environment=None,
    ):
        """
        Layer here is some description of where in the system the exception
        occurred.
        """
        if environment is None:
            environment = {}
        self.layer = layer
        self.code = code
        self.output = output
        self.environment = environment
        if layer:
            try:
                self.traceback = traceback.format_exc()
            except:
                self.traceback = 'no traceback because template parsing error'
            try:
                self.snapshot = snapshot(context=10, code=code,
                                         environment=self.environment)
            except:
                self.snapshot = {}
        else:
            self.traceback = '(no error)'
            self.snapshot = {}

    def log(self, request):
        """
        Logs the exception.
        """
        try:
            d = {
                'layer': str(self.layer),
                'code': str(self.code),
                'output': str(self.output),
                'traceback': str(self.traceback),
                'snapshot': self.snapshot,
            }
            ticket_storage = TicketStorage(db=request.tickets_db)
            ticket_storage.store(request, request.uuid.split('/', 1)[1], d)
            cmd_opts = global_settings.cmd_options
            if cmd_opts and cmd_opts.print_errors:
                logger.error(self.traceback)
            return request.uuid
        except:
            logger.error(self.traceback)
            return None


    def load(self, request, app, ticket_id):
        """
        Loads a logged exception.
        """
        ticket_storage = TicketStorage(db=request.tickets_db)
        d = ticket_storage.load(request, app, ticket_id)

        self.layer = d.get('layer')
        self.code = d.get('code')
        self.output = d.get('output')
        self.traceback = d.get('traceback')
        self.snapshot = d.get('snapshot')

    def __str__(self):
        # safely show an useful message to the user
        try:
            output = self.output
            if isinstance(output, unicode):
                output = output.encode("utf8")
            elif not isinstance(output, str):
                output = str(output)
        except:
            output = ""
        return output


def compile2(code, layer):
    """
    The ``+'\\n'`` is necessary else compile fails when code ends in a comment.
    """

    return compile(code.rstrip().replace('\r\n', '\n') + '\n', layer, 'exec')


def restricted(code, environment=None, layer='Unknown'):
    """
    Runs code in environment and returns the output. If an exception occurs
    in code it raises a RestrictedError containing the traceback. Layer is
    passed to RestrictedError to identify where the error occurred.
    """
    if environment is None:
        environment = {}
    environment['__file__'] = layer
    environment['__name__'] = '__restricted__'
    try:
        if isinstance(code, types.CodeType):
            ccode = code
        else:
            ccode = compile2(code, layer)
        exec ccode in environment
    except HTTP:
        raise
    except RestrictedError:
        # do not encapsulate (obfuscate) the original RestrictedError
        raise
    except Exception, error:
        # extract the exception type and value (used as output message)
        etype, evalue, tb = sys.exc_info()
        # XXX Show exception in Wing IDE if running in debugger
        if __debug__ and 'WINGDB_ACTIVE' in os.environ:
            sys.excepthook(etype, evalue, tb)
        output = "%s %s" % (etype, evalue)
        raise RestrictedError(layer, code, output, environment)


def snapshot(info=None, context=5, code=None, environment=None):
    """Return a dict describing a given traceback (based on cgitb.text)."""
    import time
    import linecache
    import inspect
    import pydoc
    import cgitb

    # if no exception info given, get current:
    etype, evalue, etb = info or sys.exc_info()

    if isinstance(etype, types.ClassType):
        etype = etype.__name__

    # create a snapshot dict with some basic information
    s = {}
    s['pyver'] = 'Python ' + sys.version.split()[0] + ': ' + sys.executable + ' (prefix: %s)' % sys.prefix
    s['date'] = time.ctime(time.time())

    # start to process frames
    records = inspect.getinnerframes(etb, context)
    s['frames'] = []
    for frame, file, lnum, func, lines, index in records:
        file = file and os.path.abspath(file) or '?'
        args, varargs, varkw, locals = inspect.getargvalues(frame)
        call = ''
        if func != '?':
            call = inspect.formatargvalues(args, varargs, varkw, locals,
                                           formatvalue=lambda value: '=' + pydoc.text.repr(value))

        # basic frame information
        f = {'file': file, 'func': func, 'call': call, 'lines': {},
             'lnum': lnum}

        highlight = {}

        def reader(lnum=[lnum]):
            highlight[lnum[0]] = 1
            try:
                return linecache.getline(file, lnum[0])
            finally:
                lnum[0] += 1
        vars = cgitb.scanvars(reader, frame, locals)

        # if it is a view, replace with generated code
        if file.endswith('html'):
            lmin = lnum > context and (lnum - context) or 0
            lmax = lnum + context
            lines = code.split("\n")[lmin:lmax]
            index = min(context, lnum) - 1

        if index is not None:
            i = lnum - index
            for line in lines:
                f['lines'][i] = line.rstrip()
                i += 1

        # dump local variables (referenced in current line only)
        f['dump'] = {}
        for name, where, value in vars:
            if name in f['dump']:
                continue
            if value is not cgitb.__UNDEF__:
                if where == 'global':
                    name = 'global ' + name
                elif where != 'local':
                    name = where + name.split('.')[-1]
                f['dump'][name] = pydoc.text.repr(value)
            else:
                f['dump'][name] = 'undefined'

        s['frames'].append(f)

    # add exception type, value and attributes
    s['etype'] = str(etype)
    s['evalue'] = str(evalue)
    s['exception'] = {}
    if isinstance(evalue, BaseException):
        for name in dir(evalue):
            # prevent py26 DeprecatedWarning:
            if name != 'message' or sys.version_info < (2.6):
                value = pydoc.text.repr(getattr(evalue, name))
                s['exception'][name] = value

    # add all local values (of last frame) to the snapshot
    s['locals'] = {}
    for name, value in locals.items():
        s['locals'][name] = pydoc.text.repr(value)

    # add web2py environment variables
    for k, v in environment.items():
        if k in ('request', 'response', 'session'):
            s[k] = XML(str(BEAUTIFY(v)))

    return s