Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "jsapi.h"
7 : #include "nsIXPConnect.h"
8 : #include "nsCOMPtr.h"
9 : #include "nsIServiceManager.h"
10 : #include "nsIComponentManager.h"
11 : #include "nsString.h"
12 : #include "nsIPrefService.h"
13 : #include "nspr.h"
14 : #include "mozilla/Attributes.h"
15 : #include "mozilla/Maybe.h"
16 : #include "nsContentUtils.h"
17 : #include "nsIScriptSecurityManager.h"
18 : #include "nsJSPrincipals.h"
19 : #include "nsIScriptError.h"
20 : #include "jswrapper.h"
21 :
22 : extern mozilla::LazyLogModule MCD;
23 : using mozilla::AutoSafeJSContext;
24 : using mozilla::dom::AutoJSAPI;
25 :
26 : //*****************************************************************************
27 :
28 3 : static JS::PersistentRooted<JSObject *> autoconfigSb;
29 :
30 0 : nsresult CentralizedAdminPrefManagerInit()
31 : {
32 : nsresult rv;
33 :
34 : // If the sandbox is already created, no need to create it again.
35 0 : if (autoconfigSb.initialized())
36 0 : return NS_OK;
37 :
38 : // Grab XPConnect.
39 0 : nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
40 0 : if (NS_FAILED(rv)) {
41 0 : return rv;
42 : }
43 :
44 : // Grab the system principal.
45 0 : nsCOMPtr<nsIPrincipal> principal;
46 0 : nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(principal));
47 :
48 :
49 : // Create a sandbox.
50 0 : AutoSafeJSContext cx;
51 0 : JS::Rooted<JSObject*> sandbox(cx);
52 0 : rv = xpc->CreateSandbox(cx, principal, sandbox.address());
53 0 : NS_ENSURE_SUCCESS(rv, rv);
54 :
55 : // Unwrap, store and root the sandbox.
56 0 : NS_ENSURE_STATE(sandbox);
57 0 : autoconfigSb.init(cx, js::UncheckedUnwrap(sandbox));
58 :
59 0 : return NS_OK;
60 : }
61 :
62 0 : nsresult CentralizedAdminPrefManagerFinish()
63 : {
64 0 : if (autoconfigSb.initialized()) {
65 0 : AutoSafeJSContext cx;
66 0 : autoconfigSb.reset();
67 0 : JS_MaybeGC(cx);
68 : }
69 0 : return NS_OK;
70 : }
71 :
72 0 : nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
73 : const char *filename, bool bGlobalContext,
74 : bool bCallbacks, bool skipFirstLine)
75 : {
76 0 : nsresult rv = NS_OK;
77 :
78 0 : if (skipFirstLine) {
79 : /* In order to protect the privacy of the JavaScript preferences file
80 : * from loading by the browser, we make the first line unparseable
81 : * by JavaScript. We must skip that line here before executing
82 : * the JavaScript code.
83 : */
84 0 : unsigned int i = 0;
85 0 : while (i < length) {
86 0 : char c = js_buffer[i++];
87 0 : if (c == '\r') {
88 0 : if (js_buffer[i] == '\n')
89 0 : i++;
90 0 : break;
91 : }
92 0 : if (c == '\n')
93 0 : break;
94 : }
95 :
96 0 : length -= i;
97 0 : js_buffer += i;
98 : }
99 :
100 : // Grab XPConnect.
101 0 : nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
102 0 : if (NS_FAILED(rv)) {
103 0 : return rv;
104 : }
105 :
106 0 : AutoJSAPI jsapi;
107 0 : if (!jsapi.Init(autoconfigSb)) {
108 0 : return NS_ERROR_UNEXPECTED;
109 : }
110 0 : JSContext* cx = jsapi.cx();
111 :
112 0 : nsAutoCString script(js_buffer, length);
113 0 : JS::RootedValue v(cx);
114 :
115 0 : nsString convertedScript;
116 0 : bool isUTF8 = IsUTF8(script);
117 0 : if (isUTF8) {
118 0 : convertedScript = NS_ConvertUTF8toUTF16(script);
119 : } else {
120 0 : nsContentUtils::ReportToConsoleNonLocalized(
121 0 : NS_LITERAL_STRING("Your AutoConfig file is ASCII. Please convert it to UTF-8."),
122 : nsIScriptError::warningFlag,
123 0 : NS_LITERAL_CSTRING("autoconfig"),
124 0 : nullptr);
125 : /* If the length is 0, the conversion failed. Fallback to ASCII */
126 0 : convertedScript = NS_ConvertASCIItoUTF16(script);
127 : }
128 0 : JS::Rooted<JS::Value> value(cx, JS::BooleanValue(isUTF8));
129 0 : if (!JS_DefineProperty(cx, autoconfigSb, "gIsUTF8", value, JSPROP_ENUMERATE)) {
130 0 : return NS_ERROR_UNEXPECTED;
131 : }
132 0 : rv = xpc->EvalInSandboxObject(convertedScript, filename, cx,
133 0 : autoconfigSb, &v);
134 0 : NS_ENSURE_SUCCESS(rv, rv);
135 :
136 0 : return NS_OK;
137 : }
138 :
|