Skip to content

Commit

Permalink
Update to use new blocklisting mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
kewisch committed Nov 2, 2021
1 parent ba8ad09 commit 8baeaf3
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 282 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amoqueue-wx",
"version": "10.4.5",
"version": "10.4.6",
"author": "Philipp Kewisch <[email protected]>",
"homepage": "https://github.com/kewisch/amo-helper#readme",
"repository": {
Expand Down
227 changes: 32 additions & 195 deletions src/background/blocklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,222 +3,59 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch, 2019 */

let gBlocklist = [];
let gGatherGuids = [];

/**
* This is taken from addons-server and should be rewritten to be pretty and accurate
* Copyright (c) 2010 - 2019, Mozilla Corporation
* All rights reserved.
*/
/* eslint-disable */
var VersionCompare = {
/**
* Mozilla-style version numbers comparison in Javascript
* (JS-translated version of PHP versioncompare component)
* @return -1: a<b, 0: a==b, 1: a>b
*/
compareVersions: function(a, b) {
var al = a.split("."),
bl = b.split("."),
ap, bp, r, i;
for (i=0; i<al.length || i<bl.length; i++) {
ap = (i<al.length ? al[i] : null);
bp = (i<bl.length ? bl[i] : null);
r = this.compareVersionParts(ap,bp);
if (r !== 0) {
return r;
}
}
return 0;
},

/**
* helper function: compare a single version part
*/
compareVersionParts: function(ap,bp) {
var avp = this.parseVersionPart(ap),
bvp = this.parseVersionPart(bp),
r = this.cmp(avp["numA"], bvp["numA"]);
if (r) {
return r;
}
r = this.strcmp(avp["strB"], bvp["strB"]);
if (r) {
return r;
}
r = this.cmp(avp["numC"], bvp["numC"]);
if (r) {
return r;
}
return this.strcmp(avp["extraD"], bvp["extraD"]);
},

/**
* helper function: parse a version part
*/
parseVersionPart: function(p) {
if (p == "*") {
return {
"numA": Number.MAX_VALUE,
"strB": "",
"numC": 0,
"extraD": ""
};
}
let pattern = /^([-\d]*)([^-\d]*)([-\d]*)(.*)$/,
m = pattern.exec(p),
r = {
"numA" : parseInt(m[1], 10),
"strB" : m[2],
"numC" : parseInt(m[3], 10),
"extraD" : m[4]
};
if (r["strB"] == "+") {
r["numA"]++;
r["strB"] = "pre";
}
return r;
},

/**
* helper function: compare numeric version parts
*/
cmp: function(an, bn) {
if (isNaN(an)) an = 0;
if (isNaN(bn)) bn = 0;
if (an < bn) {
return -1;
}
if (an > bn) {
return 1;
}
return 0;
},

/**
* helper function: compare string version parts
*/
strcmp: function(as,bs) {
if (as == bs) {
return 0;
}
// any string comes *before* the empty string
if (as === "") {
return 1;
}
if (bs === "") {
return -1;
}
// normal string comparison for non-empty strings (like strcmp)
if (as < bs) {
return -1;
} else if(as > bs) {
return 1;
} else {
return 0;
}
}
};
/* eslint-enable */

async function checkBlocklist(guid, version) {
function verifyVersionRange(entry) {
for (let range of entry.versionRange) {
if (VersionCompare.compareVersions(version, range.minVersion) > 0 &&
(range.maxVersion == "*" || VersionCompare.compareVersions(version, range.maxVersion) < 0)) {
return { severity: range.severity == 3 ? "hard" : "soft" };
}
}

return null;
}
let gGatherGuids = new Set();

async function fileBlocklistBug(guid, name, tabIndex) {
gGatherGuids.push(guid);

for (let entry of gBlocklist) {
let found = null;
if (entry.guid.startsWith("/")) {
let re = new RegExp(entry.guid.substring(1, entry.guid.length - 1));
if (re.test(guid)) {
found = entry;
}
} else if (entry.guid == guid) {
found = entry;
}
let guids = [...gGatherGuids].join("\n");

if (found) {
let result = verifyVersionRange(entry);
if (result) {
result.id = entry.id;
result.bug = entry.details.bug;
result.created = entry.details.created || new Date(entry.last_modified).toISOString();
result.reason = entry.details.why;
return result;
}
}
}

return null;
}
let addfile = await getStoragePreference("blocklist-addfile");

async function downloadBlocklist() {
let resp = await fetch("https://firefox.settings.services.mozilla.com/v1/buckets/blocklists/collections/addons/records");
let json = await resp.json();
if (addfile) {
let tab = await browser.tabs.create({
url: "http://bugzilla.mozilla.org/form.blocklist",
index: tabIndex + 1
});

if (json && json.data && Array.isArray(json.data)) {
gBlocklist = json.data;
await browser.tabs.executeScript(tab.id, {
code: `
document.getElementById("blocklist_guids").value = ${JSON.stringify(guids)};
document.getElementById("blocklist_name").value = ${JSON.stringify(name)};
document.getElementById("blocklist_reason").focus();
`
});
} else {
console.error("Error loading blocklist");
let params = new URLSearchParams({ guids: guids });
browser.tabs.create({
url: "https://addons-internal.prod.mozaws.net/en-US/admin/models/blocklist/blocklistsubmission/add/?" + params,
index: tabIndex + 1
});
}
}

async function fileBlocklistBug(guid, name) {
gGatherGuids.push(guid);

let guids = [...new Set(gGatherGuids.filter(Boolean))].join("\n");
let tab = await browser.tabs.create({ url: "http://bugzilla.mozilla.org/form.blocklist" });

await browser.tabs.executeScript(tab.id, {
code: `
document.getElementById("blocklist_guids").value = ${JSON.stringify(guids)};
document.getElementById("blocklist_name").value = ${JSON.stringify(name)};
document.getElementById("blocklist_reason").focus();
`
});

gGatherGuids = [];
gGatherGuids = new Set();
}

async function gatherBlocklistBug(guid) {
gGatherGuids.push(guid);
}

browser.alarms.create("blocklist", { periodInMinutes: 720 });

browser.alarms.onAlarm.addListener((alarm) => {
if (alarm.name != "blocklist") {
return;
if (guid) {
gGatherGuids.add(guid);
}
downloadBlocklist();
});
}

browser.runtime.onMessage.addListener((data, sender) => {
if (data.action != "blocklist") {
return undefined;
}

if (data.method == "check") {
return checkBlocklist(data.guid, data.version);
} else if (data.method == "refresh") {
return downloadBlocklist();
} else if (data.method == "file") {
return fileBlocklistBug(data.guid, data.name);
console.log(data);

if (data.method == "file") {
return fileBlocklistBug(data.guid, data.name, sender.tab.index);
} else if (data.method == "gather") {
return gatherBlocklistBug(data.guid);
} else if (data.method == "get") {
return Promise.resolve([...gGatherGuids]);
}

return null;
});

// Initial download
downloadBlocklist();
3 changes: 2 additions & 1 deletion src/common/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const DEFAULT_PREFERENCES = {
"tinderbar-approve-text": "",
"tinderbar-preload-tabs": 3,
"filewindow-enabled": false,
"filewindow-exclusive": false
"filewindow-exclusive": false,
"blocklist-addfile": false
};

const HIDDEN_PREFERENCES = {
Expand Down
27 changes: 4 additions & 23 deletions src/content/queueinfo/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function getInfo(doc) {

let versions = Array.from(doc.querySelectorAll(".review-files .listing-body, #review-files .listing-body")).map((listbody, idx) => {
let headerparts = listbody.previousElementSibling.firstElementChild.textContent.match(/Version ([^·]+)· ([^·]+)· (.*)/);
let blocked = !!listbody.previousElementSibling.querySelector(".blocked-version");
let submissiondate = floatingtime(headerparts[2].trim(), true);
let hasAutoApproval = false;
let hasConfirmApproval = false;
Expand Down Expand Up @@ -218,6 +219,7 @@ function getInfo(doc) {
version: headerparts[1].trim(),
date: submissiondate,
status: status,
blocked: blocked,

installurl: installanchor ? (new URL(installanchor.getAttribute("href"), location.href)).href : null,
sourceurl: sourceanchor ? (new URL(sourceanchor.getAttribute("href"), location.href)).href : null,
Expand Down Expand Up @@ -284,29 +286,8 @@ async function updateSize(info) {

// Check the blocklist
let lastVersion = info.versions[info.latest_idx];
let blocked = await browser.runtime.sendMessage({ action: "blocklist", method: "check", guid: info.guid, version: lastVersion ? lastVersion.version : "0" });
if (blocked) {
document.getElementById("main-wrapper").classList.add("amoqueue-striped-background", "amoqueue-blocklisted-" + blocked.severity);

let bugspan = document.createElement("span");

let buglink = bugspan.appendChild(document.createElement("a"));
buglink.href = blocked.bug;
buglink.target = "_blank";
buglink.textContent = blocked.bug.replace("https://bugzilla.mozilla.org/show_bug.cgi?id=", "bug ");


bugspan.appendChild(document.createTextNode(` on ${blocked.created.substr(0, 10)} (kinto `));
console.log(blocked);

let kintolink = bugspan.appendChild(document.createElement("a"));
kintolink.href = `https://settings-writer.prod.mozaws.net/v1/admin/#/buckets/staging/collections/addons/records/${blocked.id}/attributes`;
kintolink.target = "_blank";
kintolink.textContent = blocked.id;

bugspan.appendChild(document.createTextNode(")"));

createSummaryRow("amoqueue-blocklist-bug", "Blocklisted", [bugspan, blocked.reason]);
if (lastVersion?.blocked) {
document.getElementById("main-wrapper").classList.add("amoqueue-striped-background", "amoqueue-blocklisted");
}

await browser.runtime.sendMessage({ action: "infostorage", op: "set", storage: "slug", keys: { ["slugInfo." + info.slug]: info.id } });
Expand Down
5 changes: 1 addition & 4 deletions src/content/reviewinfo/review.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@
margin-bottom: 2em;
}

#main-wrapper.amoqueue-blocklisted-hard {
#main-wrapper.amoqueue-blocklisted {
background: repeating-linear-gradient(45deg, #fff, #fff 10px, #fbd4dc 10px, #fbd4dc 20px);
}
#main-wrapper.amoqueue-blocklisted-soft {
background: repeating-linear-gradient(45deg, #fff, #fff 10px, #f2eca2 10px, #f2eca2 20px);
}

#main-wrapper.amoqueue-blocklisted #addon {
padding: 10px;
Expand Down
Loading

0 comments on commit 8baeaf3

Please sign in to comment.