Skip to content

Commit

Permalink
enhance(play): add logging + distinguish runner responses
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Feb 21, 2025
1 parent 7fa3147 commit 2bc2ca7
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions libs/play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,22 +346,39 @@ export async function handleRunner(req, res) {
"https://example.com"
);
const stateParam = url.searchParams.get("state");

if (!stateParam) {
console.warn("[runner] Missing state parameter");
return res.status(400).end();
}

const { state, hash } = await decompressFromBase64(stateParam);

const isLocalhost = req.hostname === "localhost";
const hasMatchingHash = playSubdomain(req.hostname) === hash;
const isIframeOnMDN =
referer.hostname === ORIGIN_MAIN &&
req.headers["sec-fetch-dest"] === "iframe";

if (
!stateParam ||
!state ||
(!isLocalhost && !hasMatchingHash && !isIframeOnMDN)
) {
if (!state) {
console.warn("[runner] Invalid state parameter");
return res.status(404).end();
}

if (req.hostname !== "localhost") {
const expectedHash = playSubdomain(req.hostname);

if (expectedHash !== hash) {
console.warn(
`[runner] Hash mismatch: ${JSON.stringify({ expectedHash, hash })}`
);

const isOnMDN = referer.hostname === ORIGIN_MAIN;
const isIframe = req.headers["sec-fetch-dest"] === "iframe";

if (!isOnMDN || !isIframe) {
console.warn(
`[runner] No iframe on MDN: ${JSON.stringify({ isOnMDN, isIframe })}`
);
return res.status(403).end();
}
}
}

const json = JSON.parse(state);
const codeParam = url.searchParams.get("code");
const codeCookie = req.cookies["code"];
Expand Down

0 comments on commit 2bc2ca7

Please sign in to comment.