diff --git a/src/app.ts b/src/app.ts index 32589ac7..6df5b94a 100644 --- a/src/app.ts +++ b/src/app.ts @@ -83,14 +83,12 @@ if (config.ARNS_ROOT_HOST !== undefined) { app.use( createArnsMiddleware({ dataHandler, - rootHost: config.ARNS_ROOT_HOST, nameResolver: system.nameResolver, }), ); app.use( createSandboxMiddleware({ - rootHost: config.ARNS_ROOT_HOST, sandboxProtocol: config.SANDBOX_PROTOCOL, }), ); diff --git a/src/config.ts b/src/config.ts index 6c3fed52..bf27aab2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -70,6 +70,8 @@ export const ANS104_INDEX_FILTER = createFilter( JSON.parse(ANS104_INDEX_FILTER_STRING), ); export const ARNS_ROOT_HOST = env.varOrUndefined('ARNS_ROOT_HOST'); +export const ROOT_HOST_SUBDOMAIN_LENGTH = + ARNS_ROOT_HOST !== undefined ? ARNS_ROOT_HOST.split('.').length - 2 : 0; export const SANDBOX_PROTOCOL = env.varOrUndefined('SANDBOX_PROTOCOL'); export const START_WRITERS = env.varOrDefault('START_WRITERS', 'true') === 'true'; diff --git a/src/middleware/arns.ts b/src/middleware/arns.ts index 2483f61b..d537bba9 100644 --- a/src/middleware/arns.ts +++ b/src/middleware/arns.ts @@ -18,6 +18,7 @@ import { Handler } from 'express'; import { asyncMiddleware } from 'middleware-async'; +import * as config from '../config.js'; import { sendNotFound } from '../routes/data.js'; import { NameResolver } from '../types.js'; @@ -25,24 +26,21 @@ const EXCLUDED_SUBDOMAINS = new Set('www'); export const createArnsMiddleware = ({ dataHandler, - rootHost, nameResolver, }: { dataHandler: Handler; - rootHost: string; nameResolver: NameResolver; }): Handler => asyncMiddleware(async (req, res, next) => { - const rootHostSubdomainLength = rootHost.split('.').length - 2; if ( // Ignore subdomains that are part of the ArNS root hostname. !Array.isArray(req.subdomains) || - req.subdomains.length === rootHostSubdomainLength + req.subdomains.length === config.ROOT_HOST_SUBDOMAIN_LENGTH ) { next(); return; } - const arnsSubdomain = req.subdomains[req.subdomains.length - 1]; + const arnsSubdomain = req.subdomains[config.ROOT_HOST_SUBDOMAIN_LENGTH - 1]; if ( EXCLUDED_SUBDOMAINS.has(arnsSubdomain) || // Avoid collisions with sandbox URLs by ensuring the subdomain length diff --git a/src/middleware/sandbox.ts b/src/middleware/sandbox.ts index ab6604e9..41f0cc53 100644 --- a/src/middleware/sandbox.ts +++ b/src/middleware/sandbox.ts @@ -19,11 +19,11 @@ import { Handler, Request } from 'express'; import url from 'node:url'; import { base32 } from 'rfc4648'; +import * as config from '../config.js'; import { fromB64Url } from '../lib/encoding.js'; -function getRequestSandbox(req: Request, rootHost: string): string | undefined { - const rootHostSubdomainLength = rootHost.split('.').length - 2; - if (req.subdomains.length > rootHostSubdomainLength) { +function getRequestSandbox(req: Request): string | undefined { + if (req.subdomains.length > config.ROOT_HOST_SUBDOMAIN_LENGTH) { return req.subdomains[req.subdomains.length - 1]; } return undefined; @@ -38,14 +38,12 @@ function sandboxFromId(id: string): string { } export function createSandboxMiddleware({ - rootHost, sandboxProtocol, }: { - rootHost?: string; sandboxProtocol?: string; }): Handler { return (req, res, next) => { - if (rootHost === undefined) { + if (config.ARNS_ROOT_HOST === undefined) { next(); return; } @@ -56,7 +54,7 @@ export function createSandboxMiddleware({ return; } - const reqSandbox = getRequestSandbox(req, rootHost); + const reqSandbox = getRequestSandbox(req); const idSandbox = sandboxFromId(id); if (reqSandbox !== idSandbox) { const queryString = url.parse(req.originalUrl).query ?? ''; @@ -64,7 +62,7 @@ export function createSandboxMiddleware({ const protocol = sandboxProtocol ?? (req.secure ? 'https' : 'http'); return res.redirect( 302, - `${protocol}://${idSandbox}.${rootHost}${path}?${queryString}`, + `${protocol}://${idSandbox}.${config.ARNS_ROOT_HOST}${path}?${queryString}`, ); }