Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions README.md

Large diffs are not rendered by default.

215 changes: 90 additions & 125 deletions cf_ctreq.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,31 @@
// Certificate.
// - Each certificate's issuer name must be byte-identical to the subject name of
// the next certificate in the chain. Signatures are not verified.
// - The chain must lead to one of the log's accepted roots, as listed by the origin's
// get-roots endpoint: the issuer name of one of its certificates (or the subject
// name of the last) must have the same hash as a root's subject name. The list is
// cached for CONFIG.rootsCacheSeconds; if it can't be fetched or read, this check is
// skipped.
// - The chain must lead to one of the log's accepted roots, if CONFIG.logEnvironments
// gives the log a roots URL (on another hostname, listing the hashes of the roots'
// subject names, as generated by tools/roots-hashes.mjs): the issuer name of one of its
// certificates (or the subject name of the last) must have the same hash as a root's.
// The list is cached for CONFIG.rootsCacheSeconds; if it can't be fetched or read, this
// check is skipped.
// CONFIG.environment selects whose criteria apply: "sunlight", "tesseract", or
// "trillian" rejects what that origin would reject, and "common" only rejects what
// all of them would reject. CONFIG.logEnvironments can select a different one for
// particular logs, identified by their hostnames and paths.
// all of them would reject. CONFIG.logEnvironments can select a different one (and a
// roots URL) for particular logs, identified by their hostnames and paths.
// Requests that pass these checks are forwarded to the origin unchanged.

