Attachment #630203: Part 6 - Remove mContextPrincipal usage from within nsScriptSecurityManager. v1 for bug #754202

View | Details | Raw Unified | Return to bug 754202
Collapse All | Expand All

(-)a/caps/include/nsScriptSecurityManager.h (-12 lines)
Line     Link Here 
 Lines 548-587   private: Link Here 
548
    nsresult
548
    nsresult
549
    InitPrincipals(PRUint32 prefCount, const char** prefNames);
549
    InitPrincipals(PRUint32 prefCount, const char** prefNames);
550
550
551
#ifdef DEBUG_CAPS_HACKER
551
#ifdef DEBUG_CAPS_HACKER
552
    void
552
    void
553
    PrintPolicyDB();
553
    PrintPolicyDB();
554
#endif
554
#endif
555
555
556
    struct ContextPrincipal {
557
        ContextPrincipal(ContextPrincipal *next, JSContext *cx,
558
                         JSStackFrame *fp, nsIPrincipal *principal)
559
            : mNext(next), mCx(cx), mFp(fp), mPrincipal(principal) {}
560
561
        ContextPrincipal *mNext;
562
        JSContext *mCx;
563
        JSStackFrame *mFp;
564
        nsCOMPtr<nsIPrincipal> mPrincipal;
565
    };
566
567
    // JS strings we need to clean up on shutdown
556
    // JS strings we need to clean up on shutdown
568
    static jsid sEnabledID;
557
    static jsid sEnabledID;
569
558
570
    inline void
559
    inline void
571
    ScriptSecurityPrefChanged();
560
    ScriptSecurityPrefChanged();
572
561
573
    nsObjectHashtable* mOriginToPolicyMap;
562
    nsObjectHashtable* mOriginToPolicyMap;
574
    DomainPolicy* mDefaultPolicy;
563
    DomainPolicy* mDefaultPolicy;
575
    nsObjectHashtable* mCapabilities;
564
    nsObjectHashtable* mCapabilities;
576
565
577
    nsCOMPtr<nsIPrincipal> mSystemPrincipal;
566
    nsCOMPtr<nsIPrincipal> mSystemPrincipal;
578
    nsCOMPtr<nsIPrincipal> mSystemCertificate;
567
    nsCOMPtr<nsIPrincipal> mSystemCertificate;
579
    ContextPrincipal *mContextPrincipals;
580
    nsInterfaceHashtable<PrincipalKey, nsIPrincipal> mPrincipals;
568
    nsInterfaceHashtable<PrincipalKey, nsIPrincipal> mPrincipals;
581
    bool mPrefInitialized;
569
    bool mPrefInitialized;
582
    bool mIsJavaScriptEnabled;
570
    bool mIsJavaScriptEnabled;
583
    bool mIsWritingPrefs;
571
    bool mIsWritingPrefs;
584
    bool mPolicyPrefsChanged;
572
    bool mPolicyPrefsChanged;
585
573
586
    static bool sStrictFileOriginPolicy;
574
    static bool sStrictFileOriginPolicy;
