Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(libs/play): allow runner embedded on review pages #12647

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cloud-function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The function uses the following environment variables:
requests to the main site.
- `ORIGIN_LIVE_SAMPLES` (default: `"localhost"`) - The expected `Host` header
value for requests to live samples.
- `ORIGIN_REVIEW` (default: `"content.dev.mdn.mozit.cloud"`) - The host of the
content preview pages.
- `SOURCE_CONTENT` (default: `"http://localhost:8100"`) - The URL at which the
client build is served.
- `SOURCE_API` (default: `"https://developer.allizom.org/"`) - The URL at which
Expand Down
17 changes: 15 additions & 2 deletions libs/play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import he from "he";

export const ORIGIN_PLAY = process.env["ORIGIN_PLAY"] || "localhost";
export const ORIGIN_MAIN = process.env["ORIGIN_MAIN"] || "localhost";
export const ORIGIN_REVIEW =
process.env["ORIGIN_REVIEW"] || "content.dev.mdn.mozit.cloud";

/** @import { IncomingMessage, ServerResponse } from "http" */
/** @import * as express from "express" */
Expand Down Expand Up @@ -332,6 +334,18 @@ function playSubdomain(hostname) {
return "";
}

/**
* @param {URL} referer
*/
function isMDNReferer(referer) {
const { hostname } = referer;
return (
hostname === ORIGIN_MAIN ||
hostname === ORIGIN_REVIEW ||
hostname.endsWith(`.${ORIGIN_REVIEW}`)
);
}

/**
* @param {express.Request} req
* @param {express.Response} res
Expand All @@ -351,8 +365,7 @@ export async function handleRunner(req, res) {
const isLocalhost = req.hostname === "localhost";
const hasMatchingHash = playSubdomain(req.hostname) === hash;
const isIframeOnMDN =
referer.hostname === ORIGIN_MAIN &&
req.headers["sec-fetch-dest"] === "iframe";
isMDNReferer(referer) && req.headers["sec-fetch-dest"] === "iframe";

if (
!stateParam ||
Expand Down
Loading