-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04eef12
commit 5c99283
Showing
6 changed files
with
47 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
export const isInternalSender = (sender: chrome.runtime.MessageSender): boolean => | ||
sender.origin === origin && sender.id === chrome.runtime.id; | ||
export const isInternalSender = ( | ||
sender?: chrome.runtime.MessageSender, | ||
): sender is InternalSender => { | ||
if (sender?.origin && sender.id === chrome.runtime.id) { | ||
const senderUrl = new URL(sender.origin); | ||
return senderUrl.protocol === 'chrome-extension:' && senderUrl.host === chrome.runtime.id; | ||
} | ||
return false; | ||
}; | ||
|
||
export type InternalSender = chrome.runtime.MessageSender & { | ||
origin: string; | ||
id: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { alreadyApprovedSender } from './approve'; | ||
import { assertValidSender, type ValidSender } from './validate'; | ||
import { type InternalSender, isInternalSender } from './internal'; | ||
|
||
export const assertValidSessionPort = async (port: chrome.runtime.Port) => { | ||
if (isInternalSender(port.sender)) { | ||
return port as chrome.runtime.Port & { sender: InternalSender }; | ||
} | ||
|
||
const validSender = assertValidSender(port.sender); | ||
if (await alreadyApprovedSender(validSender)) { | ||
return port as chrome.runtime.Port & { sender: ValidSender }; | ||
} | ||
|
||
throw new Error('Session sender is not approved', { cause: port.sender?.origin }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters