Skip to content

Commit

Permalink
Fix WindowMessageHandler.handleEvent (#406)
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Gieringer <78054+carlgieringer@users.noreply.github.com>
  • Loading branch information
carlgieringer authored May 2, 2023
1 parent 0ea7613 commit b22b9fa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
12 changes: 12 additions & 0 deletions howdju-client-common/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export interface IframedAppMessage {
source: WindowMessageSource;
action: ExtensionFrameAction;
}

export function isIframedAppMessage(
event: MessageEvent<any>
): event is MessageEvent<IframedAppMessage> {
if (!("data" in event)) {
return false;
}
if (!("source" in event.data) || typeof event.data !== "string") {
return false;
}
return true;
}
37 changes: 26 additions & 11 deletions premiser-ui/src/WindowMessageHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { logger } from "./logger";

import { actions, IframedAppMessage, PayloadOf } from "howdju-client-common";
import {
actions,
IframedAppMessage,
isIframedAppMessage,
PayloadOf,
} from "howdju-client-common";

import {
getOrCreateSessionStorageId,
Expand Down Expand Up @@ -30,22 +35,32 @@ export default class WindowMessageHandler {
}

handleEvent(event: MessageEvent<WindowMessage>) {
if ("howdjuTrackingConsent" in event.data) {
if (event.source !== window) {
logger.error("howdjuTrackingConsent must have source === window.");
if (event.source === window) {
if ("howdjuTrackingConsent" in event.data) {
const { enabled } = event.data.howdjuTrackingConsent;
if (enabled) {
getOrCreateSessionStorageId();
} else {
clearSessionStorageId();
}
return;
}
const { enabled } = event.data.howdjuTrackingConsent;
if (enabled) {
getOrCreateSessionStorageId();
} else {
clearSessionStorageId();
}
return;
}

// Howdju would be loaded in an iframe of the content script's window when loaded by the extension
if (event.source !== window.parent) {
logger.debug(
`Window event not from window or window.parent: ${toJson(event)}`
);
return;
}
if (!isIframedAppMessage(event)) {
// E.g. source "react-devtools-bridge"
logger.debug(
`Window message from parent must be an Iframed app message ${toJson(
event
)}`
);
return;
}
const source = event.data.source;
Expand Down

0 comments on commit b22b9fa

Please sign in to comment.