Skip to content

Commit

Permalink
fix: better errors for expirations (#519)
Browse files Browse the repository at this point in the history
* improve error handling for file expiry

* add missing semicolons

---------

Co-authored-by: dicedtomato <35403473+diced@users.noreply.github.com>
  • Loading branch information
Wingysam and diced committed Dec 24, 2023
1 parent 265760f commit 0ab814f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/lib/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ export function humanTime(string: StringValue | string): Date {
}
}

export function parseExpiry(header: string): Date | null {
if (!header) return null;
export function parseExpiry(header: string): Date {
if (!header) throw new Error('no expiry provided');
header = header.toLowerCase();

if (header.startsWith('date=')) {
const date = new Date(header.substring(5));

if (!date.getTime()) return null;
if (date.getTime() < Date.now()) return null;
if (!date.getTime()) throw new Error('invalid date');
if (date.getTime() < Date.now()) throw new Error('expiry must be in the future');
return date;
}

const human = humanTime(header);

if (!human) return null;
if (human.getTime() < Date.now()) return null;
if (!human) throw new Error('failed to parse human time');
if (human.getTime() < Date.now()) throw new Error('expiry must be in the future');

return human;
}
Expand Down
8 changes: 6 additions & 2 deletions src/pages/api/auth/invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
count: number;
};

const expiry = parseExpiry(expiresAt);
if (!expiry) return res.badRequest('invalid date');
let expiry: Date;
try {
expiry = parseExpiry(expiresAt);
} catch (error) {
return res.badRequest(error.message);
}
const counts = count ? count : 1;

if (counts > 1) {
Expand Down
14 changes: 9 additions & 5 deletions src/pages/api/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ async function handler(req: NextApiReq, res: NextApiRes) {
let expiry: Date;

if (expiresAt) {
expiry = parseExpiry(expiresAt);
if (!expiry) return res.badRequest('invalid date');
else {
try {
expiry = parseExpiry(expiresAt);
response.expiresAt = expiry;
} catch (error) {
return res.badRequest(error.message);
}
}

if (zconfig.uploader.default_expiration) {
expiry = parseExpiry(zconfig.uploader.default_expiration);
if (!expiry) return res.badRequest('invalid date (UPLOADER_DEFAULT_EXPIRATION)');
try {
expiry = parseExpiry(zconfig.uploader.default_expiration);
} catch (error) {
return res.badRequest(`${error.message} (UPLOADER_DEFAULT_EXPIRATION)`);
}
}

const rawFormat = ((req.headers['format'] as string) || zconfig.uploader.default_format).toLowerCase();
Expand Down

0 comments on commit 0ab814f

Please sign in to comment.