Skip to content

Commit

Permalink
fix(extension): #107: allow http for localhost (#153)
Browse files Browse the repository at this point in the history
* fix(extension): #107: allow http for localhost

* fix(extension): #107: allow http for localhost only for dev environment

* fix(extension): #107: fix matching urls in the manifest

* fix(extension): #107: add more test cases
  • Loading branch information
VanishMax authored Aug 13, 2024
1 parent 73017fd commit 74258a8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions apps/extension/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
},
"content_scripts": [
{
"matches": ["https://*/*"],
"matches": ["https://*/*", "http://localhost/*"],
"js": ["injected-connection-port.js", "injected-request-listener.js"],
"run_at": "document_start"
},
{
"matches": ["https://*/*"],
"matches": ["https://*/*", "http://localhost/*"],
"js": ["injected-penumbra-global.js"],
"run_at": "document_start",
"world": "MAIN"
Expand Down
30 changes: 30 additions & 0 deletions apps/extension/src/senders/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ describe('assertValidSender', () => {
expect(() => assertValidSender(invalidProtocol)).toThrow('Sender protocol is not');
});

it(`throws if sender protocol is http and origin is localhost but not in dev mode`, () => {
globalThis.__DEV__ = true;
const localhostSender: chrome.runtime.MessageSender = {
...mockValid,
origin: 'http://localhost:8000',
url: 'http://localhost:8000/index.html',
};
expect(assertValidSender(localhostSender)).toMatchObject(localhostSender);
});

it(`succeeds if sender protocol is http and origin is localhost in dev mode`, () => {
globalThis.__DEV__ = true;
const localhostSender: chrome.runtime.MessageSender = {
...mockValid,
origin: 'http://localhost',
url: 'http://localhost/index.html',
};
expect(assertValidSender(localhostSender)).toMatchObject(localhostSender);
});

it(`succeeds if sender protocol is http and origin is localhost with port specified in dev mode`, () => {
globalThis.__DEV__ = true;
const localhostSender: chrome.runtime.MessageSender = {
...mockValid,
origin: 'http://localhost:8000',
url: 'http://localhost:8000/index.html',
};
expect(assertValidSender(localhostSender)).toMatchObject(localhostSender);
});

it('throws if sender has no URL', () => {
const urlless: chrome.runtime.MessageSender = {
...mockValid,
Expand Down
18 changes: 12 additions & 6 deletions apps/extension/src/senders/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ type ValidSender = chrome.runtime.MessageSender & {
frameId: 0;
documentId: string;
tab: chrome.tabs.Tab & { id: number };

// the relationship between origin and url is pretty complex.
// just rely on the browser's tools.
origin: `${ValidProtocol}//${string}`;
url: `${ValidProtocol}//${string}/${string}`;
origin: string;
url: string;
};

const isHttpLocalhost = (url: URL): boolean =>
url.protocol === 'http:' && url.hostname === 'localhost';

export const assertValidSender = (sender?: chrome.runtime.MessageSender) => {
if (!sender) {
throw new Error('Sender undefined');
Expand All @@ -34,7 +34,13 @@ export const assertValidSender = (sender?: chrome.runtime.MessageSender) => {
if (parsedOrigin.origin !== sender.origin) {
throw new Error('Sender origin is invalid');
}
if (!(parsedOrigin.protocol in ValidProtocol)) {

if (
!(
parsedOrigin.protocol in ValidProtocol ||
(globalThis.__DEV__ && isHttpLocalhost(parsedOrigin))
)
) {
throw new Error(`Sender protocol is not ${Object.values(ValidProtocol).join(',')}`);
}

Expand Down

0 comments on commit 74258a8

Please sign in to comment.