Skip to content

Commit dcc15f7

Browse files
committed
prevent dev server fetching from unknown origins
1 parent bf70f87 commit dcc15f7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

dotcom-rendering/src/server/lib/get-content-from-url.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import type { Handler } from 'express';
55
const isStringTuple = (_: [string, unknown]): _ is [string, string] =>
66
typeof _[1] === 'string';
77

8+
const allowedOrigins = [
9+
'https://www.theguardian.com',
10+
'https://m.code.dev-theguardian.com',
11+
'https://m.thegulocal.com',
12+
'http://localhost:9000',
13+
];
14+
815
/**
916
* Get DCR content from a `theguardian.com` URL.
1017
* Takes in optional `X-Gu-*` headers to send.
@@ -17,6 +24,17 @@ async function getContentFromURL(
1724
// searchParams will only work for the first set of query params because 'url' is already a query param itself
1825
const searchparams = url.searchParams.toString();
1926

27+
// Prevent requests to unknown origins
28+
if (!allowedOrigins.includes(url.origin)) {
29+
throw new Error(
30+
`Origin ${
31+
url.origin
32+
} is not allowed. Allowed origins are: ${allowedOrigins.join(
33+
', ',
34+
)}`,
35+
);
36+
}
37+
2038
// Reconstruct the parsed url adding .json?dcr which we need to force dcr to return json
2139
const jsonUrl = `${url.origin}${url.pathname}.json?dcr=true&${searchparams}`;
2240

0 commit comments

Comments
 (0)