~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: mattgiuca
  • Date: 2008-07-21 04:21:18 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:917
interpret.py: Removed the code which removes HTTP_COOKIE from the CGI
    environment. Student code can now access cookies.
Note: This was previously a security risk because malicious code could
steal IVLE cookies. Now that we have separate domain space for other users,
the worst you can do is:
    a) Steal your own IVLE cookie.
    b) Steal other user's non-IVLE cookies (ie. other public cookies).

This makes all student code vulnerable to cookie theft, but that is simply a
disclaimer (if you use cookies, your apps are vulnerable). It is not a
security risk to IVLE itself.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
import sys
33
33
import os
34
34
import os.path
 
35
import urllib
 
36
 
35
37
import conf
36
38
import conf.apps
 
39
import conf.conf
37
40
import apps
38
41
 
39
42
from request import Request
42
45
import login
43
46
from common import (util, forumutil)
44
47
import traceback
45
 
import cStringIO
 
48
import plugins.console
 
49
import logging
 
50
import socket
46
51
 
47
52
def handler(req):
48
53
    """Handles a request which may be to anywhere in the site except media.
79
84
    just used to catch exceptions.
80
85
    Takes both an IVLE request and an Apache req.
81
86
    """
 
87
    # Hack? Try and get the user login early just in case we throw an error
 
88
    # (most likely 404) to stop us seeing not logged in even when we are.
 
89
    req.user = login.get_user_details(req)
 
90
 
82
91
    # Check req.app to see if it is valid. 404 if not.
83
92
    if req.app is not None and req.app not in conf.apps.app_url:
84
93
        # Maybe it is a special app!
144
153
 
145
154
    # When done, write out the HTML footer if the app has requested it
146
155
    if req.write_html_head_foot:
 
156
        # Show the console if required
 
157
        if logged_in and app.useconsole:
 
158
            plugins.console.present(req, windowpane=True)
147
159
        html.write_html_foot(req)
148
160
 
149
161
    # Note: Apache will not write custom HTML error messages here.
171
183
    the IVLE request is created.
172
184
    """
173
185
    req.content_type = "text/html"
 
186
    logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
 
187
    logfail = False
174
188
    # For some reason, some versions of mod_python have "_server" instead of
175
189
    # "main_server". So we check for both.
176
190
    try:
186
200
    except AttributeError:
187
201
        httpcode = None
188
202
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
 
203
    try:
 
204
        login = req.user.login
 
205
    except AttributeError:
 
206
        login = None
 
207
 
 
208
    # Log File
 
209
    try:
 
210
        logging.basicConfig(level=logging.INFO,
 
211
            format='%(asctime)s %(levelname)s: ' +
 
212
                '(HTTP: ' + str(req.status) +
 
213
                ', Ref: ' + str(login) + '@' +
 
214
                str(socket.gethostname()) + str(req.uri) +
 
215
                ') %(message)s',
 
216
            filename=logfile,
 
217
            filemode='a')
 
218
    except IOError:
 
219
        logfail = True
 
220
    logging.debug('Logging Unhandled Exception')
 
221
 
189
222
    # We handle 3 types of error.
190
223
    # IVLEErrors with 4xx response codes (client error).
191
224
    # IVLEErrors with 5xx response codes (handled server error).
199
232
        # IVLEErrors with 4xx response codes are client errors.
200
233
        # Therefore, these have a "nice" response (we even coat it in the IVLE
201
234
        # HTML wrappers).
 
235
        
202
236
        req.write_html_head_foot = True
203
237
        req.write('<div id="ivle_padding">\n')
204
238
        try:
217
251
            req.write("<p>%s</p>\n" % cgi.escape(msg))
218
252
        else:
219
253
            req.write("<p>An unknown error occured.</p>\n")
 
254
        
 
255
        # Logging
 
256
        logging.info(str(msg))
 
257
        
220
258
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
 
259
        if logfail:
 
260
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
261
                %cgi.escape(logfile))
221
262
        req.write('</div>\n')
222
263
    else:
223
264
        # A "bad" error message. We shouldn't get here unless IVLE
227
268
        # If this is a non-4xx IVLEError, get the message and httpcode and
228
269
        # make the error message a bit nicer (but still include the
229
270
        # traceback).
230
 
        try:
231
 
            codename, msg = req.get_http_codename(httpcode)
232
 
        except AttributeError:
233
 
            codename, msg = None, None
234
 
        # Override the default message with the supplied one,
235
 
        # if available.
236
 
        if hasattr(exc_value, 'message') and exc_value.message is not None:
237
 
            msg = exc_value.message
238
 
            # Prepend the exception type
239
 
            if exc_type != util.IVLEError:
240
 
                msg = exc_type.__name__ + ": " + msg
 
271
        # We also need to special-case IVLEJailError, as we can get another
 
272
        # almost-exception out of it.
 
273
 
 
274
        codename, msg = None, None
 
275
 
 
276
        if exc_type is util.IVLEJailError:
 
277
            msg = exc_value.type_str + ": " + exc_value.message
 
278
            tb = 'Exception information extracted from IVLEJailError:\n'
 
279
            tb += urllib.unquote(exc_value.info)
 
280
        else:
 
281
            try:
 
282
                codename, msg = req.get_http_codename(httpcode)
 
283
            except AttributeError:
 
284
                pass
 
285
            # Override the default message with the supplied one,
 
286
            # if available.
 
287
            if hasattr(exc_value, 'message') and exc_value.message is not None:
 
288
                msg = exc_value.message
 
289
                # Prepend the exception type
 
290
                if exc_type != util.IVLEError:
 
291
                    msg = exc_type.__name__ + ": " + msg
 
292
 
 
293
            tb = ''.join(traceback.format_exception(exc_type, exc_value,
 
294
                                                    exc_traceback))
 
295
 
 
296
        # Logging
 
297
        logging.error('%s\n%s'%(str(msg), tb))
241
298
 
242
299
        req.write("""<html>
243
300
<head><title>IVLE Internal Server Error</title></head>
259
316
administrator). Include the following information:</p>
260
317
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
261
318
 
262
 
        tb_print = cStringIO.StringIO()
263
 
        traceback.print_exception(exc_type, exc_value, exc_traceback,
264
 
            file=tb_print)
265
 
        req.write("<pre>\n")
266
 
        req.write(cgi.escape(tb_print.getvalue()))
267
 
        req.write("</pre>\n</body>\n")
 
319
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
320
        if logfail:
 
321
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
322
                %cgi.escape(logfile))
 
323
        req.write("</body>")