1. Validate the resolved URL origin, not the input string
Calling `new URL(path, "https://api.example/v1/")` accepts absolute URLs and protocol-relative strings like `//attacker.example/steal`, silently replacing the base host. Checking `startsWith(base)` can still fail on lookalike domains such as `https://api.example.attacker.test`.
Resolve the target URL first, verify its scheme is `http:` or `https:`, reject embedded credentials, and compare `target.origin === base.origin` before attaching any API key or Authorization header.
const base = new URL("https://api.example/v1/");
const target = new URL(userPath, base);
if (target.origin !== base.origin || target.username || target.password) {
throw new Error("Cross-origin or credentialed target blocked");
}2. Publish in-flight promises synchronously
If an async cache helper checks `cache.get(key)`, awaits a setup step, and only then calls `cache.set(key, promise)`, two concurrent callers arriving before the first await both miss the cache and start duplicate work.
Create and store the in-flight promise synchronously before yielding, and evict failed promises from the cache so later callers can retry.
3. Separate absent properties from undefined values
In TypeScript, `{ a: undefined }` satisfies `{ a?: number }`, yet `Object.hasOwn(obj, "a")` is true while `"a" in obj` differs from reading `obj.a`. Similarly, checking truthiness on a Map key or parsed tuple element drops valid `0`, `""`, or `false` entries.
Test presence explicitly with `Object.hasOwn` or `Map.prototype.has` whenever missing state and explicit empty state carry different meanings.
- Verify `target.origin === base.origin` after `new URL(input, base)`.
- Store in-flight promises synchronously before any `await`.
- Require explicit opt-in config before accepting unsigned tokens.
Frequently asked questions
- How can `new URL(path, base)` bypass a base URL in TypeScript or JavaScript?
- According to the WHATWG URL standard, if `path` is an absolute URL (https://other.test) or protocol-relative URL (//other.test), `new URL(path, base)` discards `base` host and resolves to the new origin. Always check `target.origin === base.origin` after resolution.
- Why does `tsc --strict` not catch async cache stampedes or reentrancy bugs?
- Type checking verifies static shapes, not execution order across microtasks or synchronous callbacks. State ownership and promise publication timing must be verified with concurrency and lifecycle regression tests.
Further reading
Start with primary reports and documentation. Community discussions are discovery leads, not evidence on their own.