Clearwake / app · clearwake.ai / crates / clearwake-fetch / pages / clearwake-fetch.js
| 1 | // clearwake.ai/fetch: loads a page or picture for Clearwake on the web. |
| 2 | // Every address and every redirect is checked by Clearwake's own filter |
| 3 | // (Rust, compiled to clearwake_fetch.wasm) before it is requested. Only |
| 4 | // pages, text and pictures come back. No cookies are sent or kept. |
| 5 | import filterModule from './clearwake_fetch.wasm'; |
| 6 | |
| 7 | const MAX_BYTES = 10 * 1024 * 1024; |
| 8 | const USER_AGENT = 'Mozilla/5.0 (compatible; Clearwake/0.1; +https://clearwake.ai)'; |
| 9 | const ORIGINS = ['https://clearwake.ai', 'https://www.clearwake.ai']; |
| 10 | const enc = new TextEncoder(); |
| 11 | |
| 12 | function check(url) { |
| 13 | const f = new WebAssembly.Instance(filterModule, {}).exports; |
| 14 | const b = enc.encode(url); |
| 15 | const p = f.cw_alloc(b.length); |
| 16 | new Uint8Array(f.memory.buffer, p, b.length).set(b); |
| 17 | return f.cw_check(p, b.length); |
| 18 | } |
| 19 | |
| 20 | function reply(status, text, extra = {}) { |
| 21 | return new Response(text, { |
| 22 | status, |
| 23 | headers: { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', ...extra }, |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | export async function clearwakeFetch(request) { |
| 28 | const origin = request.headers.get('origin') || ''; |
| 29 | const site = request.headers.get('sec-fetch-site') || ''; |
| 30 | if (!(site === 'same-origin' || ORIGINS.includes(origin))) return reply(403, 'This loader only works inside Clearwake.'); |
| 31 | if (request.method !== 'GET') return reply(405, 'Only pages can be loaded.'); |
| 32 | let target = new URL(request.url).searchParams.get('url') || ''; |
| 33 | for (let hop = 0; hop < 8; hop++) { |
| 34 | const verdict = check(target); |
| 35 | if (verdict === 1) return reply(403, 'Clearwake blocked this address because it serves ads or tracking.'); |
| 36 | if (verdict === 2) return reply(400, "That address doesn't look right."); |
| 37 | if (verdict === 3) return reply(400, 'Clearwake on the web can only open public sites.'); |
| 38 | let res; |
| 39 | try { |
| 40 | res = await fetch(target, { |
| 41 | redirect: 'manual', |
| 42 | headers: { 'user-agent': USER_AGENT, accept: 'text/html,application/xhtml+xml,text/plain;q=0.9,image/*;q=0.8', 'accept-language': 'en-US,en;q=0.8' }, |
| 43 | }); |
| 44 | } catch { |
| 45 | return reply(502, "Clearwake couldn't reach that site."); |
| 46 | } |
| 47 | if (res.status >= 300 && res.status < 400) { |
| 48 | const loc = res.headers.get('location'); |
| 49 | if (!loc) return reply(502, 'The site sent Clearwake somewhere it won\'t go.'); |
| 50 | target = new URL(loc, target).toString(); |
| 51 | continue; |
| 52 | } |
| 53 | if (res.status >= 400) return reply(502, `The site answered with an error (${res.status}).`); |
| 54 | const ct = res.headers.get('content-type') || ''; |
| 55 | const l = ct.toLowerCase(); |
| 56 | if (!(l === '' || l.includes('html') || l.includes('xml') || l.startsWith('text/') || l.startsWith('image/') || l.includes('json'))) { |
| 57 | return reply(415, "This is a file, not a page. Clearwake doesn't download files."); |
| 58 | } |
| 59 | const body = await res.arrayBuffer(); |
| 60 | if (body.byteLength > MAX_BYTES) return reply(413, 'This page is too large to open.'); |
| 61 | return new Response(body, { |
| 62 | status: 200, |
| 63 | headers: { 'content-type': ct || 'application/octet-stream', 'x-clearwake-url': target, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff' }, |
| 64 | }); |
| 65 | } |
| 66 | return reply(508, 'The site redirected too many times.'); |
| 67 | } |