Skip to content

Commit 56ed0c7

Browse files
committed
Improved cross-browser and cross-manifest support.
1 parent bcf200c commit 56ed0c7

File tree

6 files changed

+112
-53
lines changed

6 files changed

+112
-53
lines changed

build.sh

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ BUILD="$BASE/build"
1010
MANIFEST_IN="$SRC/manifest.json"
1111
MANIFEST_OUT="$BUILD/manifest.json"
1212

13+
if [ "$1" == "watch" ]; then
14+
while :; do
15+
$0 -u debug
16+
inotifywait -e 'create,modify,move,delete' -r "$SRC"
17+
done
18+
fi
19+
20+
UNPACKED=
21+
if [ "$1" == '-u' ]; then
22+
UNPACKED=1
23+
shift
24+
fi
25+
1326
strip_rc_ver() {
1427
MANIFEST="$1"
1528
if [[ "$2" == "rel" ]]; then
@@ -67,7 +80,7 @@ if [[ "$1" == "bump" ]]; then
6780
echo "Bumping to $VER"
6881
git add "$MANIFEST_IN"
6982
git commit -m "Version bump: $VER."
70-
if ! [[ $VER == *rc* ]]; then
83+
if ! ([[ $VER == *rc* ]] || [[ $VER =~ \.9[0-9][09]$ ]]); then
7184
# it's a stable release: let's lock nscl and tag
7285
git submodule update
7386
"$0" tag
@@ -94,22 +107,15 @@ CHROMIUM_BUILD_CMD="$BUILD_CMD"
94107
CHROMIUM_BUILD_OPTS="$BUILD_OPTS"
95108

96109
if [[ $VER == *rc* ]]; then
97-
sed -re 's/^(\s+)"strict_min_version":.*$/\1"update_url": "https:\/\/secure.informaction.com\/update\/?v='$VER'",\n\0/' \
98-
"$MANIFEST_IN" > "$MANIFEST_OUT"
99110
if [[ "$1" =~ ^sign(ed)?$ ]]; then
100111
BUILD_CMD="$BASE/../../we-sign"
101112
BUILD_OPTS=""
102113
fi
103114
else
104-
grep -v '"update_url":' "$MANIFEST_IN" > "$MANIFEST_OUT"
105115
if [[ "$1" == "sign" ]]; then
106116
echo >&2 "WARNING: won't auto-sign a release version, please manually upload to AMO."
107117
fi
108118
fi
109-
if ! grep '"id":' "$MANIFEST_OUT" >/dev/null; then
110-
echo >&2 "Cannot build manifest.json"
111-
exit 1
112-
fi
113119

114120
if [ "$1" != "debug" ]; then
115121
DBG=""
@@ -124,8 +130,10 @@ else
124130
DBG="-dbg"
125131
fi
126132

127-
echo "Creating $XPI.xpi..."
128-
mkdir -p "$XPI_DIR"
133+
if ! [ "$UNPACKED" ]; then
134+
echo "Creating $XPI.xpi..."
135+
mkdir -p "$XPI_DIR"
136+
fi
129137

130138
if which cygpath; then
131139
WEBEXT_IN="$(cygpath -w "$BUILD")"
@@ -137,55 +145,58 @@ fi
137145

138146
COMMON_BUILD_OPTS="--ignore-files='test/**' 'embargoed/**' content/experiments.js"
139147

148+
fix_manifest() {
149+
node manifest.js "$1" "$MANIFEST_IN" "$MANIFEST_OUT"
150+
}
151+
140152
build() {
141-
"$BUILD_CMD" $BUILD_OPTS --source-dir="$WEBEXT_IN" --artifacts-dir="$WEBEXT_OUT" $COMMON_BUILD_OPTS
153+
if [ "$1" ]; then
154+
UNPACKED_DIR="$BASE/$1"
155+
rm -rf "$UNPACKED_DIR"
156+
cp -rp "$WEBEXT_IN" "$UNPACKED_DIR" && echo >&2 "Copied $WEBEXT_IN to $UNPACKED_DIR"
157+
fi
158+
[ "$UNPACKED" ] || \
159+
( "$BUILD_CMD" $BUILD_OPTS --source-dir="$WEBEXT_IN" \
160+
--artifacts-dir="$WEBEXT_OUT" $COMMON_BUILD_OPTS | \
161+
grep 'ready: .*\.zip' | sed -re 's/.* ready: //' )
142162
}
143163

144-
build
164+
fix_manifest mv2firefox
165+
build firefox
145166

146167
SIGNED="$XPI_DIR/noscript_security_suite-$VER-an+fx.xpi"
147168
if [ -f "$SIGNED" ]; then
148169
mv "$SIGNED" "$XPI.xpi"
149170
elif [ -f "$XPI.zip" ]; then
150-
SIGNED=""
151171
if unzip -l "$XPI.xpi" | grep "META-INF/mozilla.rsa" >/dev/null 2>&1; then
152172
echo "A signed $XPI.xpi already exists, not overwriting."
153173
else
154174
[[ "$VER" == *rc* ]] && xpicmd="mv" || xpicmd="cp"
155175
$xpicmd "$XPI.zip" "$XPI$DBG.xpi"
156176
echo "Created $XPI$DBG.xpi"
157177
fi
158-
elif ! [ -f "$XPI.xpi" ]; then
178+
fi
179+
if [ -f "$XPI.xpi" ]; then
180+
ln -fs "$XPI.xpi" "$BASE/latest.xpi"
181+
elif ! [ "$UNPACKED" ]; then
159182
echo >&2 "ERROR: Could not create $XPI$DBG.xpi!"
160183
exit 3
161184
fi
162-
ln -fs "$XPI.xpi" "$BASE/latest.xpi"
163185

164186
# create Chromium pre-release
165187

166188
BUILD_CMD="$CHROMIUM_BUILD_CMD"
167189
BUILD_OPTS="$CHROMIUM_BUILD_OPTS"
168-
CHROMIUM_UNPACKED="$BASE/chromium"
169-
EDGE_UPDATE_URL="https://edge.microsoft.com/extensionwebstorebase/v1/crx"
170-
rm -rf "$CHROMIUM_UNPACKED"
171-
strip_rc_ver "$MANIFEST_OUT"
172-
173-
# manifest.json patching for Chromium:
174190

175-
GECKO_PERMS="dns|<all_urls>|webRequestBlocking"
176-
177-
node manifest.js $MANIFEST_OUT
178-
179-
CHROME_ZIP=$(build | grep 'ready: .*\.zip' | sed -re 's/.* ready: //')
180-
181-
if [ -f "$CHROME_ZIP" ]; then
182-
node manifest.js mv3 $MANIFEST_OUT && build
183-
mv "$CHROME_ZIP" "$XPI$DBG-chrome.zip"
184-
fi
191+
fix_manifest mv3edge
192+
ZIP=$(build edge)
193+
[ -f "$ZIP" ] && mv "$ZIP" "$XPI$DBG-edge.zip"
185194

186-
mv "$BUILD" "$CHROMIUM_UNPACKED"
195+
fix_manifest mv3chrome
196+
ZIP=$(build chromium)
197+
[ -f "$ZIP" ] && mv "$ZIP" "$XPI$DBG-chrome.zip"
187198

188-
if [ "$SIGNED" ]; then
199+
if [ "$SIGNED" ] && ! [ "$UNPACKED" ]; then
189200
"$0" tag quiet
190201
nscl
191202
../../we-publish "$XPI.xpi"

manifest.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,37 @@
2323
const fs = require('fs');
2424
const path = require('path');
2525
const args = process.argv.slice(2);
26-
const MANIFEST_VER = /^m?v?[2-3]$/i.test(args[0]) ? args.shift() : "mv3edge";
26+
const MANIFEST_VER = /^m?v?[2-3]/i.test(args[0]) ? args.shift() : "mv3edge";
2727
const MANIFEST_SRC = args[0] || "src/manifest.json";
2828
const MANIFEST_DEST = args[1] || args[0] || "build/manifest.json";
2929

30-
const EDGE_UPDATE_URL = "https://edge.microsoft.com/extensionwebstorebase/v1/crx";
3130

3231
console.log(`${MANIFEST_SRC} --[${MANIFEST_VER}]--> ${MANIFEST_DEST}`);
3332

3433
const srcContent = fs.readFileSync(MANIFEST_SRC, 'utf8');
3534
const json = JSON.parse(srcContent);
3635
const permissions = new Set(json.permissions);
3736

38-
if (MANIFEST_VER.includes(3)) {
39-
delete json.browser_specific_settings;
40-
const {scripts} = json.background;
37+
const extVer = json.version;
38+
const FIREFOX_UPDATE_URL = "https://secure.informaction.com/update/?v=" + extVer;
39+
const EDGE_UPDATE_URL = "https://edge.microsoft.com/extensionwebstorebase/v1/crx";
40+
41+
const isFirefox = MANIFEST_VER.includes("firefox");
42+
43+
if (isFirefox && /rc|\.9\d{2}$/.test(extVer)) {
44+
json.browser_specific_settings.update_url = FIREFOX_UPDATE_URL;
45+
}
4146

42-
if (scripts) {
47+
if (MANIFEST_VER.includes(3)) {
48+
// MV3
49+
json.manifest_version = 3;
50+
if (!isFirefox) {
51+
delete json.browser_specific_settings;
52+
const {scripts} = json.background;
4353
delete json.background.scripts;
4454
delete json.background.persistent;
4555
const requiredPath = path.join(path.dirname(MANIFEST_DEST), "REQUIRED.js");
46-
fs.writeFileSync(requiredPath,
56+
scripts && fs.writeFileSync(requiredPath,
4757
`include.REQUIRED = ${JSON.stringify(scripts, null, 2)};`)
4858
}
4959

@@ -53,17 +63,38 @@ if (MANIFEST_VER.includes(3)) {
5363
delete json.update_url;
5464
}
5565

56-
permissions.delete("<all_urls>");
57-
permissions.delete("webRequestBlocking");
66+
for (const p of [
67+
"<all_urls>",
68+
"webRequestBlocking",
69+
"webRequestBlocking",
70+
"webRequestFilterResponse",
71+
"webRequestFilterResponse.serviceWorkerScript",
72+
]) {
73+
permissions.delete (p);
74+
}
5875

5976
const excludedScriptsRx = /\bcontent\/(?:embeddingDocument|dirindex)\.js$/;
6077
for (const cs of json.content_scripts) {
6178
cs.js = cs.js.filter(src => !excludedScriptsRx.test(src));
6279
}
6380
delete json.browser_action;
6481
delete json.commands._execute_browser_action
82+
} else {
83+
// MV2
84+
json.manifest_version = 2;
85+
delete json.background.service_worker;
86+
delete json.web_accessible_resources;
87+
delete json.host_permissions;
88+
delete json.action;
89+
for (const p of [
90+
"debugger",
91+
]) {
92+
permissions.delete(p);
93+
}
6594
}
6695

96+
97+
6798
// remove developer-only stuff
6899
permissions.delete("declarativeNetRequestFeedback");
69100

src/bg/RequestGuard.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
* this program. If not, see <https://www.gnu.org/licenses/>.
1919
*/
2020

21+
"use strict";
2122
{
22-
'use strict';
2323
const VERSION_LABEL = `NoScript ${browser.runtime.getManifest().version}`;
2424
browser.action.setTitle({title: VERSION_LABEL});
2525
const CSP_MARKER = "report-to noscript-reports";
@@ -648,8 +648,9 @@
648648
const listeners = {
649649
onBeforeRequest(request) {
650650
try {
651-
if (browser.runtime?.onSyncMessage.isMessageRequest(request)) return ALLOW;
652-
651+
if (browser.runtime.onSyncMessage?.isMessageRequest(request)) {
652+
return ALLOW;
653+
}
653654
initPendingRequest(request);
654655

655656
let result = checkRequest(request);
@@ -819,6 +820,7 @@
819820

820821
async function injectPolicyScript(details) {
821822
await ns.initializing;
823+
if (ns.local.debug?.disablePolicyInjection) return ''; // DEV_ONLY
822824
const {url, tabId, frameId} = details;
823825
const domPolicy = await ns.computeChildPolicy({url}, {tab: {id: tabId}, frameId});
824826
domPolicy.navigationURL = url;

src/bg/main.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
// depends on /nscl/service/Scripting.js
2222

23+
"use strict";
2324
{
24-
'use strict';
2525
{
2626
for (let event of ["onInstalled", "onUpdateAvailable"]) {
2727
browser.runtime[event].addListener(async details => {
@@ -442,10 +442,15 @@
442442
debug("Collected seen", seen); // DEV_ONLY
443443
return seen;
444444
} catch (e) {
445-
await include("/nscl/common/restricted.js");
446-
if (!isRestrictedURL((await browser.tabs.get(tabId)).url)) {
447-
// probably a page where content scripts cannot run, let's open the options instead
448-
error(e, "Cannot collect noscript activity data");
445+
try {
446+
const {url} = (await browser.tabs.get(tabId)).url;
447+
await include("/nscl/common/restricted.js");
448+
if (!isRestrictedURL(url)) {
449+
// probably a page where content scripts cannot run, let's open the options instead
450+
error(e, `Cannot collect noscript activity data from ${url} (tab ${tabId}!)`); // DEV_ONLY
451+
}
452+
} catch (e2) {
453+
error(e2, `Cannot collect seen / access url on tab ${tabId}!`) // DEV_ONLY
449454
}
450455
}
451456
return null;

src/manifest.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"browser_specific_settings": {
66
"gecko": {
77
"id": "{73a6fe31-595d-460b-a920-fcc0f8843232}",
8-
"strict_min_version": "666.0",
8+
"strict_min_version": "66.0",
99
"strict_max_version": "665.0"
1010
},
1111
"gecko_android": {}
1212
},
13-
"version": "12.0.8",
13+
"version": "12.0.8.901",
1414
"description": "__MSG_Description__",
1515
"incognito": "spanning",
1616

@@ -32,6 +32,8 @@
3232
"webNavigation",
3333
"webRequest",
3434
"webRequestBlocking",
35+
"webRequestFilterResponse",
36+
"webRequestFilterResponse.serviceWorkerScript",
3537
"dns",
3638
"<all_urls>"
3739
],
@@ -141,6 +143,14 @@
141143
}
142144
],
143145

146+
"web_accessible_resources": [
147+
{
148+
"resources": [ "/nscl/common/SyncMessage/*" ],
149+
"matches": [ "<all_urls>" ]
150+
}
151+
],
152+
153+
144154
"options_ui": {
145155
"page": "ui/options.html",
146156
"open_in_tab": true

0 commit comments

Comments
 (0)