587
575
(-)a/caps/src/nsScriptSecurityManager.cpp (-55 / +4 lines)
Line     Link Here 
 Lines 2217-2278   nsScriptSecurityManager::GetPrincipalAndFrame(JSContext *cx, Link Here 
2217
{
2217
{
2218
    NS_PRECONDITION(rv, "Null out param");
2218
    NS_PRECONDITION(rv, "Null out param");
2219
    //-- If there's no principal on the stack, look at the global object
2219
    //-- If there's no principal on the stack, look at the global object
2220
    //   and return the innermost frame for annotations.
2220
    //   and return the innermost frame for annotations.
2221
    *rv = NS_OK;
2221
    *rv = NS_OK;
2222
2222
2223
    if (cx)
2223
    if (cx)
2224
    {
2224
    {
2225
        JSStackFrame *target = nsnull;
2226
        nsIPrincipal *targetPrincipal = nsnull;
2227
        for (ContextPrincipal *cp = mContextPrincipals; cp; cp = cp->mNext)
2228
        {
2229
            if (cp->mCx == cx)
2230
            {
2231
                target = cp->mFp;
2232
                targetPrincipal = cp->mPrincipal;
2233
                break;
2234
            }
2235
        }
2236
2237
        // Get principals from innermost JavaScript frame.
2225
        // Get principals from innermost JavaScript frame.
2238
        JSStackFrame *fp = nsnull; // tell JS_FrameIterator to start at innermost
2226
        JSStackFrame *fp = nsnull; // tell JS_FrameIterator to start at innermost
2239
        for (fp = JS_FrameIterator(cx, &fp); fp; fp = JS_FrameIterator(cx, &fp))
2227
        for (fp = JS_FrameIterator(cx, &fp); fp; fp = JS_FrameIterator(cx, &fp))
2240
        {
2228
        {
2241
            if (fp == target)
2242
                break;
2243
            nsIPrincipal* result = GetFramePrincipal(cx, fp, rv);
2229
            nsIPrincipal* result = GetFramePrincipal(cx, fp, rv);
2244
            if (result)
2230
            if (result)
2245
            {
2231
            {
2246
                NS_ASSERTION(NS_SUCCEEDED(*rv), "Weird return");
2232
                NS_ASSERTION(NS_SUCCEEDED(*rv), "Weird return");
2247
                *frameResult = fp;
2233
                *frameResult = fp;
2248
                return result;
2234
                return result;
2249
            }
2235
            }
2250
        }
2236
        }
2251
2237
2252
        // If targetPrincipal is non-null, then it means that someone wants to
2253
        // clamp the principals on this context to this principal. Note that
2254
        // fp might not equal target here (fp might be null) because someone
2255
        // could have set aside the frame chain in the meantime.
2256
        if (targetPrincipal)
2257
        {
2258
            if (fp && fp == target)
2259
            {
2260
                *frameResult = fp;
2261
            }
2262
            else
2263
            {
2264
                JSStackFrame *inner = nsnull;
2265
                *frameResult = JS_FrameIterator(cx, &inner);
2266
            }
2267
2268
            return targetPrincipal;
2269
        }
2270
2271
        nsIScriptContextPrincipal* scp =
2238
        nsIScriptContextPrincipal* scp =
2272
            GetScriptContextPrincipalFromJSContext(cx);
2239
            GetScriptContextPrincipalFromJSContext(cx);
2273
        if (scp)
2240
        if (scp)
2274
        {
2241
        {
2275
            nsIScriptObjectPrincipal* globalData = scp->GetObjectPrincipal();
2242
            nsIScriptObjectPrincipal* globalData = scp->GetObjectPrincipal();
2276
            if (!globalData)
2243
            if (!globalData)
2277
            {
2244
            {
2278
                *rv = NS_ERROR_FAILURE;
2245
                *rv = NS_ERROR_FAILURE;
 Lines 2448-2484   NS_IMETHODIMP Link Here 
2448
nsScriptSecurityManager::IsCapabilityEnabled(const char *capability,
2415
nsScriptSecurityManager::IsCapabilityEnabled(const char *capability,
2449
                                             bool *result)
2416
                                             bool *result)
2450
{
2417
{
2451
    nsresult rv;
2418
    nsresult rv;
2452
    JSStackFrame *fp = nsnull;
2419
    JSStackFrame *fp = nsnull;
2453
    JSContext *cx = GetCurrentJSContext();
2420
    JSContext *cx = GetCurrentJSContext();
2454
    fp = cx ? JS_FrameIterator(cx, &fp) : nsnull;
2421
    fp = cx ? JS_FrameIterator(cx, &fp) : nsnull;
2455
2422
2456
    JSStackFrame *target = nsnull;
2457
    nsIPrincipal *targetPrincipal = nsnull;
2458
    for (ContextPrincipal *cp = mContextPrincipals; cp; cp = cp->mNext)
2459
    {
2460
        if (cp->mCx == cx)
2461
        {
2462
            target = cp->mFp;
2463
            targetPrincipal = cp->mPrincipal;
2464
            break;
2465
        }
2466
    }
2467
2468
    if (!fp)
2423
    if (!fp)
2469
    {
2424
    {
2470
        // No script code on stack. If we had a principal pushed for this
2425
        // No script code on stack. We don't have enough information and have
2471
        // context and fp is null, then we use that principal. Otherwise, we
2426
        // to allow execution.
2472
        // don't have enough information and have to allow execution.
2427
        *result = true;
2473
2474
        *result = (targetPrincipal && !target)
2475
                  ? (targetPrincipal == mSystemPrincipal)
2476
                  : true;
2477
2428
2478
        return NS_OK;
2429
        return NS_OK;
2479
    }
2430
    }
2480
2431
2481
    *result = false;
2432
    *result = false;
2482
    nsIPrincipal* previousPrincipal = nsnull;
2433
    nsIPrincipal* previousPrincipal = nsnull;
2483
    do
2434
    do
2484
    {
2435
    {
 Lines 2512-2528   nsScriptSecurityManager::IsCapabilityEnabled(const char *capability, Link Here 
2512
        if (NS_FAILED(rv)) return rv;
2463
        if (NS_FAILED(rv)) return rv;
2513
        if (*result)
2464
        if (*result)
2514
            return NS_OK;
2465
            return NS_OK;
2515
2466
2516
        // Capabilities do not extend to calls into C/C++ and then back into
2467
        // Capabilities do not extend to calls into C/C++ and then back into
2517
        // the JS engine via JS_EvaluateScript or similar APIs.
2468
        // the JS engine via JS_EvaluateScript or similar APIs.
2518
        if (JS_IsGlobalFrame(cx, fp))
2469
        if (JS_IsGlobalFrame(cx, fp))
2519
            break;
2470
            break;
2520
    } while (fp != target && (fp = JS_FrameIterator(cx, &fp)) != nsnull);
2471
    } while ((fp = JS_FrameIterator(cx, &fp)) != nsnull);
2521
2472
2522
    if (!previousPrincipal)
2473
    if (!previousPrincipal)
2523
    {
2474
    {
2524
        // No principals on the stack, all native code.  Allow
2475
        // No principals on the stack, all native code.  Allow
2525
        // execution if the subject principal is the system principal.
2476
        // execution if the subject principal is the system principal.
2526
2477
2527
        return SubjectPrincipalIsSystem(result);
2478
        return SubjectPrincipalIsSystem(result);
2528
    }
2479
    }
 Lines 2996-3012   nsScriptSecurityManager::Observe(nsISupports* aObject, const char* aTopic, Link Here 
2996
2947
2997
/////////////////////////////////////////////
2948
/////////////////////////////////////////////
2998
// Constructor, Destructor, Initialization //
2949
// Constructor, Destructor, Initialization //
2999
/////////////////////////////////////////////
2950
/////////////////////////////////////////////
3000
nsScriptSecurityManager::nsScriptSecurityManager(void)
2951
nsScriptSecurityManager::nsScriptSecurityManager(void)
3001
    : mOriginToPolicyMap(nsnull),
2952
    : mOriginToPolicyMap(nsnull),
3002
      mDefaultPolicy(nsnull),
2953
      mDefaultPolicy(nsnull),
3003
      mCapabilities(nsnull),
2954
      mCapabilities(nsnull),
3004
      mContextPrincipals(nsnull),
3005
      mPrefInitialized(false),
2955
      mPrefInitialized(false),
3006
      mIsJavaScriptEnabled(false),
2956
      mIsJavaScriptEnabled(false),
3007
      mIsWritingPrefs(false),
2957
      mIsWritingPrefs(false),
3008
      mPolicyPrefsChanged(true)
2958
      mPolicyPrefsChanged(true)
3009
{
2959
{
3010
    MOZ_STATIC_ASSERT(sizeof(intptr_t) == sizeof(void*),
2960
    MOZ_STATIC_ASSERT(sizeof(intptr_t) == sizeof(void*),
3011
                      "intptr_t and void* have different lengths on this platform. "
2961
                      "intptr_t and void* have different lengths on this platform. "
3012
                      "This may cause a security failure with the SecurityLevel union.");
2962
                      "This may cause a security failure with the SecurityLevel union.");
 Lines 3076-3092   nsresult nsScriptSecurityManager::Init() Link Here 
3076
3026
3077
static nsScriptSecurityManager *gScriptSecMan = nsnull;
3027
static nsScriptSecurityManager *gScriptSecMan = nsnull;
3078
3028
3079
jsid nsScriptSecurityManager::sEnabledID   = JSID_VOID;
3029
jsid nsScriptSecurityManager::sEnabledID   = JSID_VOID;
3080
3030
3081
nsScriptSecurityManager::~nsScriptSecurityManager(void)
3031
nsScriptSecurityManager::~nsScriptSecurityManager(void)
3082
{
3032
{
3083
    Preferences::RemoveObservers(this, kObservedPrefs);
3033
    Preferences::RemoveObservers(this, kObservedPrefs);
3084
    NS_ASSERTION(!mContextPrincipals, "Leaking mContextPrincipals");
3085
    delete mOriginToPolicyMap;
3034
    delete mOriginToPolicyMap;
3086
    if(mDefaultPolicy)
3035
    if(mDefaultPolicy)
3087
        mDefaultPolicy->Drop();
3036
        mDefaultPolicy->Drop();
3088
    delete mCapabilities;
3037
    delete mCapabilities;
3089
    gScriptSecMan = nsnull;
3038
    gScriptSecMan = nsnull;
3090
}
3039
}
3091
3040
3092
void
3041
void

Return to bug 754202