summaryrefslogblamecommitdiff
path: root/background.js
blob: 59c6c2b34547d40476344d628257296bd8abc03b (plain) (tree)
1
2
3
4

                                                                                                 

               

























                                                                                 





                                            








                                                                      



















                                                                                   
// Based on https://github.com/mdn/webextensions-examples/blob/main/root-cert-stats/background.js

let certs = [];

// On header receive, inspect cert and update app icon as required
async function onHeaderReceive(details) {
    try {
        await certInspectUpdate(details.requestId);
    } catch(error) {
        console.error(error);
    }
}

async function certInspectUpdate(requestId) {
    let securityInfo = await browser.webRequest.getSecurityInfo(
        requestId,
        {
            "certificateChain": true
        }
    );
    
    if (securityInfo.state !== "secure" || securityInfo.isUntrusted) {
        setIcon("nope");
        return;
    }

    // Flagged as "secure" - check if CA is against any of our flagged CAs

    // root is last in the array cert chain
    let rootCA = securityInfo.certificates[securityInfo.certificates.length - 1];

    for (let cert of certs) {
        if (rootCA.subject.includes(cert)) {
            setIcon("nope");
            return;
        }
    }

    setIcon("ok");
}

function setIcon(icon) {
    browser.browserAction.setIcon({ path: "icons/" + icon + ".png" });
}

function onReady() {
    // Listen for all header receive events, which contain the cert details we want
    browser.webRequest.onHeadersReceived.addListener(
        onHeaderReceive,
        {
            urls: ["<all_urls>"]
        },
        [
            "blocking"
        ]
    );
}

// Fetch config for certs list
const getting = browser.storage.sync.get("certs");
getting.then(saved => {
    certs = saved.certs;
    console.log("certs=" + certs)
    onReady();
}, console.error);