~ubuntu-branches/ubuntu/feisty/firefox/feisty-security

« back to all changes in this revision

Viewing changes to js/src/xpconnect/loader/mozJSSubScriptLoader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack, Alexander Sack
  • Date: 2008-06-23 15:08:12 UTC
  • mfrom: (1.1.24 upstream)
  • Revision ID: james.westby@ubuntu.com-20080623150812-sxdwhn3dz9pmapvf
Tags: 2.0.0.15+0nobinonly-0ubuntu0.7.4
[ Alexander Sack ]
* New security/stability upstream release (v2.0.0.15)
  - see USN-619-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
#include "nsIInputStream.h"
53
53
#include "nsNetCID.h"
54
54
#include "nsDependentString.h"
 
55
#include "nsAutoPtr.h"
 
56
#include "nsNetUtil.h"
 
57
#include "nsIProtocolHandler.h"
 
58
#include "nsIFileURL.h"
55
59
 
56
60
#include "jsapi.h"
 
61
#include "jsdbgapi.h"
57
62
 
58
63
static NS_DEFINE_CID(kIOServiceCID,              NS_IOSERVICE_CID);
59
64
 
60
65
/* load() error msgs, XXX localize? */
61
66
#define LOAD_ERROR_NOSERVICE "Error creating IO Service."
62
 
#define LOAD_ERROR_NOCHANNEL "Error creating channel (invalid URL scheme?)"
 
67
#define LOAD_ERROR_NOURI "Error creating URI (invalid URL scheme?)"
 
68
#define LOAD_ERROR_NOSCHEME "Failed to get URI scheme.  This is bad."
 
69
#define LOAD_ERROR_URI_NOT_LOCAL "Trying to load a non-local URI."
63
70
#define LOAD_ERROR_NOSTREAM  "Error opening input stream (invalid filename?)"
64
71
#define LOAD_ERROR_NOCONTENT "ContentLength not available (not a local URL?)"
65
72
#define LOAD_ERROR_BADREAD   "File Read Error."
66
73
#define LOAD_ERROR_READUNDERFLOW "File Read Error (underflow.)"
 
74
#define LOAD_ERROR_NOPRINCIPALS "Failed to get principals."
 
75
#define LOAD_ERROR_NOSPEC "Failed to get URI spec.  This is bad."
67
76
 
68
77
// We just use the same reporter as the component loader
69
78
extern void JS_DLL_CALLBACK
135
144
        rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
136
145
        if (NS_FAILED(rv) || !mSystemPrincipal)
137
146
            return rv;
 
147
    }
138
148
 
139
 
    }
140
 
    
141
149
    char     *url;
142
150
    JSObject *target_obj = nsnull;
143
151
    ok = JS_ConvertArguments (cx, argc, argv, "s / o", &url, &target_obj);
147
155
        /* let the exception raised by JS_ConvertArguments show through */
148
156
        return NS_OK;
149
157
    }
150
 
    
 
158
 
151
159
    if (!target_obj)
152
160
    {
153
161
        /* if the user didn't provide an object to eval onto, find the global
206
214
     * js exceptions */
207
215
    PRInt32   len = -1;
208
216
    PRUint32  readcount;
209
 
    char     *buf = nsnull;
 
217
    nsAutoArrayPtr<char> buf;
210
218
    
211
219
    JSString        *errmsg;
212
220
    JSErrorReporter  er;
214
222
    
215
223
    nsCOMPtr<nsIChannel>     chan;
216
224
    nsCOMPtr<nsIInputStream> instream;
 
225
    nsCOMPtr<nsIURI> uri;
 
226
    nsCAutoString uriStr;
 
227
    nsCAutoString scheme;
 
228
 
 
229
    JSStackFrame* frame = nsnull;
 
230
    JSScript* script = nsnull;
 
231
 
 
232
    // Figure out who's calling us
 
233
    do
 
234
    {
 
235
        frame = JS_FrameIterator(cx, &frame);
 
236
 
 
237
        if (frame)
 
238
            script = JS_GetFrameScript(cx, frame);
 
239
    } while (frame && !script);
 
240
 
 
241
    if (!script)
 
242
    {
 
243
        // No script means we don't know who's calling, bail.
 
244
 
 
245
        return NS_ERROR_FAILURE;
 
246
    }
217
247
 
218
248
    nsCOMPtr<nsIIOService> serv = do_GetService(kIOServiceCID);
219
249
    if (!serv)
222
252
        goto return_exception;
223
253
    }
