|
| 1 | +import { URL } from 'whatwg-url'; |
| 2 | +import HmacSHA1 from 'crypto-js/hmac-sha1'; |
| 3 | +import Base64 from 'crypto-js/enc-base64'; |
| 4 | +import createFetchForOrigin from '../../../utils/createFetchForOrigin'; |
| 5 | + |
| 6 | +const fetch = createFetchForOrigin('echo', { |
| 7 | + edgio: { |
| 8 | + caching: { |
| 9 | + bypass_cache: true, |
| 10 | + }, |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +export async function handleHttpRequest(request, context) { |
| 15 | + // ** IMPORTANT ** |
| 16 | + // Secret key should be defined as an environment variable in the Edgio console |
| 17 | + const secretKey = '$0m3th!ngS3cr3t'; // context.environmentVars.REQ_SIGNING_SECRET_KEY; |
| 18 | + |
| 19 | + if (request.url.includes('/sign/')) { |
| 20 | + return generateSignedUrl(request, secretKey); |
| 21 | + } |
| 22 | + |
| 23 | + return verifyAndFetch(request, secretKey); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Generates a signed URL for the given URL and secret key |
| 28 | + * @param {URL} url |
| 29 | + * @param {string} secretKey |
| 30 | + */ |
| 31 | +async function generateSignedUrl(request, key) { |
| 32 | + const url = new URL(request.url); |
| 33 | + |
| 34 | + // Replace /sign/ with /verify/ in the URL since we are generating a signed URL for verification |
| 35 | + url.pathname = url.pathname.replace('/sign/', '/verify/'); |
| 36 | + |
| 37 | + const expirationMs = 1000 * 60 * 5; // 5 minutes |
| 38 | + const expiry = Date.now() + expirationMs; |
| 39 | + const dataToAuthenticate = url.pathname + expiry; |
| 40 | + |
| 41 | + const hash = HmacSHA1(dataToAuthenticate, key); |
| 42 | + const base64Mac = Base64.stringify(hash); |
| 43 | + |
| 44 | + url.searchParams.set('mac', base64Mac); |
| 45 | + url.searchParams.set('expiry', expiry.toString()); |
| 46 | + |
| 47 | + const validUrl = url.toString(); |
| 48 | + const modifiedExpiryUrl = new URL(validUrl); |
| 49 | + modifiedExpiryUrl.searchParams.set('expiry', `${expiry + 5}`); |
| 50 | + const modifiedMacUrl = new URL(validUrl); |
| 51 | + modifiedMacUrl.searchParams.set('mac', `${base64Mac}x`); |
| 52 | + |
| 53 | + console.log('Valid URL:\n', validUrl); |
| 54 | + console.log('Modified expiry URL:\n', modifiedExpiryUrl.toString()); |
| 55 | + console.log('Modified MAC URL:\n', modifiedMacUrl.toString()); |
| 56 | + |
| 57 | + const htmlResponse = ` |
| 58 | + <html> |
| 59 | + <body> |
| 60 | + <p>Click the following links for verification:</p> |
| 61 | + <ul> |
| 62 | + <li><a href="${validUrl}">Valid URL</a><pre>(${validUrl})</pre></li> |
| 63 | + <li><a href="${modifiedExpiryUrl}">Invalid with modified Expiry URL</a><pre>(${modifiedExpiryUrl})</pre></li> |
| 64 | + <li><a href="${modifiedMacUrl}">Invalid with modified Mac URL</a><pre>(${modifiedMacUrl})</pre></li> |
| 65 | + </ul> |
| 66 | + </body> |
| 67 | + </html> |
| 68 | + `; |
| 69 | + |
| 70 | + return new Response(htmlResponse, { |
| 71 | + headers: { 'Content-Type': 'text/html' }, |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Verifies the MAC and expiry of the given URL. If the URL is valid, the request is forwarded to the origin. |
| 77 | + */ |
| 78 | +async function verifyAndFetch(request, key) { |
| 79 | + const invalidResponse = (reason) => |
| 80 | + new Response(`Invalid request - ${reason}`, { status: 403 }); |
| 81 | + const url = new URL(request.url); |
| 82 | + |
| 83 | + if (!url.searchParams.has('mac') || !url.searchParams.has('expiry')) { |
| 84 | + return invalidResponse('Missing MAC or expiry'); |
| 85 | + } |
| 86 | + |
| 87 | + const expiry = Number(url.searchParams.get('expiry')); |
| 88 | + const dataToAuthenticate = url.pathname + expiry; |
| 89 | + |
| 90 | + const receivedMacBase64 = url.searchParams.get('mac'); |
| 91 | + const receivedMac = Base64.parse(receivedMacBase64); |
| 92 | + |
| 93 | + const hash = HmacSHA1(dataToAuthenticate, key); |
| 94 | + const hashInBase64 = Base64.stringify(hash); |
| 95 | + |
| 96 | + // Ensure that the MAC is valid |
| 97 | + if (hashInBase64 !== receivedMacBase64) { |
| 98 | + return invalidResponse('Invalid MAC'); |
| 99 | + } |
| 100 | + |
| 101 | + // Ensure that the URL has not expired |
| 102 | + if (Date.now() > expiry) { |
| 103 | + return invalidResponse('URL has expired'); |
| 104 | + } |
| 105 | + |
| 106 | + // Forward the remaining request path after **/verify/* to the origin |
| 107 | + url.pathname = url.pathname.split('/verify/')[1]; |
| 108 | + |
| 109 | + return fetch(url.toString()); |
| 110 | +} |
0 commit comments