Skip to content

Commit

Permalink
fix(extension): #107: allow http for localhost only for dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
VanishMax authored and turbocrime committed Aug 5, 2024
1 parent f92e587 commit e9019d1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions apps/extension/src/senders/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ 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 isException = (url: URL): boolean => {
return url.protocol === 'http:' && url.hostname === 'localhost';
};
const isHttpLocalhost = (url: URL): boolean =>
url.protocol === 'http:' && url.hostname === 'localhost';

export const assertValidSender = (sender?: chrome.runtime.MessageSender) => {
if (!sender) {
Expand All @@ -39,7 +35,12 @@ export const assertValidSender = (sender?: chrome.runtime.MessageSender) => {
throw new Error('Sender origin is invalid');
}

if (!(parsedOrigin.protocol in ValidProtocol || isException(parsedOrigin))) {
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 e9019d1

Please sign in to comment.