Skip to content

Commit

Permalink
Scope approvals to documentId
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime committed Jun 22, 2024
1 parent 93cc701 commit cccba15
Show file tree
Hide file tree
Showing 22 changed files with 1,505 additions and 1,190 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-otters-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'chrome-extension': patch
---

scope init to documentId
225 changes: 0 additions & 225 deletions apps/extension/src/approve-origin.test.ts

This file was deleted.

77 changes: 0 additions & 77 deletions apps/extension/src/approve-origin.ts

This file was deleted.

20 changes: 17 additions & 3 deletions apps/extension/src/content-scripts/injected-connection-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ import { PraxMessage } from './message-event';
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client';
import { PraxConnection } from '../message/prax';

// this inits the session client that transports messages on the DOM channel through the Chrome runtime
const initOnce = (req: unknown, _sender: chrome.runtime.MessageSender, respond: () => void) => {
// content script unconditionally announces itself to extension background.
void chrome.runtime.sendMessage(PraxConnection.Init);

// listen for init command from background. this may arrive soon after announce,
// or much later, after a request is made. this activates the channel session
// that transports messages from the DOM channel into the Chrome runtime
const initOnce = (
req: unknown,
// in a content script, sender is always an extension background script
_: chrome.runtime.MessageSender,
// this handler will only ever send an empty response
emptyResponse: (no?: never) => void,
) => {
if (req !== PraxConnection.Init) return false;

chrome.runtime.onMessage.removeListener(initOnce);

// create session, post port to window where the injected global can catch it
const port = CRSessionClient.init(PRAX);
window.postMessage({ [PRAX]: port } satisfies PraxMessage<MessagePort>, '/', [port]);
respond();

emptyResponse();
return true;
};

Expand Down
17 changes: 5 additions & 12 deletions apps/extension/src/content-scripts/injected-penumbra-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,12 @@ const requestResponseListener = (msg: MessageEvent<unknown>) => {
if (msg.origin === window.origin) {
if (isPraxFailureMessageEvent(msg)) {
// @ts-expect-error - ts can't understand the injected string
const status = msg.data[PRAX] as PraxConnection;
const status: unknown = msg.data[PRAX];
const failure = new Error('Connection request failed');
switch (status) {
case PraxConnection.Denied:
failure.cause = PenumbraRequestFailure.Denied;
break;
case PraxConnection.NeedsLogin:
failure.cause = PenumbraRequestFailure.NeedsLogin;
break;
default:
failure.cause = 'Unknown';
break;
}
failure.cause =
typeof status === 'string' && status in PenumbraRequestFailure
? status
: `Unknown failure: ${String(status)}`;
request.reject(failure);
}
}
Expand Down
19 changes: 9 additions & 10 deletions apps/extension/src/content-scripts/injected-request-listener.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { PraxMessage, isPraxRequestMessageEvent } from './message-event';
import { PraxConnection } from '../message/prax';
import { PenumbraRequestFailure } from '@penumbra-zone/client';

const handleRequest = (ev: MessageEvent<unknown>) => {
if (ev.origin === window.origin && isPraxRequestMessageEvent(ev)) {
void (async () => {
window.removeEventListener('message', handleRequest);
const result = await chrome.runtime.sendMessage<

// any response to this message only indicates failure. success is
// resolved upon successful connection, and those messages are handled by
// the script in injected-connection-port
const failure = await chrome.runtime.sendMessage<
PraxConnection,
Exclude<PraxConnection, PraxConnection.Request>
undefined | PenumbraRequestFailure
>(PraxConnection.Request);
// init is handled by injected-connection-port
if (result !== PraxConnection.Init)
window.postMessage(
{ [PRAX]: result } satisfies PraxMessage<
PraxConnection.Denied | PraxConnection.NeedsLogin
>,
'/',
);
if (failure)
window.postMessage({ [PRAX]: failure } satisfies PraxMessage<PenumbraRequestFailure>, '/');
})();
}
};
Expand Down
Loading

0 comments on commit cccba15

Please sign in to comment.