Skip to content

Commit

Permalink
fix(extension): #107: allow http for localhost (#129)
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
  • Loading branch information
VanishMax committed Aug 5, 2024
1 parent af52afa commit 8141aa4
Show file tree
Hide file tree
Showing 2 changed files with 14 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,7 +16,7 @@
},
"content_scripts": [
{
"matches": ["https://*/*"],
"matches": ["https://*/*", "http://localhost:*/*"],
"js": [
"injected-connection-port.js",
"injected-disconnect-listener.js",
Expand All @@ -25,7 +25,7 @@
"run_at": "document_start"
},
{
"matches": ["https://*/*"],
"matches": ["https://*/*", "http://localhost:*/*"],
"js": ["injected-penumbra-global.js"],
"run_at": "document_start",
"world": "MAIN"
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 8141aa4

Please sign in to comment.