From 7ef9967c8a17215575be6dca47fbddcd614bafbe Mon Sep 17 00:00:00 2001 From: hackademix Date: Sat, 29 Sep 2018 01:54:22 +0200 Subject: Lenient WebLabels matching (by URL, by id, by name). --- bg/ExternalLicenses.js | 44 ++++++++++++++++++++++++++++++++------------ main_background.js | 5 +++-- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js index 27d9223..058b651 100644 --- a/bg/ExternalLicenses.js +++ b/bg/ExternalLicenses.js @@ -26,10 +26,27 @@ "use strict"; let licensesByLabel = new Map(); +let licensesByUrl = new Map(); { let {licenses} = require("../license_definitions"); - for (let l of Object.values(licenses).filter(l => l.identifier)) { - licensesByLabel.set(l.identifier, l); + for (let [id, l] of Object.entries(licenses)) { + let {identifier, canonicalUrl, licenseName} = l; + if (identifier) { + licensesByLabel.set(identifier, l); + } else { + l.identifier = id; + } + if (id !== identifier) { + licensesByLabel.set(id, l); + } + if (licenseName) { + licensesByLabel.set(licenseName, l); + } + if (Array.isArray(canonicalUrl)) { + for (let url of canonicalUrl) { + licensesByUrl.set(url, l); + } + } } } @@ -55,23 +72,26 @@ var ExternalLicenses = { return null; } scriptInfo.licenses = new Set(); - scriptInfo.allFree = true; scriptInfo.toString = function() { let licenseIds = [...this.licenses].map(l => l.identifier).sort().join(", "); return licenseIds - ? (this.allFree ? `Free license${this.licenses.length > 1 ? "s" : ""} (${licenseIds})` - : `Mixed free (${licenseIds}) and unknown licenses`) + ? `Free license${this.licenses.size > 1 ? "s" : ""} (${licenseIds})` : "Unknown license(s)"; } - - for (let {label} of scriptInfo.licenseLinks) { - if (licensesByLabel.has(label)) { - scriptInfo.licenses.add(licensesByLabel.get(label)); - } else { - scriptInfo.allFree = false; - break; + let match = (map, key) => { + if (map.has(key)) { + scriptInfo.licenses.add(map.get(key)); + return true; } + return false; + }; + + for (let {label, url} of scriptInfo.licenseLinks) { + match(licensesByLabel, label = label.trim()) || + match(licensesByUrl, url) || + match(licensesByLabel, label.replace(/^GNU-|-(?:or-later|only)$/i, '')); } + scriptInfo.free = scriptInfo.licenses.size > 0; return scriptInfo; }, diff --git a/main_background.js b/main_background.js index 4cfca7a..cdc987e 100644 --- a/main_background.js +++ b/main_background.js @@ -859,6 +859,7 @@ var ResponseHandler = { let {request} = response; let {url, type, tabId, frameId, documentUrl} = request; + let fullUrl = url; url = ListStore.urlItem(url); let site = ListStore.siteItem(url); @@ -888,11 +889,11 @@ var ResponseHandler = { "whitelisted": [url, whitelistedSite ? `User whitelisted ${site}` : "Whitelisted by user"]}); return ResponseProcessor.ACCEPT; } else { - let scriptInfo = await ExternalLicenses.check({url, tabId, frameId, documentUrl}); + let scriptInfo = await ExternalLicenses.check({url: fullUrl, tabId, frameId, documentUrl}); if (scriptInfo) { let verdict, ret; let msg = scriptInfo.toString(); - if (scriptInfo.allFree) { + if (scriptInfo.free) { verdict = "accepted"; ret = ResponseProcessor.ACCEPT; } else { -- cgit v1.2.3 From 37774e035b5ebf53e2fde92b11e4f3419b53909c Mon Sep 17 00:00:00 2001 From: hackademix Date: Sun, 30 Sep 2018 00:12:17 +0200 Subject: WebLabels matching by id made case-insensitive. --- bg/ExternalLicenses.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bg/ExternalLicenses.js b/bg/ExternalLicenses.js index 058b651..33bcbf6 100644 --- a/bg/ExternalLicenses.js +++ b/bg/ExternalLicenses.js @@ -29,18 +29,19 @@ let licensesByLabel = new Map(); let licensesByUrl = new Map(); { let {licenses} = require("../license_definitions"); + let mapByLabel = (label, license) => licensesByLabel.set(label.toUpperCase(), license); for (let [id, l] of Object.entries(licenses)) { let {identifier, canonicalUrl, licenseName} = l; if (identifier) { - licensesByLabel.set(identifier, l); + mapByLabel(identifier, l); } else { l.identifier = id; } if (id !== identifier) { - licensesByLabel.set(id, l); + mapByLabel(id, l); } if (licenseName) { - licensesByLabel.set(licenseName, l); + mapByLabel(licenseName, l); } if (Array.isArray(canonicalUrl)) { for (let url of canonicalUrl) { @@ -87,9 +88,9 @@ var ExternalLicenses = { }; for (let {label, url} of scriptInfo.licenseLinks) { - match(licensesByLabel, label = label.trim()) || + match(licensesByLabel, label = label.trim().toUpperCase()) || match(licensesByUrl, url) || - match(licensesByLabel, label.replace(/^GNU-|-(?:or-later|only)$/i, '')); + match(licensesByLabel, label.replace(/^GNU-|-(?:OR-LATER|ONLY)$/, '')); } scriptInfo.free = scriptInfo.licenses.size > 0; return scriptInfo; -- cgit v1.2.3 From 43502637288f9f60019be2759e5049d0fe9377fe Mon Sep 17 00:00:00 2001 From: hackademix Date: Tue, 2 Oct 2018 15:50:23 +0200 Subject: Jasmine-based automated tests suite. --- .gitignore | 1 + README | 11 ++ build.sh | 15 +- common/Test.js | 55 +++++++ html/common.css | 12 ++ html/display_panel/content/display-panel.html | 2 + html/display_panel/content/main_panel.js | 12 ++ html/display_panel/content/panel-styles.css | 3 +- main_background.js | 15 ++ test/SpecRunner.html | 43 ++++++ test/resources/app-trilicensed.js | 1 + test/resources/index.html | 38 +++++ test/resources/jquery.js | 1 + test/resources/jslicense.html | 25 +++ test/resources/proprietary.js | 1 + test/resources/tracker.js | 1 + test/spec/LibreJSSpec.js | 212 ++++++++++++++++++++++++++ 17 files changed, 446 insertions(+), 2 deletions(-) create mode 100644 common/Test.js create mode 100644 test/SpecRunner.html create mode 100644 test/resources/app-trilicensed.js create mode 100644 test/resources/index.html create mode 100644 test/resources/jquery.js create mode 100644 test/resources/jslicense.html create mode 100644 test/resources/proprietary.js create mode 100644 test/resources/tracker.js create mode 100644 test/spec/LibreJSSpec.js diff --git a/.gitignore b/.gitignore index 9a139c8..3314ced 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ SDK LibreJS source/ # dependencies node_modules/ +test/lib/ # artifacts librejs.xpi diff --git a/README b/README index a380900..f5cf254 100644 --- a/README +++ b/README @@ -15,6 +15,11 @@ To build the extension run: To build the extension plus create a .xpi package run: $ ./build.sh +To build the extension including the automated test suite (see TEST below) run: + $ ./build.sh -t + or + $ ./build.sh --test + Note: this build.sh script relies on no new source files being created. @@ -24,6 +29,12 @@ To debug this add-on on IceCat and other Firefox derivatives, browse to the spec LibreJS should work with other WebExtensions-compliant browsers; but currently, none of them meet the freedom standards of GNU, so no help will be provided for their usage. +TEST: + +An automated test suite runs automatically in its own tab whenever the extension +is loaded as a "temporary add-on" from about:debugging. +Otherwise (if included in the xpi, see BUILD above) it can be launched from the +UI by clicking the [Automated self test...] button. CONTACT: diff --git a/build.sh b/build.sh index 1e07fcb..0cd41d7 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,16 @@ PATH=$PATH:./node_modules/.bin which browserify > /dev/null || (echo "can not find browserify" && false) || exit +# prepare / update testing environment if needed +JASMINE_VER=3.2.1 +JASMINE_LIB="test/lib/jasmine-$JASMINE_VER" +if ! [ -d "$JASMINE_LIB" ]; then + mkdir -p test/lib + rm -rf test/lib/jasmine-* + JASMINE_URL="https://github.com/jasmine/jasmine/releases/download/v$JASMINE_VER/jasmine-standalone-$JASMINE_VER.zip" + curl -L -o "$JASMINE_LIB.zip" "$JASMINE_URL" && unzip -d test/ $JASMINE_LIB.zip lib/** + rm -f "$JASMINE_LIB.zip" +fi # Build the main file browserify main_background.js -o bundle.js @@ -9,7 +19,10 @@ browserify main_background.js -o bundle.js mkdir ./build_temp # Move source files to temp directory -cp -r icons ./build_temp +if [ "$1" == "-t" -o "$1" == "--test" ]; then + cp -r ./test ./build_temp +fi +cp -r ./icons ./build_temp cp -r ./html ./build_temp cp -r ./content ./build_temp cp -r ./common ./build_temp diff --git a/common/Test.js b/common/Test.js new file mode 100644 index 0000000..e272d1e --- /dev/null +++ b/common/Test.js @@ -0,0 +1,55 @@ +/** +* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. +* +* Copyright (C) 2018 Giorgio Maone +* +* This file is part of GNU LibreJS. +* +* GNU LibreJS is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GNU LibreJS is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GNU LibreJS. If not, see . +*/ + +"use strict"; +var Test = (() => { + const RUNNER_URL = browser.extension.getURL("/test/SpecRunner.html"); + return { + /* + returns RUNNER_URL if it's a test-enabled build or an about:debugging + temporary extension session, null otherwise + */ + async getURL() { + let url = RUNNER_URL; + try { + await fetch(url); + } catch (e) { + url = null; + } + this.getURL = () => url; + return url; + }, + + async getTab(activate = false) { + let url = await this.getURL(); + let tab = url ? (await browser.tabs.query({url}))[0] || + (await browser.tabs.create({url})) + : null; + if (tab && activate) { + await browser.tabs.update(tab.id, {active: true}); + } + return tab; + } + }; +})(); +if (typeof module === "object") { + module.exports = Test; +} diff --git a/html/common.css b/html/common.css index cf2c5d1..14931fa 100644 --- a/html/common.css +++ b/html/common.css @@ -3,6 +3,7 @@ html { margin:0px; color:#000 !important; background:url('background-panel.png') !important; + font-family: sans-serif; } body { padding:0; @@ -26,4 +27,15 @@ h1.libre { font-weight:bold; background:url('librejs-title.png') no-repeat top left; text-indent:-1000px; + position: relative; +} +h1.libre span { + font-family: sans-serif; + position: absolute; + bottom: 32px; + padding: 0 16px; + left: 230px; + display: block; + text-indent: 0; + vertical-align: bottom; } diff --git a/html/display_panel/content/display-panel.html b/html/display_panel/content/display-panel.html index df153b3..80f4cb9 100644 --- a/html/display_panel/content/display-panel.html +++ b/html/display_panel/content/display-panel.html @@ -53,6 +53,7 @@ +
@@ -100,5 +101,6 @@
+ diff --git a/html/display_panel/content/main_panel.js b/html/display_panel/content/main_panel.js index c55b167..2509545 100644 --- a/html/display_panel/content/main_panel.js +++ b/html/display_panel/content/main_panel.js @@ -38,6 +38,18 @@ myPort.postMessage({"update": true, tabId: parseInt(currentReport && currentRepo // Display the actual extension version Number document.querySelector("#version").textContent = browser.runtime.getManifest().version; +// Enable autotest button if this is a test-enabled build / session +(async () => { + if (await Test.getURL()) { + let button = document.querySelector("#autotest"); + button.style.display = "block"; + button.onclick = async () => { + await Test.getTab(true); + close(); + } + } +})(); + var liTemplate = document.querySelector("#li-template"); liTemplate.remove(); diff --git a/html/display_panel/content/panel-styles.css b/html/display_panel/content/panel-styles.css index 0d849e4..940a8ff 100644 --- a/html/display_panel/content/panel-styles.css +++ b/html/display_panel/content/panel-styles.css @@ -141,7 +141,8 @@ span.blocked { display: none; } -.tab #must-reload, .tab #buttons, .empty #buttons { +.tab #must-reload, .tab #buttons, +.empty #complain, .empty #report-tab, #autotest { display: none; } diff --git a/main_background.js b/main_background.js index cdc987e..5c2e8df 100644 --- a/main_background.js +++ b/main_background.js @@ -1175,6 +1175,21 @@ async function init_addon() { ResponseProcessor.install(ResponseHandler); legacy_license_lib.init(); + + + let Test = require("./common/Test"); + if (Test.getURL()) { + // export testable functions to the global scope + this.LibreJS = { + editHtml, + handle_script, + ExternalLicenses, + }; + // create or focus the autotest tab if it's a debugging session + if ((await browser.management.getSelf()).installType === "development") { + Test.getTab(true); + } + } } diff --git a/test/SpecRunner.html b/test/SpecRunner.html new file mode 100644 index 0000000..42a372f --- /dev/null +++ b/test/SpecRunner.html @@ -0,0 +1,43 @@ + + + + + + LibreJS Tests + + + + + + + + + + + + + +

LibreJS Autotest

+ + diff --git a/test/resources/app-trilicensed.js b/test/resources/app-trilicensed.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/app-trilicensed.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/resources/index.html b/test/resources/index.html new file mode 100644 index 0000000..c5f364c --- /dev/null +++ b/test/resources/index.html @@ -0,0 +1,38 @@ + + + + + + LibreJS test document + + + +

LibreJS test document

+JavaScript license information + + + + + + diff --git a/test/resources/jquery.js b/test/resources/jquery.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/jquery.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/resources/jslicense.html b/test/resources/jslicense.html new file mode 100644 index 0000000..0cd0449 --- /dev/null +++ b/test/resources/jslicense.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + +
Fake jQueryExpat +jquery-1.7.tar.gz
App specific script, tri-licensed (2 free and 1 proprietary license) +GNU V3 +
+Expat +
+Proprietary, optional
app-trilicensed.js
App specific script, proprietary only +Proprietaryproprietary.js
diff --git a/test/resources/proprietary.js b/test/resources/proprietary.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/proprietary.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/resources/tracker.js b/test/resources/tracker.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/tracker.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/spec/LibreJSSpec.js b/test/spec/LibreJSSpec.js new file mode 100644 index 0000000..8638d67 --- /dev/null +++ b/test/spec/LibreJSSpec.js @@ -0,0 +1,212 @@ +/* +* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. +* +* Copyright (C) 2018 Giorgio Maone +* +* This file is part of GNU LibreJS. +* +* GNU LibreJS is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GNU LibreJS is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GNU LibreJS. If not, see . +*/ +"use strict"; + +describe("LibreJS' components", () => { + const LENIENT_TAG_MSG = + `actual requirement? at this moment (20181001) + formal validity only gets checked, therefore this test would fail.`; + + let LibreJS = browser.extension.getBackgroundPage().LibreJS; + let license = { + id: 'GPL-3.0', + url: 'http://www.gnu.org/licenses/gpl-3.0.html', + magnet: 'magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt', + }; + let unknownLicense = { + id: 'Acme-proprietary-1.5', + url: 'http://www.acme.com/license-1.5.html', + magnet: 'magnet:?xt=urn:btih:2f739d935676111cfff4b4693e3816e664797050&dn=acme-1.5.txt' + }; + + let trivial = "1+1"; + let nontrivial = `function nt() { document.documentElement.innerHTML=""; nt(); }`; + let licensed = `// @license ${license.id} ${license.magnet}\n${nontrivial}\n// @license-end`; + let unknownLicensed = `// @license ${unknownLicense.id} ${unknownLicense.magnet}\n${nontrivial}\n// @license-end`; + let malformedLicensed = `// @license\n${nontrivial}`; + + let tab; + + beforeAll(async () => { + let url = browser.extension.getURL("/test/resources/index.html"); + tab = (await browser.tabs.query({url}))[0] || (await browser.tabs.create({url})); + }); + + describe("The external script source processor", () => { + let url = "https://www.gnu.org/mock-script.js"; + + let processScript = async (source, whitelisted = false) => + await LibreJS.handle_script({ + text: source, + request: {url, tabId: tab.id, documentUrl: tab.url, frameId: 0}, + }, whitelisted); + + it("should accept whitelisted scripts", async () => { + expect(await processScript(nontrivial, true) || nontrivial).toContain(nontrivial); + }); + + it("should block trivial scripts too", async () => { + let processed = await processScript(trivial); + expect(processed || trivial).not.toContain(trivial); + }); + + it("should block non-trivial scripts", async () => { + let processed = await processScript(nontrivial); + expect(processed || nontrivial).not.toContain(nontrivial); + }); + + it("should accept scripts with known free license tags", async () => { + let processed = await processScript(licensed); + expect(processed || licensed).toContain(nontrivial); + }); + + xit("should block scripts with unknown license tags", async () => { + let processed = await processScript(unknownLicensed); + expect(processed).not.toContain(nontrivial); + }).pend(LENIENT_TAG_MSG); + + it("should block scripts with malformed license tags", async () => { + let processed = await processScript(malformedLicensed); + expect(processed).not.toContain(nontrivial); + }); + }); + + describe("The HTML processor", () => { + let processHtml = + async (html, whitelisted = false) => + LibreJS.editHtml(html, tab.url, tab.id, 0, whitelisted); + + let addScript = (html, script, before = "") => + html.replace(before, `${before}`); + + function extractScripts(html, def = "") { + let matches = html && html.match(/