fix: Add CORP and COEP headers to embedded pages (#15275)
* Add CORP headers * Improve test * Add unit tests as well --------- Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
1 parent
6e66f7f8f8
commit
37caa57798
12 files changed
+302
-21
No files matched your search
@@ -14,7 +14,10 @@ export default function withEmbedSsrAppDir<T extends Record<string, any>>(
|
||||
) {
|
||||
return async (context: GetServerSidePropsContext): Promise<T> => {
|
||||
const { embed, layout } = context.query;
|
||||
|
||||
const isCOEPEnabled = context.query["flag.coep"] === "true";
|
||||
if (isCOEPEnabled) {
|
||||
context.res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
|
||||
}
|
||||
try {
|
||||
const props = await getData(context);
|
||||
|
||||
|
||||
@@ -133,6 +133,24 @@ describe("withEmbedSsr", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should add Cross-Origin-Embedder-Policy header when "flag.coep" query param is true', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
redirectUrl: "/reschedule",
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
"flag.coep": "true",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBe("require-corp");
|
||||
});
|
||||
});
|
||||
|
||||
describe("when redirect destination is absolute, should add /embed to end of the path", () => {
|
||||
@@ -206,6 +224,23 @@ describe("withEmbedSsr", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
it('should add Cross-Origin-Embedder-Policy header when "flag.coep" query param is true', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
redirectUrl: "https://calcom.cal.local/owner",
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
"flag.coep": "true",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBe("require-corp");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -233,6 +268,85 @@ describe("withEmbedSsr", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should add Cross-Origin-Embedder-Policy header when "flag.coep" query param is true', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
props: {
|
||||
prop1: "value1",
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
"flag.coep": "true",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBe("require-corp");
|
||||
});
|
||||
|
||||
it('should not add Cross-Origin-Embedder-Policy header when "flag.coep" query param is false', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
props: {
|
||||
prop1: "value1",
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
"flag.coep": "false",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not add Cross-Origin-Embedder-Policy header when "flag.coep" query param is not set', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
props: {
|
||||
prop1: "value1",
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should not add Cross-Origin-Embedder-Policy header when "flag.coep" query param is set to something other than true', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
props: {
|
||||
prop1: "value1",
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
"flag.coep": "abasdf",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("when gSSP doesn't have props or redirect ", () => {
|
||||
@@ -254,5 +368,23 @@ describe("withEmbedSsr", () => {
|
||||
|
||||
expect(ret).toEqual({ notFound: true });
|
||||
});
|
||||
|
||||
it('should add Cross-Origin-Embedder-Policy header when "flag.coep" query param is true', async () => {
|
||||
const withEmbedGetSsr = withEmbedSsr(
|
||||
getServerSidePropsFnGenerator({
|
||||
notFound: true,
|
||||
})
|
||||
);
|
||||
|
||||
const context = getServerSidePropsContextArg({
|
||||
embedRelatedParams: {
|
||||
layout: "week_view",
|
||||
embed: "",
|
||||
"flag.coep": "true",
|
||||
},
|
||||
});
|
||||
await withEmbedGetSsr(context);
|
||||
expect(context.res.getHeader("Cross-Origin-Embedder-Policy")).toBe("require-corp");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,10 @@ export default function withEmbedSsr(getServerSideProps: GetServerSideProps) {
|
||||
const ssrResponse = await getServerSideProps(context);
|
||||
const embed = context.query.embed;
|
||||
const layout = context.query.layout;
|
||||
const isCOEPEnabled = context.query["flag.coep"] === "true";
|
||||
if (isCOEPEnabled) {
|
||||
context.res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
|
||||
}
|
||||
|
||||
if ("redirect" in ssrResponse) {
|
||||
const destinationUrl = ssrResponse.redirect.destination;
|
||||
@@ -44,6 +48,7 @@ export default function withEmbedSsr(getServerSideProps: GetServerSideProps) {
|
||||
if (!("props" in ssrResponse)) {
|
||||
return ssrResponse;
|
||||
}
|
||||
|
||||
return {
|
||||
...ssrResponse,
|
||||
props: {
|
||||
|
||||
@@ -387,6 +387,13 @@ const nextConfig = {
|
||||
};
|
||||
},
|
||||
async headers() {
|
||||
// This header can be set safely as it ensures the browser will load the resources even when COEP is set.
|
||||
// But this header must be set only on those resources that are safe to be loaded in a cross-origin context e.g. all embeddable pages's resources
|
||||
const CORP_CROSS_ORIGIN_HEADER = {
|
||||
key: "Cross-Origin-Resource-Policy",
|
||||
value: "cross-origin",
|
||||
};
|
||||
|
||||
return [
|
||||
{
|
||||
source: "/auth/:path*",
|
||||
@@ -419,6 +426,26 @@ const nextConfig = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
source: "/embed/embed.js",
|
||||
headers: [CORP_CROSS_ORIGIN_HEADER],
|
||||
},
|
||||
{
|
||||
source: "/:path*/embed",
|
||||
// COEP require-corp header is set conditionally when flag.coep is set to true
|
||||
headers: [CORP_CROSS_ORIGIN_HEADER],
|
||||
},
|
||||
// These resources loads through embed as well, so they need to have CORP_CROSS_ORIGIN_HEADER
|
||||
...[
|
||||
{
|
||||
source: "/api/avatar/:path*",
|
||||
headers: [CORP_CROSS_ORIGIN_HEADER],
|
||||
},
|
||||
{
|
||||
source: "/avatar.svg",
|
||||
headers: [CORP_CROSS_ORIGIN_HEADER],
|
||||
},
|
||||
],
|
||||
...(isOrganizationsEnabled
|
||||
? [
|
||||
{
|
||||
|
||||
@@ -20,7 +20,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||
const { uid: bookingUid, seatReferenceUid } = z
|
||||
.object({ uid: z.string(), seatReferenceUid: z.string().optional() })
|
||||
.parse(context.query);
|
||||
|
||||
const coepFlag = context.query["flag.coep"];
|
||||
const { uid, seatReferenceUid: maybeSeatReferenceUid } = await maybeGetBookingUidFromSeat(
|
||||
prisma,
|
||||
bookingUid
|
||||
@@ -136,6 +136,10 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||
|
||||
destinationUrl.set("rescheduleUid", seatReferenceUid || bookingUid);
|
||||
|
||||
// TODO: I think we should just forward all the query params here including coep flag
|
||||
if (coepFlag) {
|
||||
destinationUrl.set("flag.coep", coepFlag as string);
|
||||
}
|
||||
return {
|
||||
redirect: {
|
||||
destination: `/${eventPage}?${destinationUrl.toString()}${
|
||||
|
||||
Reference in new issue
Block a user