|
| 1 | +export default { |
| 2 | + async fetch(request, env, ctx) { |
| 3 | + const url = new URL(request.url); |
| 4 | + const policyPath = '/.well-known/mta-sts.txt'; |
| 5 | + |
| 6 | + // recommended MAX_AGEs: |
| 7 | + // 1 week: 604800 (good for testing) |
| 8 | + // 1 month: 2628000 (good for prod?) |
| 9 | + // 1 year: 31557600 (you cannot go higher as defined in RFC 8461) |
| 10 | + |
| 11 | + // max_age: non-negative integer, max 31,557,600 (RFC 8461 §3.2) |
| 12 | + const MAX_AGE_MIN = 0; // if MAX_AGE < MAX_AGE_MIN then we use MAX_AGE_DEFAULT |
| 13 | + const MAX_AGE_DEFAULT = 60; // only used until you set environment variable MAX_AGE |
| 14 | + const MAX_AGE_MAX = 31_557_600; // if MAX_AGE > MAX_AGE_MAX then we use MAX_AGE_MAX |
| 15 | + |
| 16 | + const rawMaxAge = String(env.MAX_AGE ?? '').trim(); |
| 17 | + const parsedMaxAge = rawMaxAge === '' ? NaN : parseInt(rawMaxAge, 10); |
| 18 | + const maxAge = !Number.isInteger(parsedMaxAge) || parsedMaxAge < 0 |
| 19 | + ? MAX_AGE_DEFAULT // RFC: Non-negative integer required |
| 20 | + : parsedMaxAge < MAX_AGE_MIN |
| 21 | + ? MAX_AGE_DEFAULT // Your choice: reset to default if too low |
| 22 | + : parsedMaxAge > MAX_AGE_MAX |
| 23 | + ? MAX_AGE_MAX // RFC: Usually 1 year (31557600) |
| 24 | + : parsedMaxAge; |
| 25 | + |
| 26 | + const defaultHeaders = { |
| 27 | + 'Allow': 'GET, HEAD', |
| 28 | + 'Content-Type': 'text/plain; charset=utf-8', |
| 29 | + // Optional |
| 30 | + 'Strict-Transport-Security': `max-age=${maxAge}`, |
| 31 | + }; |
| 32 | + |
| 33 | + // Only GET/HEAD are allowed |
| 34 | + const method = request.method.toUpperCase(); |
| 35 | + if (method !== 'GET' && method !== 'HEAD') { |
| 36 | + const h = new Headers({ ...defaultHeaders, 'Allow': 'GET, HEAD' }); |
| 37 | + return new Response('Method Not Allowed', { status: 405, headers: h }); |
| 38 | + } |
| 39 | + |
| 40 | + // Redirect everything that's not the canonical path to the policy URL |
| 41 | + // Dedicated hostname => cache redirect aggressively |
| 42 | + if (url.pathname !== policyPath) { |
| 43 | + const target = new URL(policyPath, url.origin).toString(); |
| 44 | + const redirectHeaders = new Headers({ |
| 45 | + ...defaultHeaders, |
| 46 | + 'Location': target, |
| 47 | + 'Cache-Control': `public, max-age=${maxAge}, immutable`, |
| 48 | + }); |
| 49 | + return new Response(null, { status: 308, headers: redirectHeaders }); |
| 50 | + } |
| 51 | + |
| 52 | + // mode: enforce | testing | none |
| 53 | + const allowedModes = new Set(['enforce', 'testing', 'none']); |
| 54 | + const modeRaw = (env.MODE || 'none').toLowerCase(); |
| 55 | + const mode = allowedModes.has(modeRaw) ? modeRaw : 'none'; |
| 56 | + |
| 57 | + // mx: only applicable for enforce/testing |
| 58 | + const mxLines = |
| 59 | + mode !== 'none' |
| 60 | + ? String(env.MX_HOSTS || '') |
| 61 | + .split(/[,\s]+/) |
| 62 | + .map((s) => s.trim()) |
| 63 | + .filter(Boolean) |
| 64 | + .map((mx) => `mx: ${mx}`) |
| 65 | + : []; |
| 66 | + |
| 67 | + // Enforce RFC rule: mx required when mode is enforce/testing |
| 68 | + if ((mode === 'enforce' || mode === 'testing') && mxLines.length === 0) { |
| 69 | + const h = new Headers({ ...defaultHeaders }); |
| 70 | + return new Response( |
| 71 | + 'Misconfigured Worker: MX_HOSTS required when MODE is "enforce" or "testing".', |
| 72 | + { status: 500, headers: h } |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // For mode "none", omit mx lines in the policy output |
| 77 | + const lines = [ |
| 78 | + 'version: STSv1', |
| 79 | + `mode: ${mode}`, |
| 80 | + ...mxLines, |
| 81 | + `max_age: ${maxAge}`, |
| 82 | + ]; |
| 83 | + const body = lines.join('\n') + '\n'; // add newline at end of file |
| 84 | + const headers = new Headers(defaultHeaders); |
| 85 | + |
| 86 | + if (method == 'HEAD') { |
| 87 | + return new Response(null, { status: 200, headers }); |
| 88 | + } |
| 89 | + |
| 90 | + return new Response(body, { status: 200, headers }); |
| 91 | + }, |
| 92 | +}; |
0 commit comments