Skip to content

Commit

Permalink
Merge pull request facebookincubator#344 from ezzak/css_verif_refac
Browse files Browse the repository at this point in the history
CSS coverage
  • Loading branch information
ezzak authored Nov 1, 2024
2 parents 1ca765c + 44f4321 commit 75670cb
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 300 deletions.
54 changes: 28 additions & 26 deletions src/js/__tests__/content-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import {jest} from '@jest/globals';
import {MESSAGE_TYPE} from '../config';
import {
hasInvalidScripts,
scanForScripts,
FOUND_SCRIPTS,
storeFoundJS,
hasInvalidScriptsOrStyles,
scanForScriptsAndStyles,
FOUND_ELEMENTS,
storeFoundElement,
UNINITIALIZED,
} from '../content';
import {setCurrentOrigin} from '../content/updateCurrentState';
Expand All @@ -22,21 +22,22 @@ describe('content', () => {
beforeEach(() => {
window.chrome.runtime.sendMessage = jest.fn(() => {});
setCurrentOrigin('FACEBOOK');
FOUND_SCRIPTS.clear();
FOUND_SCRIPTS.set(UNINITIALIZED, []);
FOUND_ELEMENTS.clear();
FOUND_ELEMENTS.set(UNINITIALIZED, []);
});
describe('storeFoundJS', () => {
describe('storeFoundElement', () => {
it('should handle scripts with src correctly', () => {
const fakeUrl = 'https://fancytestingyouhere.com/';
const fakeScriptNode = {
src: fakeUrl,
getAttribute: () => {
return '123_main';
},
nodeName: 'SCRIPT',
};
storeFoundJS(fakeScriptNode);
expect(FOUND_SCRIPTS.get('123').length).toEqual(1);
expect(FOUND_SCRIPTS.get('123')[0].src).toEqual(fakeUrl);
storeFoundElement(fakeScriptNode);
expect(FOUND_ELEMENTS.get('123').length).toEqual(1);
expect(FOUND_ELEMENTS.get('123')[0].src).toEqual(fakeUrl);
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(1);
});
it('should send update icon message if valid', () => {
Expand All @@ -46,23 +47,24 @@ describe('content', () => {
getAttribute: () => {
return '123_main';
},
nodeName: 'SCRIPT',
};
storeFoundJS(fakeScriptNode);
storeFoundElement(fakeScriptNode);
const sentMessage = window.chrome.runtime.sendMessage.mock.calls[0][0];
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(1);
expect(sentMessage.type).toEqual(MESSAGE_TYPE.UPDATE_STATE);
});
it.skip('storeFoundJS keeps existing icon if not valid', () => {
it.skip('storeFoundElement keeps existing icon if not valid', () => {
// TODO: come back to this after testing processFoundJS
});
});
describe('hasInvalidScripts', () => {
describe('hasInvalidScriptsOrStyles', () => {
it('should not check for non-HTMLElements', () => {
const fakeElement = {
nodeType: 2,
tagName: 'tagName',
};
hasInvalidScripts(fakeElement);
hasInvalidScriptsOrStyles(fakeElement);
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(0);
});
it('should store any script elements we find', () => {
Expand All @@ -74,10 +76,10 @@ describe('content', () => {
nodeName: 'SCRIPT',
nodeType: 1,
tagName: 'tagName',
src: '',
src: 'fakeurl',
};
hasInvalidScripts(fakeElement);
expect(FOUND_SCRIPTS.get('123').length).toBe(1);
hasInvalidScriptsOrStyles(fakeElement);
expect(FOUND_ELEMENTS.get('123').length).toBe(1);
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(1);
expect(window.chrome.runtime.sendMessage.mock.calls[0][0].type).toBe(
MESSAGE_TYPE.UPDATE_STATE,
Expand Down Expand Up @@ -110,8 +112,8 @@ describe('content', () => {
nodeName: 'nodename',
tagName: 'tagName',
};
hasInvalidScripts(fakeElement);
expect(FOUND_SCRIPTS.get(UNINITIALIZED).length).toBe(0);
hasInvalidScriptsOrStyles(fakeElement);
expect(FOUND_ELEMENTS.get(UNINITIALIZED).length).toBe(0);
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(0);
});
it('should store any script element direct children', () => {
Expand All @@ -134,7 +136,7 @@ describe('content', () => {
nodeType: 1,
childNodes: [],
tagName: 'tagName',
src: '',
src: 'fakeUrl',
},
],
getAttribute: () => {
Expand All @@ -144,8 +146,8 @@ describe('content', () => {
nodeName: 'nodename',
tagName: 'tagName',
};
hasInvalidScripts(fakeElement);
expect(FOUND_SCRIPTS.get('123').length).toBe(1);
hasInvalidScriptsOrStyles(fakeElement);
expect(FOUND_ELEMENTS.get('123').length).toBe(1);
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(1);
expect(window.chrome.runtime.sendMessage.mock.calls[0][0].type).toBe(
MESSAGE_TYPE.UPDATE_STATE,
Expand Down Expand Up @@ -197,19 +199,19 @@ describe('content', () => {
nodeName: 'nodename',
tagName: 'tagName',
};
hasInvalidScripts(fakeElement);
expect(FOUND_SCRIPTS.get('123').length).toBe(2);
hasInvalidScriptsOrStyles(fakeElement);
expect(FOUND_ELEMENTS.get('123').length).toBe(2);
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(2);
});
});
describe('scanForScripts', () => {
describe('scanForScriptsAndStyles', () => {
it('should find existing script tags in the DOM and check them', () => {
jest.resetModules();
document.body.innerHTML =
'<div>' +
' <script data-btmanifest="123_main" src="https://facebook.com/"></script>' +
'</div>';
scanForScripts();
scanForScriptsAndStyles();
expect(window.chrome.runtime.sendMessage.mock.calls.length).toBe(1);
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/js/__tests__/removeDynamicStrings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ describe('removeDynamicStrings', () => {
).toEqual(`const foo = /*BTDS*/"";`);
});
it('Handles empty strings', () => {
expect(
removeDynamicStrings(`const foo = /*BTDS*/'';`),
).toEqual(`const foo = /*BTDS*/'';`);
expect(removeDynamicStrings(`const foo = /*BTDS*/'';`)).toEqual(
`const foo = /*BTDS*/'';`,
);
});
it('Handles strings in different scenarios', () => {
expect(removeDynamicStrings(`/*BTDS*/'dynamic string';`)).toEqual(
Expand Down
26 changes: 13 additions & 13 deletions src/js/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Manifest = {
};

function getManifestMapForOrigin(origin: Origin): Map<string, Manifest> {
// store manifest to subsequently validate JS
// store manifest to subsequently validate JS/CSS
let manifestMap = MANIFEST_CACHE.get(origin);
if (manifestMap == null) {
manifestMap = new Map();
Expand Down Expand Up @@ -137,7 +137,7 @@ function handleMessages(
return true;
}

case MESSAGE_TYPE.RAW_JS: {
case MESSAGE_TYPE.RAW_SRC: {
const origin = MANIFEST_CACHE.get(message.origin);
if (!origin) {
sendResponse({valid: false, reason: 'no matching origin'});
Expand All @@ -150,9 +150,9 @@ function handleMessages(
return;
}

if (message.rawjs.includes(DYNAMIC_STRING_MARKER)) {
if (message.pkgRaw.includes(DYNAMIC_STRING_MARKER)) {
try {
message.rawjs = removeDynamicStrings(message.rawjs);
message.pkgRaw = removeDynamicStrings(message.pkgRaw);
} catch (e) {
sendResponse({valid: false, reason: 'failed parsing AST'});
return;
Expand All @@ -161,27 +161,27 @@ function handleMessages(

// fetch the src
const encoder = new TextEncoder();
const encodedJS = encoder.encode(message.rawjs);
const encodedSrc = encoder.encode(message.pkgRaw);
// hash the src
crypto.subtle.digest('SHA-256', encodedJS).then(jsHashBuffer => {
const jsHashArray = Array.from(new Uint8Array(jsHashBuffer));
const jsHash = jsHashArray
crypto.subtle.digest('SHA-256', encodedSrc).then(hashBuffer => {
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray
.map(b => b.toString(16).padStart(2, '0'))
.join('');

if (manifestObj.leaves.includes(jsHash)) {
sendResponse({valid: true, hash: jsHash});
if (manifestObj.leaves.includes(hash)) {
sendResponse({valid: true, hash: hash});
} else {
sendResponse({
valid: false,
hash: jsHash,
hash: hash,
reason:
'Error: hash does not match ' +
message.origin +
', ' +
message.version +
', unmatched JS is ' +
message.rawjs,
', unmatched SRC is ' +
message.pkgRaw,
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/js/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const MESSAGE_TYPE = Object.freeze({
DEBUG: 'DEBUG',
LOAD_COMPANY_MANIFEST: 'LOAD_COMPANY_MANIFEST',
POPUP_STATE: 'POPUP_STATE',
RAW_JS: 'RAW_JS',
RAW_SRC: 'RAW_SRC',
UPDATE_STATE: 'UPDATE_STATE',
STATE_UPDATED: 'STATE_UPDATED',
CONTENT_SCRIPT_START: 'CONTENT_SCRIPT_START',
Expand Down
Loading

0 comments on commit 75670cb

Please sign in to comment.