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

Fix CF cookie dateLessThan and add IP condition. #32

Merged
merged 1 commit into from
Dec 6, 2024
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
7 changes: 5 additions & 2 deletions conf/node/controllers/cloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const getDateLessThan = () => {
* @returns {import('@aws-sdk/cloudfront-signer').CloudfrontSignedCookiesOutput} cookies - The signed CloudFront cookies
*/

export const getCookies = ({ resource, dateLessThan }) => {
export const getCookies = ({ resource, dateLessThan, ipAddress }) => {
// Check if the cache has a value for the resource
const cachedValue =
cache.cookieSets?.[resource]?.dateLessThan === dateLessThan;
Expand All @@ -121,7 +121,10 @@ export const getCookies = ({ resource, dateLessThan }) => {
Resource: resource,
Condition: {
DateLessThan: {
"AWS:EpochTime": new Date(dateLessThan).getTime() / 1000, // time in seconds
"AWS:EpochTime": dateLessThan, // time in seconds
},
IpAddress: {
"AWS:SourceIp": ipAddress,
},
},
},
Expand Down
15 changes: 15 additions & 0 deletions conf/node/controllers/cloudfront.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,30 @@ describe("getCookies", () => {
it("should return cookies for CloudFront", () => {
const dateLessThan = getDateLessThan();
const resource = "https://archive.example.com/*";
const ipAddress = "127.0.0.1";

const result = getCookies({
resource,
dateLessThan,
ipAddress,
});

expect(result).toBeDefined();
expect(result["CloudFront-Key-Pair-Id"]).toBeDefined();
expect(result["CloudFront-Policy"]).toBeDefined();
expect(result["CloudFront-Signature"]).toBeDefined();

// Trim trailing underscores from result["CloudFront-Policy"]
const policyBase64 = result["CloudFront-Policy"].replace(/_*$/g, "");

const policy = JSON.parse(Buffer.from(policyBase64, "base64").toString());

const statement = policy.Statement[0];

expect(statement.Resource).toBe(resource);
expect(statement.Condition.DateLessThan["AWS:EpochTime"]).toBe(
dateLessThan,
);
expect(statement.Condition.IpAddress["AWS:SourceIp"]).toBe(ipAddress);
});
});
3 changes: 3 additions & 0 deletions conf/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ app.post("/spider", function (req, res) {

app.get("/access-archive", async function (req, res, next) {
try {
const clientIp = req.headers["x-forwarded-for"] || req.ip;

// Get the current domain from the request
const appUrl = new URL(
`${req.headers["x-forwarded-proto"] || req.protocol}://${
Expand All @@ -90,6 +92,7 @@ app.get("/access-archive", async function (req, res, next) {
const cookies = getCookies({
resource: `${cdnUrl.origin}/*`,
dateLessThan: getDateLessThan(),
clientIp,
});

// Set the cookies on the response
Expand Down
Loading