const CONFIG = {
// Default environment: "sunlight", "tesseract", "trillian", or "common" (only
// reject what all of them would reject).
// reject what all of them would reject). The accepted roots aren't checked for logs
// that use it.
environment: "common",
// Environments for particular logs, as [regular expression, environment] pairs: a
// request uses the environment of the first pair whose regular expression matches
// its hostname followed by its path (e.g. "tiger2027h1.ct.sectigo.com/ct/v1/add-chain"),
// or else the default environment.
// Environments for particular logs, as [regular expression, environment, roots URL]: a
// request uses the environment of the first entry whose regular expression matches its
// hostname followed by its path (e.g. "tiger2027h1.ct.sectigo.com/ct/v1/add-chain"), or
// else the default environment. The optional roots URL lists the hashes of the log's
// accepted roots' subject names (see ROOTS_HEADER; tools/roots-hashes.mjs generates it
// from the log's get-roots response or a PEM bundle), and must be on another hostname, as
// Cloudflare fails a Snippet that makes more than one request to the origin (error 1201);
// the accepted roots aren't checked if it's omitted, "" or null.
logEnvironments: [
// Sectigo Static CT logs.
[/^(monument|statue)\d{4}h[12]\.sub\.ct\.sectigo\.com\//, "tesseract"],
Expand All @@ -60,7 +66,7 @@ const CONFIG = {
// without being checked, so as not to spend the Snippet's limited CPU time on them.
maxChainLength: 10,
minCertBytes: 128,
// How long each log's accepted roots (from its get-roots endpoint) are cached, both in
// How long each log's accepted roots (from its roots URL) are cached, both in
// Cloudflare's cache and in the snippet's memory.
rootsCacheSeconds: 60,
// Prefix of the problem details "type" URI; the rejection reason code is appended.
Expand Down Expand Up @@ -88,15 +94,30 @@ const ENVIRONMENTS = {
trillian: [ORIGINS.trillian],
common: Object.values(ORIGINS),
};
// Resolved when the snippet is loaded, so that a wrong environment fails at
// deployment rather than on every request.
const DEFAULT_ORIGINS = originsFor(CONFIG.environment, "CONFIG.environment");
const LOG_ORIGINS = CONFIG.logEnvironments.map((pair, i) => {
// Resolved when the snippet is loaded, so that a wrong setting fails at deployment
// rather than on every request.
const DEFAULT_LOG = { origins: originsFor(CONFIG.environment, "CONFIG.environment"), rootsUrl: null };
const LOG_SETTINGS = CONFIG.logEnvironments.map((entry, i) => {
const setting = `CONFIG.logEnvironments[${i}]`;
if (!Array.isArray(pair) || pair.length !== 2 || !(pair[0] instanceof RegExp)) {
throw new Error(`${setting} must be a [regular expression, environment] pair`);
if (!Array.isArray(entry) || entry.length < 2 || entry.length > 3 || !(entry[0] instanceof RegExp)) {
throw new Error(`${setting} must be [regular expression, environment] or [regular expression, environment, roots URL]`);
}
return [pair[0], originsFor(pair[1], `${setting}'s environment`)];
const [pattern, environment, roots] = entry;
let rootsUrl = null;
if (roots !== undefined && roots !== null && roots !== "") {
let parsed;
try {
parsed = new URL(roots);
} catch {
throw new Error(`${setting}'s roots URL must be a URL, "" or null, not ${JSON.stringify(roots)}`);
}
// A roots URL that the log's own pattern matches is probably on the log's hostname.
if ((parsed.hostname + parsed.pathname).search(pattern) !== -1) {
throw new Error(`${setting}'s roots URL must be on another hostname than the log's, as Cloudflare fails a Snippet that makes more than one request to the origin: ${roots}`);
}
rootsUrl = parsed.href;
}
return { pattern, origins: originsFor(environment, `${setting}'s environment`), rootsUrl };
});

function originsFor(environment, setting) {
Expand All @@ -107,16 +128,16 @@ function originsFor(environment, setting) {
return ENVIRONMENTS[environment];
}

// Returns the origins of the environment for a request's URL.
function originsForLog(url) {
// Returns the settings ({ origins, rootsUrl }) for a request's URL.
function logSettings(url) {
const log = url.hostname + url.pathname;
for (const [pattern, origins] of LOG_ORIGINS) {
for (const settings of LOG_SETTINGS) {
// search() ignores lastIndex, so a pattern with the g or y flag matches every time.
if (log.search(pattern) !== -1) {
return origins;
if (log.search(settings.pattern) !== -1) {
return settings;
}
}
return DEFAULT_ORIGINS;
return DEFAULT_LOG;
}

// OIDs as keys from oidKey(), i.e. their minimal encodings (without tag and length).
Expand Down Expand Up @@ -615,24 +636,33 @@ function isRoot(roots, name) {
return roots[lo] === h;
}

// Each log's accepted roots, by get-roots URL: { roots, expires }, roots being the
// Each log's accepted roots, by roots URL: { roots, expires }, roots being the
// sorted hashes of their subject names (4 bytes per root), or null if they couldn't be
// read.
const ROOTS_CACHE = new Map();
const MAX_CACHED_LOGS = 100;
// Roots' subjects are mostly within their first 513 bytes (684 base64 characters).
const ROOT_PREFIX_CHARS = 684;
// The largest get-roots response that is read (the largest real ones are about 1 MB),
// so that reading one stays well within the time limit.
const MAX_ROOTS_BYTES = 2 * 1024 * 1024;
// A roots URL's response lists the hashes of a log's accepted roots' subject names (as
// generated by tools/roots-hashes.mjs): this line, then one hash per line, as 8 hex digits
// (nameHash() of the DER subject name), optionally followed by whitespace and anything else
// (such as the subject name, for people to read). Blank lines, and lines starting with "#",
// are ignored. Precalculated hashes, rather than certificates, are read so that a log with
// hundreds of roots stays well within the Snippet's CPU time limit.
const ROOTS_HEADER = "CF_CTreq Accepted Roots v1";
const ROOTS_LINE = /^([0-9a-fA-F]{8})(?:[\t ]|$)/;
// The largest response that is read (about 3,000 annotated roots).
const MAX_ROOTS_BYTES = 256 * 1024;
const ASCII = new TextDecoder();

// Returns the hashes of the accepted root names of the log that a request (to add-chain
// or add-pre-chain) is for, or null if they are unknown, from the in-memory cache, or else
// the get-roots endpoint, whose response Cloudflare caches too. Only data is shared
// between requests, never a promise, as I/O can't be.
async function rootHashes(url) {
const rootsUrl = url.origin + url.pathname.slice(0, url.pathname.lastIndexOf("/") + 1) + "get-roots";
// Returns the hashes of the accepted root names of a log, or null if they are unknown or
// aren't to be checked, from the in-memory cache, or else the log's roots URL, whose
// response Cloudflare caches too. Only data is shared between requests, never a promise,
// as I/O can't be.
async function rootHashes(rootsUrl, url) {
// Fetching from the log's own hostname would make forwarding fail (error 1201), so a
// roots URL there is ignored.
if (rootsUrl === null || new URL(rootsUrl).hostname === url.hostname) {
return null;
}
const cached = ROOTS_CACHE.get(rootsUrl);
if (cached !== undefined && cached.expires > Date.now()) {
return cached.roots;
Expand All @@ -653,95 +683,30 @@ async function rootHashes(url) {
return roots;
}

// Reads a get-roots response, {"certificates":["<base64 DER>",...]}, returning the
// sorted hashes of the roots' subject names, or null if it isn't one. It's streamed,
// and only enough of each root is decoded to find its subject, so that a response of a
// megabyte (hundreds of roots) needs little memory and CPU time. Go's encoding/json escapes no base64
// characters, so each string ends at the next quote. A response larger than
// MAX_ROOTS_BYTES isn't read.
// Reads a roots URL's response (see ROOTS_HEADER), returning the sorted hashes, or null if
// it isn't a list of at least one hash, or is larger than MAX_ROOTS_BYTES.
async function readRootHashes(response) {
if (!response.ok || response.body === null) {
if (!response.ok) {
await response.body?.cancel();
return null;
}
const reader = response.body.getReader();
const body = await readBody(response, MAX_ROOTS_BYTES);
const lines = body === null ? [] : ASCII.decode(body).split("\n");
if (lines[0]?.trimEnd() !== ROOTS_HEADER) {
return null;
}
const hashes = [];
let key;
let pending = new Uint8Array(0);
let total = 0;
for (let r = await reader.read(); !r.done; r = await reader.read()) {
total += r.value.length;
if (total > MAX_ROOTS_BYTES) {
await reader.cancel();
return null;
}
let buf = r.value;
if (pending.length) {
buf = new Uint8Array(pending.length + r.value.length);
buf.set(pending);
buf.set(r.value, pending.length);
}
let i = 0;
for (let open, close; (open = buf.indexOf(0x22, i)) !== -1; i = close + 1) {
close = buf.indexOf(0x22, open + 1);
if (close === -1) {
break;
}
const string = buf.subarray(open + 1, close);
// The first string must be the key, and the rest roots.
const hash = key === undefined ? ((key = ASCII.decode(string)) === "certificates" ? -1 : null) : rootHash(string);
if (hash === null) {
await reader.cancel();
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line !== "" && !line.startsWith("#")) {
const hex = ROOTS_LINE.exec(line)?.[1];
if (hex === undefined) {
return null;
}
if (hash >= 0) {
hashes.push(hash);
}
}
pending = buf.subarray(buf.indexOf(0x22, i) === -1 ? buf.length : i);
}
return key === undefined ? null : new Uint32Array(hashes).sort();
}

// Returns the hash of a root's subject name from the bytes of its base64 DER, or null
// if it can't be found. Usually, only a prefix needs decoding.
function rootHash(bytes) {
try {
let der = atob(ASCII.decode(bytes.subarray(0, ROOT_PREFIX_CHARS)));
let subject = subjectOf(der);
if (subject === null && bytes.length > ROOT_PREFIX_CHARS) {
der = atob(ASCII.decode(bytes));
subject = subjectOf(der);
}
return subject === null ? null : nameHash(der, subject.start, subject.end);
} catch {
return null; // Not base64.
}
}

// Returns the location of the subject name in a DER certificate, or a prefix of one,
// or null if it isn't found there. Roots are trusted to be well-formed, so the
// Certificate and TBSCertificate headers, whose values extend past a prefix, are
// simply skipped.
function subjectOf(der) {
let i = 0;
for (let n = 0; n < 2; n++) {
const length = der.charCodeAt(i + 1);
i += 2 + (length > 0x80 ? length & 0x7f : 0);
}
if (der.charCodeAt(i) === 0xa0) {
i = derDecode(der, i)?.end ?? der.length; // version
}
let field;
// serialNumber, signature, issuer, validity, subject.
for (const tag of [0x02, 0x30, 0x30, 0x30, 0x30]) {
field = derDecode(der, i);
if (field?.tag !== tag) {
return null;
hashes.push(parseInt(hex, 16));
}
i = field.end;
}
return field;
return hashes.length ? new Uint32Array(hashes).sort() : null;
}

// Returns true for an add-pre-chain path, false for an add-chain path, or null for any
Expand All @@ -762,13 +727,13 @@ function isPreChainPath(pathname) {
return endpoint === "add-pre-chain" ? true : endpoint === "add-chain" ? false : null;
}

// Reads the request body as bytes, or returns null as soon as it exceeds maxBytes, so that
// a body without a Content-Length (e.g. chunked) is bounded too.
async function readBody(request, maxBytes) {
// Reads a request's (or response's) body as bytes, or returns null as soon as it exceeds
// maxBytes, so that a body without a Content-Length (e.g. chunked) is bounded too.
async function readBody(message, maxBytes) {
const chunks = [];
let total = 0;
if (request.body !== null) {
const reader = request.body.getReader();
if (message.body !== null) {
const reader = message.body.getReader();
for (let r = await reader.read(); !r.done; r = await reader.read()) {
total += r.value.byteLength;
if (total > maxBytes) {
Expand Down Expand Up @@ -866,7 +831,7 @@ function bodyRejectionOrChain(body) {
export default {
async fetch(request) {
const url = new URL(request.url);
const origins = originsForLog(url);
const { origins, rootsUrl } = logSettings(url);
const isPreChainAPI = isPreChainPath(url.pathname);
if (isPreChainAPI === null) {
return fetch(request);
Expand All @@ -886,7 +851,7 @@ export default {
return errorResponse(rejection("body-too-large", "request body too large", 413), origins);
}
// Started now, so that any fetch of the roots overlaps reading and parsing the body.
const rootsPromise = rootHashes(url);
const rootsPromise = rootHashes(rootsUrl, url);
const body = await readBody(request, maxBodyBytes);
if (body === null) {
return errorResponse(rejection("body-too-large", "request body too large", 413), origins);
Expand Down
Loading
Loading