224
254
 
225
 
    rv = serv->NewChannel(nsDependentCString(url), nsnull, NS_STATIC_CAST(nsIURI *, nsnull),
226
 
                          getter_AddRefs(chan));
 
255
    // Make sure to explicitly create the URI, since we'll need the
 
256
    // canonicalized spec.
 
257
    rv = NS_NewURI(getter_AddRefs(uri), url, nsnull, serv);
 
258
    if (NS_FAILED(rv)) {
 
259
        errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOURI);
 
260
        goto return_exception;
 
261
    }
 
262
 
 
263
    rv = uri->GetSpec(uriStr);
 
264
    if (NS_FAILED(rv)) {
 
265
        errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOSPEC);
 
266
        goto return_exception;
 
267
    }    
 
268
 
 
269
    rv = uri->GetScheme(scheme);
227
270
    if (NS_FAILED(rv))
228
271
    {
229
 
        errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOCHANNEL);
 
272
        errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOSCHEME);
230
273
        goto return_exception;
231
274
    }
232
275
 
233
 
    rv = chan->Open (getter_AddRefs(instream));
 
276
    if (!scheme.EqualsLiteral("chrome"))
 
277
    {
 
278
        // This might be a URI to a local file, though!
 
279
        nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(uri);
 
280
        if (!fileURL)
 
281
        {
 
282
            errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_URI_NOT_LOCAL);
 
283
            goto return_exception;
 
284
        }
 
285
 
 
286
        // For file URIs prepend the filename with the filename of the
 
287
        // calling script, and " -> ". See bug 418356.
 
288
        nsCAutoString tmp(JS_GetScriptFilename(cx, script));
 
289
        tmp.AppendLiteral(" -> ");
 
290
        tmp.Append(uriStr);
 
291
 
 
292
        uriStr = tmp;
 
293
    }        
 
294
        
 
295
    rv = NS_OpenURI(getter_AddRefs(instream), uri, serv,
 
296
                    nsnull, nsnull, nsIRequest::LOAD_NORMAL,
 
297
                    getter_AddRefs(chan));
234
298
    if (NS_FAILED(rv))
235
299
    {
236
300
        errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOSTREAM);
266
330
     * destructor */
267
331
    rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals);
268
332
    if (NS_FAILED(rv) || !jsPrincipals) {
269
 
        delete[] buf;
270
 
        return rv;
 
333
        errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOPRINCIPALS);
 
334
        goto return_exception;
271
335
    }
272
336
 
273
337
    /* set our own error reporter so we can report any bad things as catchable
275
339
    er = JS_SetErrorReporter (cx, mozJSLoaderErrorReporter);
276
340
 
277
341
    ok = JS_EvaluateScriptForPrincipals (cx, target_obj, jsPrincipals,
278
 
                                         buf, len, url, 1, rval);        
 
342
                                         buf, len, uriStr.get(), 1, rval);        
279
343
    /* repent for our evil deeds */
280
344
    JS_SetErrorReporter (cx, er);
281
345
 
282
346
    cc->SetExceptionWasThrown (!ok);
283
347
    cc->SetReturnValueWasSet (ok);
284
348
 
285
 
    delete[] buf;
286
349
    JSPRINCIPALS_DROP(cx, jsPrincipals);
287
350
    return NS_OK;
288
351
 
289
352
 return_exception:
290
 
    if (buf)
291
 
        delete[] buf;
292
 
 
293
353
    JS_SetPendingException (cx, STRING_TO_JSVAL(errmsg));
294
354
    cc->SetExceptionWasThrown (JS_TRUE);
295
355
    return NS_OK;
296
 
 
297
356
}
298
357
 
299
358
#endif /* NO_SUBSCRIPT_LOADER */