(config)
| 671 | // ───────────────────────────────────────────────────────────────────────────── |
| 672 | |
| 673 | export function startStaticServer(config) { |
| 674 | const host = config.host || DEFAULT_BIND_HOST; |
| 675 | const route = createRouter(config.routes); |
| 676 | const proxy = createProxyHandlers({ label: `static:${config.port}` }); |
| 677 | const dirAbs = resolve(config.dir); |
| 678 | const policy = applySessionKeyPolicy({ |
| 679 | host, |
| 680 | sessionApiKey: config.sessionApiKey || null, |
| 681 | authRequired: config.authRequired || false, |
| 682 | allowLanSessionKey: config.allowLanSessionKey || false, |
| 683 | }); |
| 684 | const injectionOpts = { |
| 685 | sessionApiKey: policy.sessionApiKey, |
| 686 | authRequired: policy.authRequired, |
| 687 | runtimeServicesInfo: config.runtimeServicesInfo || null, |
| 688 | lockToCloud: config.lockToCloud || null, |
| 689 | basePath: normalizeBasePath(config.basePath), |
| 690 | vscodeBasePath: config.vscodeBasePath || null, |
| 691 | disableTelemetry: config.disableTelemetry || false, |
| 692 | }; |
| 693 | const basePath = injectionOpts.basePath; |
| 694 | const rejectPrefixes = config.rejectPrefixes ?? []; |
| 695 | const noReferrerPrefixes = config.noReferrerPrefixes ?? []; |
| 696 | const staticMiddleware = createStaticMiddleware(dirAbs); |
| 697 | |
| 698 | const uninstallDiagnostics = proxy.installDiagnostics(); |
| 699 | // DigitalOcean Managed Agents for the browser: with MARS_WEB=1, host the MARS |
| 700 | // bridge and proxies to sessions' tunnel listeners, behind the session key |
| 701 | // (see mars-web-bridge.mjs). Deliberately `config.sessionApiKey`, not |
| 702 | // `policy.sessionApiKey`: the policy decides whether the key may be baked |
| 703 | // into the HTML on a LAN bind (Docker binds `::` and strips it), while the |
| 704 | // bridge only ever checks a key the client sends — the one the user types |
| 705 | // on the API key screen. A LAN bind with no key at all refuses to mount. |
| 706 | const marsWeb = mountMarsWebBridge({ |
| 707 | host, |
| 708 | sessionApiKey: config.sessionApiKey || null, |
| 709 | }); |
| 710 | |
| 711 | const server = createServer((req, res) => { |
| 712 | if (isForeignLoopbackRequest(req, host)) { |
| 713 | res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8" }); |
| 714 | res.end("Forbidden"); |
| 715 | return; |
| 716 | } |
| 717 | const url = req.url ?? "/"; |
| 718 | if (marsWeb?.handleHttp(req, res)) return; |
| 719 | const backend = route(url); |
| 720 | if (backend) { |
| 721 | // The editor is advertised as `<origin><prefix>/?tkn=<token>`, and that |
| 722 | // token is agent-server's session key. The workbench loads webviews, |
| 723 | // previews and extension content from that document, so without this a |
| 724 | // Referer carrying the key rides along on those subrequests. |
| 725 | if (matchesAnyPrefix(url, noReferrerPrefixes)) { |
| 726 | res.setHeader("Referrer-Policy", "no-referrer"); |
| 727 | } |
| 728 | if ( |
| 729 | config.runtimeServicesInfo && |
| 730 | isServerInfoRequest(req) && |
no test coverage detected