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: handle invalid dates properly #266

Merged
merged 6 commits into from
Jan 28, 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
23 changes: 13 additions & 10 deletions src/utils/request.ts
flakey5 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ export function parseUrl(request: Request): URL | undefined {
}

export function parseConditionalHeaders(headers: Headers): ConditionalHeaders {
flakey5 marked this conversation as resolved.
Show resolved Hide resolved
const ifModifiedSince = headers.has('if-modified-since')
? new Date(headers.get('if-modified-since')!)
: undefined;

if (ifModifiedSince instanceof Date) {
ifModifiedSince.setSeconds(ifModifiedSince.getSeconds() + 1);
}
const ifModifiedSince = getDateFromHeader(headers.get('if-modified-since'));

const ifMatch = headers.has('if-match')
? headers.get('if-match')!.replaceAll('"', '')
Expand All @@ -46,9 +40,9 @@ export function parseConditionalHeaders(headers: Headers): ConditionalHeaders {
? headers.get('if-none-match')!.replaceAll('"', '')
: undefined;

const ifUnmodifiedSince = headers.has('if-unmodified-since')
? new Date(headers.get('if-unmodified-since')!)
: undefined;
const ifUnmodifiedSince = getDateFromHeader(
headers.get('if-unmodified-since')
);

const range = headers.has('range')
? parseRangeHeader(headers.get('range')!)
Expand All @@ -63,6 +57,15 @@ export function parseConditionalHeaders(headers: Headers): ConditionalHeaders {
};
}

function getDateFromHeader(dateString: string | null): Date | undefined {
if (dateString === null) {
return undefined;
}

const date = new Date(dateString);
return !isNaN(date.getTime()) ? date : undefined;
}

/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
* @returns undefined if header is invalid
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ describe('File Tests', () => {
assert.strictEqual(res.status, 200);
assert.strictEqual(res.headers.has('last-modified'), true);

const date = new Date(res.headers.get('last-modified')!);
date.setSeconds(date.getSeconds() + 1);

// Make sure it returns a 304 when If-Modified-Since
// >= file last modified
res = await mf.dispatchFetch(`${url}dist/index.json`, {
headers: {
'if-modified-since': res.headers.get('last-modified')!,
'if-modified-since': date.toUTCString(),
},
});
assert.strictEqual(res.status, 304);
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/utils/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { parseRangeHeader } from '../../../src/utils/request';
import {
parseConditionalHeaders,
parseRangeHeader,
} from '../../../src/utils/request';

describe('parseRangeHeader', () => {
it('`bytes=0-10`', () => {
Expand Down Expand Up @@ -49,3 +52,20 @@ describe('parseRangeHeader', () => {
assert.strictEqual(result, undefined);
});
});

describe('parseConditionalHeaders', () => {
it('invalid dates', () => {
const headers = new Headers({
'if-modified-since': 'asd',
'if-unmodified-since': 'asd',
});

assert.deepStrictEqual(parseConditionalHeaders(headers), {
ifMatch: undefined,
ifNoneMatch: undefined,
ifModifiedSince: undefined,
ifUnmodifiedSince: undefined,
range: undefined,
});
});
});
Loading