Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlee85 committed Jan 9, 2024
1 parent de965da commit 725de66
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ async function generateSignedUrl(request, key) {
* Verifies the MAC and expiry of the given URL. If the URL is valid, the request is forwarded to the origin.
*/
async function verifyAndFetch(request, key) {
const invalidResponse = new Response('Invalid request', { status: 403 });
const invalidResponse = (reason) =>
new Response(`Invalid request - ${reason}`, { status: 403 });
const url = new URL(request.url);

if (!url.searchParams.has('mac') || !url.searchParams.has('expiry')) {
invalidResponse.body += ' - Missing MAC or expiry';
return invalidResponse;
return invalidResponse('Missing MAC or expiry');
}

const expiry = Number(url.searchParams.get('expiry'));
Expand All @@ -89,14 +89,12 @@ async function verifyAndFetch(request, key) {

// Ensure that the MAC is valid
if (hashInBase64 !== receivedMacBase64) {
invalidResponse.body += ' - Bad MAC';
return invalidResponse;
return invalidResponse('Invalid MAC');
}

// Ensure that the URL has not expired
if (Date.now() > expiry) {
invalidResponse.body += ' - Expired';
return invalidResponse;
return invalidResponse('URL has expired');
}

// Forward the remaining request path after **/verify/* to the origin
Expand Down

0 comments on commit 725de66

Please sign in to comment.