Skip to content

blackhole ​

@c9up/blackhole: a security filter run by a native Rust engine: CSRF tokens, rate limiting, path traversal and parameter pollution refusal, CORS, protective headers with a nonce-based CSP, and HTML response sanitization, for ream, Express, Fastify and Hono. It also holds the named per-route limiter. Guide: CSRF protection.

Installation ​

bash
pnpm add @c9up/blackhole

ream configure @c9up/blackhole registers the provider, registers @c9up/blackhole/server_middleware on the server tier and @c9up/blackhole/middleware at the end of the router tier, and writes config/blackhole.ts. A Redis-backed store needs @c9up/quasar (or a client of your own); the database limiter store uses @c9up/atlas when present; @c9up/blackhole/hono needs hono.

The native engine comes as one small package per platform, @c9up/blackhole-<platform>, for linux x64 and arm64 (glibc), macOS x64 and arm64, and Windows x64. @c9up/blackhole lists them as optional dependencies, and your package manager installs only the one that matches your machine. No Rust toolchain is needed. Do not install with optional dependencies turned off (--no-optional, --omit=optional): the engine would be left out.

There is no JavaScript fallback. Without the engine, createBlackhole() throws E_BLACKHOLE_NAPI_REQUIRED, and its message names the package to install, for example @c9up/blackhole-linux-x64-gnu.

Entry points ​

ImportContents
@c9up/blackholecreateBlackhole, defineConfig, the option types, CSP keywords, rate-limit stores, the limiter, errors
@c9up/blackhole/configdefineConfig, BlackholeConfig
@c9up/blackhole/providerBlackholeProvider (default export) and its container keys
@c9up/blackhole/server_middlewareThe server-tier middleware: rate limit, path checks, CORS, headers
@c9up/blackhole/middlewareThe router-tier middleware: CSRF, CSP nonce, response sanitization
@c9up/blackhole/expressblackholeExpress()
@c9up/blackhole/fastifyblackholeFastify()
@c9up/blackhole/honoblackholeHono()
@c9up/blackhole/limiterThe named and manual rate limiter
@c9up/blackhole/services/limiterThe application's LimiterManager (default export)
@c9up/blackhole/configureconfigure(), run by ream configure @c9up/blackhole

Exports ​

@c9up/blackhole ​

The filter:

ExportKindDescription
createBlackholefunctioncreateBlackhole(options?: BlackholeOptions): Blackhole; validates the options and throws on a misconfiguration
BlackholeinterfaceThe filter instance (see below)
BlackholeOptionsinterfaceOptions of createBlackhole (see Configuration)
defineConfigfunctiondefineConfig(config: BlackholeConfig): BlackholeConfig
BlackholeConfiginterfaceBlackholeOptions plus limiter and the named rate-limit stores
CheckResultinterface{ allowed, status?, body?, headers?, rateLimit?: RateLimitMeta, csrfEnforced? }
EngineChecktype'rateLimit' | 'shield' | 'csrf'
RateLimitMetainterface{ limit, remaining, resetSeconds }
BlackholeContextinterface{ request: { method(), url(), header(name) }, route? }, what a CORS or CSRF callback receives
pathContextfunctionpathContext(method: string, path: string, headers?, routePattern?): BlackholeContext, for a host without a context object
clientKeyfunctionclientKey(ip: string): string; an IPv6 address counts by its /64
assertNoRetiredOptionsfunctionassertNoRetiredOptions(options: object): void; throws for a removed option
ReleasableSecretinterface{ release(): string }, an environment Secret

Headers and CSP:

ExportKindDescription
SecurityHeadersOptionsinterface{ xFrame?, hsts?, contentTypeSniffing?, csp?, referrerPolicy?, permissionsPolicy? }
XFrameConfiginterface{ enabled?, action?: 'DENY' | 'SAMEORIGIN' | 'ALLOW-FROM', domain? }
HstsConfiginterface{ enabled?, maxAge?: number | string, includeSubDomains?, preload? }
ContentTypeSniffingConfiginterface{ enabled? }
CspConfiginterface{ enabled?, directives?, useDefaults?, policy?, reportOnly? }
ReferrerPolicyConfig, PermissionsPolicyConfiginterface{ enabled?, policy? }
DEFAULT_CSP_POLICYconst"default-src 'self'; base-uri 'self'; form-action 'self'; object-src 'none'; script-src 'self' @nonce"
getDefaultDirectivesfunctiongetDefaultDirectives(): Record<string, string[]>, the baseline merged under csp.directives
dangerouslyDisableDefaultSrcconstSymbol to set csp.directives.defaultSrc to, to drop default-src
cspKeywordsconstThe CspKeywords registry: register(keyword, resolver), has(keyword)
CspKeywordsclassThe registry's class
CspKeywordResolvertype(request: unknown, response: unknown, nonce: string | undefined) => string; an empty answer drops the keyword
CspKeywordTargetinterface{ request?, response? }

CSRF, CORS, XSS:

ExportKindDescription
CsrfConfiginterfaceCSRF options (see Configuration)
CsrfCookieConfiginterface{ sameSite?: 'strict' | 'lax' | 'none', secure?, httpOnly?, path? }
CorsConfiginterfaceCORS options (see Configuration)
CorsResultinterface{ headers, varyOrigin, preflight }
CorsOriginValue, CorsHeadersValuetypeboolean | string | string[]
XssConfiginterface{ enabled?, allowAttributes?: XssOptInAttribute[], maxInputBytes?, maxDepth? }
XssOptInAttributetype'id' | 'is' | 'slot' | 'part' | 'data-*'

Rate-limit stores (the global rateLimit option):

ExportKindDescription
RateLimitStoreinterface{ increment(key: string, windowSeconds: number): Promise<{ count: number; resetSeconds: number }> }
RateLimitStoreFactorytype() => RateLimitStore
storesconststores.memory(), stores.redis({ connection, prefix? }); connection is a client, a resolver or a @c9up/quasar connection name
MemoryRateLimitStoreclassCounts in this process
RedisRateLimitStoreclassnew RedisRateLimitStore(source: RateLimitRedisSource, options?: { prefix?: string }); prefix blackhole:rate
RateLimitRedisClientinterface{ incr, expire, ttl }
RateLimitRedisSourcetypeA client, or a function returning one
RateLimitContextinterface{ request: { ip(): string }; auth?: { user?: { id? } | null } }, what keyFor receives
RateLimitDecisioninterface{ allowed, limit, remaining, resetSeconds }
quasarConnectionfunctionquasarConnection(name?: string): Promise<RateLimitRedisClient>, a @c9up/quasar connection checked for the commands the counter issues

Refused SQL statements:

ExportKindDescription
onUnsafeStatementfunctiononUnsafeStatement(policy: UnsafeStatementPolicy): (event: UnsafeStatementLike) => Promise<void>: counts the statements the query builder refused per key, and blocks a key past a threshold (notifies only, outside production)
UnsafeStatementPolicyinterface{ keyFor, store: RateLimitStore, windowSeconds?, thresholds?: { injectionPattern?, invalidShape? }, onBlock, onNotify?, blocking? }; window 1 hour, thresholds 1 and 5
UnsafeStatementLikeinterface{ kind: 'injection-pattern' | 'invalid-shape'; code; message; connection? }
UnsafeStatementDecisioninterface{ key?, count, threshold, blocked, event }
ExportKindDescription
errorsnamespaceThe package's errors, one class per code, see Errors

Every export of @c9up/blackhole/limiter is also exported here.

Blackhole ​

MemberSignatureDescription
checkcheck(req: { method; path; query?; headers; body?; remoteAddr?; protocol?; host?; csrfBinding?; checks?: EngineCheck[] }): CheckResultRun the request checks in the engine
checkRateLimitcheckRateLimit(key: string): Promise<RateLimitDecision>Count against the configured store; throws without one
hasRateLimitStorehasRateLimitStore(): booleanWhether a store counts instead of the engine
rateLimitKeyrateLimitKey(ctx: RateLimitContext): stringrateLimit.keyFor(ctx), or the client key of the IP
rateLimitKeyedByRequestrateLimitKeyedByRequest(): booleanWhether keyFor is set
generateCsrfTokengenerateCsrfToken(binding?: string): stringA signed token, bound to a session secret when given
csrfTokenIsValidcsrfTokenIsValid(token: string, binding?: string): booleanWhether a held token still verifies (to decide on reissuing it)
csrfEnabled / xsrfCookieEnabled / csrfSessionRequired(): booleanCSRF switches
csrfExemptcsrfExempt(ctx: BlackholeContext): booleanWhether exceptRoutes exempts the request
csrfCookiecsrfCookie(): { name: string; options: Record<string, unknown> }The XSRF-TOKEN cookie to seed
securityHeaderssecurityHeaders(nonce?: string, target?: CspKeywordTarget): Record<string, string>The protective headers, @nonce and other keywords resolved
cspHasNoncecspHasNonce(): booleanWhether the CSP names @nonce
generateNoncegenerateNonce(): stringA fresh nonce
corscors(requestOrigin: string, method: string, requestMethod?: string, requestHeaders?: string, ctx?: BlackholeContext): CorsResult | undefinedThe CORS decision; undefined without cors
sanitizeResponsesanitizeResponse(body: string, contentType: string): stringSanitize a text/html body as untrusted markup, whatever xss says; throws E_BLACKHOLE_HTML_TOO_LARGE, E_BLACKHOLE_HTML_TOO_DEEP or E_BLACKHOLE_HTML_TOO_COMPLEX past the bounds
xssEnabledxssEnabled(): booleanWhether responses are sanitized

@c9up/blackhole/config ​

ExportKindDescription
defineConfigfunctionSame as in @c9up/blackhole
BlackholeConfiginterfaceSame as in @c9up/blackhole

@c9up/blackhole/provider ​

ExportKindDescription
defaultproviderBlackholeProvider: builds the filter from the blackhole config (secret defaults to APP_KEY; CSRF bound to the session when config/session.ts exists) and the LimiterManager from limiter
BlackholeAppContextinterface{ container, config }
BLACKHOLE_KEYconst'blackhole'
BLACKHOLE_LIMITER_KEYconst'blackhole.limiter'

@c9up/blackhole/server_middleware ​

ExportKindDescription
defaultclassBlackholeServerMiddleware, handle(ctx, next): rate limit (unless keyFor defers it), path traversal and parameter pollution checks, CORS and protective headers, on every request including a 404
blackholeServerMiddlewarefunctionblackholeServerMiddleware(ctx: ReamContext, next): Promise<void>, the same as a function

@c9up/blackhole/middleware ​

ExportKindDescription
defaultclassBlackholeMiddleware, handle(ctx, next): CSRF check after the body parser and the session, CSP nonce on response.nonce, CSRF token on request.csrfToken, response sanitization; runs the server-tier checks too when that half is not registered
blackholeMiddlewarefunctionblackholeMiddleware(ctx: ReamContext, next): Promise<void>
blackholeServerMiddlewarefunctionSame as in @c9up/blackhole/server_middleware
createBlackhole, Blackhole, BlackholeOptionsfunction / typeSame as in @c9up/blackhole
ReamContextinterfaceThe slice of the HTTP context the middleware reads

@c9up/blackhole/express, /fastify, /hono ​

ExportKindDescription
blackholeExpressfunctionblackholeExpress(options?: BlackholeOptions), an Express middleware for the whole pipeline
blackholeFastifyfunctionblackholeFastify(options?: BlackholeOptions), a Fastify plugin; register it at the root scope
blackholeHonofunctionblackholeHono(options?: BlackholeHonoOptions): MiddlewareHandler
BlackholeHonoOptionsinterfaceBlackholeOptions plus clientIp?(c), protocol?(c), host?(c); defaults read @hono/node-server and the request URL

The adapters have no session: csrf.session: true refuses every request there. A page the application renders itself skips response sanitization when trustedHtml is true in res.locals (Express), on the reply (Fastify), with c.set (Hono) or in ctx.store (ream).

@c9up/blackhole/limiter ​

ExportKindDescription
LimiterManagerclassNamed stores and limits: use, multi, allowRequests, define, noLimit, clear
LimiterclassOne limit on one store
MultiLimiterclassSeveral limits counted together, each on its key
HttpLimiterclassBuilder of a route limit: store, allowRequests, every, usingKey, blockFor, limitExceeded
defineThrottlefunctiondefineThrottle(name: string, builder: (ctx) => HttpLimiter | null | Promise<HttpLimiter | null>): ThrottleMiddleware
ThrottleMiddlewaretype(ctx: LimiterHttpContext, next) => Promise<void>
LimiterResponseclass{ limit, remaining, consumed, availableIn }, toJSON()
limiterStoresconstmemory(), redis({ connection, prefix? }), database({ connection?, tableName? })
MemoryLimiterStoreclassIn-process store
RedisLimiterStoreclassnew RedisLimiterStore(source: LimiterRedisSource, options?: { prefix?: string }); prefix blackhole:limiter
DatabaseLimiterStoreclassnew DatabaseLimiterStore(source: LimiterDatabaseSource, options?: { tableName?: string }); PostgreSQL, MySQL or SQLite, table rate_limits
LimiterStoreinterface{ name; increment(key, amount, durationSeconds); set(key, consumed, durationSeconds); get(key); delete(key); clear() }, each returning a promise
LimiterRecordinterface{ consumed: number; msBeforeNext: number }
LimiterStoreFactorytype() => LimiterStore
LimiterRedisClient, LimiterRedisSourceinterface / typeWhat the Redis store calls
LimiterDatabase, LimiterDatabaseSourceinterface / type{ dialect; rawQuery(sql, bindings) }, or a function returning one
LimiterOptionsinterface{ requests: number; duration: number | string; blockDuration?: number | string }
MultiLimiterOptionsinterfaceLimiterOptions & { key: string | number }
LimiterConfiginterface{ default: string; stores: Record<string, LimiterStoreFactory> }
ThrottleContext, LimiterHttpContextinterfaceThe HTTP context slice a throttle reads

LimiterManager:

MemberSignatureDescription
useuse(options: LimiterOptions): Limiter
use(store: string, options: LimiterOptions): Limiter
A limiter on the default or the named store
multimulti(options: MultiLimiterOptions[]): MultiLimiter
multi(store: string, options: MultiLimiterOptions[]): MultiLimiter
allowRequestsallowRequests(requests: number): HttpLimiterStart a route limit
definedefine(name: string, builder): ThrottleMiddlewareA named route middleware; the builder returns null for no limit
noLimitnoLimit(): null
clearclear(stores?: string[]): Promise<void>Empty every store, or the named ones
configreadonly config: LimiterConfig

Limiter (new Limiter(store: LimiterStore, options: LimiterOptions)):

MemberSignatureDescription
consumeconsume(key: string, amount?: number): Promise<LimiterResponse>Count; throws errors.E_TOO_MANY_REQUESTS over the budget
increment / decrementincrement(key: string, amount?: number): Promise<LimiterResponse>Count without throwing
attemptattempt<T>(key: string, callback: () => T | Promise<T>, amount?: number): Promise<T | undefined>Run callback if the key has budget left
penalizepenalize<T>(key: string, callback: () => T | Promise<T>, amount?: number): Promise<[ThrottleException, null] | [null, T]>Run callback, the attempt counted first; a throw keeps it counted, a success clears the key
blockblock(key: string, duration: number | string): Promise<LimiterResponse>Block the key
setset(key: string, requests: number, duration?: number | string): Promise<LimiterResponse>
getget(key: string): Promise<LimiterResponse | null>
deletedelete(key: string): Promise<boolean>
remaining / availableInremaining(key: string): Promise<number>
isBlockedisBlocked(key: string): Promise<boolean>
clearclear(): Promise<void>
ts
import limiter from '@c9up/blackhole/services/limiter'

export const throttle = limiter.define('api', (ctx) =>
  limiter.allowRequests(60).every('1 minute').usingKey(ctx.auth?.user?.id),
)

const login = limiter.use({ requests: 5, duration: '1 minute', blockDuration: '20 mins' })
const [error, user] = await login.penalize(`login_${email}`, () => verify(email, password))

@c9up/blackhole/services/limiter ​

ExportKindDescription
defaultconstlimiter: LimiterManager, the manager BlackholeProvider seated, or an in-memory one when none was
setLimiter / clearLimiterfunctionsetLimiter(manager: LimiterManager): void / clearLimiter(manager: LimiterManager): void

@c9up/blackhole/configure ​

ExportKindDescription
configurefunctionconfigure(codemods): Promise<void>: registers the provider and both middleware, writes config/blackhole.ts

Configuration ​

config/blackhole.ts, read under the blackhole key.

ts
import { defineConfig, stores } from '@c9up/blackhole'
import env from '#start/env'

export default defineConfig({
  secret: env.get('APP_KEY'),
  csrf: { trustedOrigins: ['https://admin.example.com'] },
  rateLimit: {
    max: 100,
    windowSeconds: 60,
    default: env.get('RATE_LIMIT_STORE', 'memory'),
    stores: { memory: stores.memory(), redis: stores.redis({ connection: 'main' }) },
  },
  csp: { directives: { scriptSrc: ["'self'", '@nonce', 'https://cdn.example'] } },
})
OptionTypeDefaultDescription
secretstring | ReleasableSecretAPP_KEY (provider)Signs CSRF tokens; required while CSRF is on, a publicly known value is refused
csrfboolean | CsrfConfigtrueCSRF check on unsafe methods
rateLimit{ max; windowSeconds; keyFor?; default?; stores?; store? }—One budget for every request; without a store, the engine counts in this process
limiterLimiterConfig{ default: 'memory', stores: { memory } }Stores behind the named and manual limits
pathTraversalbooleantrueRefuse .. in the path, raw or percent-encoded
paramPollutionbooleantrueRefuse a repeated query key (keys ending in [] excepted)
corsCorsConfig—Omitted, CORS is not handled
xssboolean | XssConfigfalseSanitize every text/html response as untrusted markup; allowAttributes, maxInputBytes (1 MiB), maxDepth (512)
xFrameXFrameConfigon, SAMEORIGINX-Frame-Options
hstsHstsConfigon, 180 days, includeSubDomainsStrict-Transport-Security; maxAge in seconds or a duration string
contentTypeSniffingContentTypeSniffingConfigonX-Content-Type-Options: nosniff
cspCspConfigon, DEFAULT_CSP_POLICYContent-Security-Policy; directives merge over getDefaultDirectives() unless useDefaults: false, or a policy string; reportOnly
referrerPolicyReferrerPolicyConfigon, strict-origin-when-cross-originReferrer-Policy
permissionsPolicyPermissionsPolicyConfigon, camera=(), microphone=(), geolocation=()Permissions-Policy

X-XSS-Protection: 0 is always sent. A header is added only where the handler did not set it. An unknown key in a header object throws.

CsrfConfig:

OptionTypeDefaultDescription
enabledbooleantrue
methodsstring[]POST, PUT, PATCH, DELETEMethods checked; a request is checked when its request-line method or its overridden (_method) method is listed
exceptRoutesstring[] | ((ctx: BlackholeContext) => boolean)—Route patterns, paths, path prefixes ending in *, or a predicate
trustedOriginsstring[]—Other origins allowed for state-changing requests; same origin always is
enableXsrfCookiebooleantrueSeed the readable XSRF-TOKEN cookie
sessionbooleantrue when config/session.ts exists (provider)Bind tokens to the session and refuse a request without one
cookie / cookieOptionsCsrfCookieConfigsameSite: 'lax', path: '/'XSRF-TOKEN cookie attributes

The token is read from X-XSRF-TOKEN, X-CSRF-TOKEN or the _csrf form field.

CorsConfig:

OptionTypeDefaultDescription
originCorsOriginValue | ((origin, ctx) => CorsOriginValue)—true reflects, '*' is the wildcard, false none, a string or array is an allow-list; true and '*' are refused with credentials
enabledboolean | ((ctx) => boolean)true
methodsstring[]GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONSPreflight methods
headersCorsHeadersValue | ((headers, ctx) => CorsHeadersValue)Content-Type, Authorization, Accept, X-Requested-WithPreflight request headers; true reflects
exposeHeaders / exposedHeadersstring[]—Access-Control-Expose-Headers
credentialsboolean—Access-Control-Allow-Credentials: true
maxAgenumber—Preflight cache, in seconds

Container bindings ​

TokenResolves to
'blackhole' (BLACKHOLE_KEY)The Blackhole built from the blackhole config
'blackhole.limiter' (BLACKHOLE_LIMITER_KEY)The LimiterManager

Errors ​

CodeRaised when
E_TOO_MANY_REQUESTSThe global rate limit refused the request (429, Retry-After), or a throttle middleware or limiter.consume() found the key over its budget (ThrottleException, 429)
E_BAD_CSRF_TOKENThe CSRF token is missing or invalid (403, errors.InvalidCSRFToken)
E_BLACKHOLE_CSRF_ORIGIN_MISMATCHA state-changing request came from an origin not trusted (403)
E_BLACKHOLE_PATH_TRAVERSALThe path holds .. (400)
E_BLACKHOLE_PARAMETER_POLLUTIONA query key appears twice (400)
E_BLACKHOLE_MISSING_IPRate limiting is on and the request has no client address (400)
E_BLACKHOLE_BLOCKEDA refusal the engine gave no JSON body for
E_BLACKHOLE_SESSION_NOT_READYCSRF is bound to the session and the request reached the middleware without one (500)
E_BLACKHOLE_UNKNOWN_CSP_KEYWORDThe CSP names an @keyword with no resolver, or a keyword was registered under an invalid name
E_BLACKHOLE_INVALID_CSP_KEYWORD_VALUEA keyword resolver answered a value with ;, , or a line break
E_BLACKHOLE_DATABASE_UNAVAILABLEThe database limiter store has no database to count in
E_BLACKHOLE_UNSUPPORTED_DATABASEThe database limiter store was given a database other than PostgreSQL, MySQL or SQLite
E_BLACKHOLE_INVALID_CSPThe Content-Security-Policy is invalid: a bad directive name or value, an unquoted keyword, a directive given twice, no default-src, a line break, or both directives and policy.
E_BLACKHOLE_REMOVED_OPTIONThe config still sets an option that was removed (securityHeaders); the message names its replacement.
E_BLACKHOLE_UNKNOWN_OPTIONA header or CORS option names a key it does not have.
E_BLACKHOLE_INVALID_HEADER_VALUEAn option's value cannot be written as a header value (empty, or a control or non-ASCII character).
E_BLACKHOLE_INVALID_XFRAMExFrame.action is not DENY, SAMEORIGIN or ALLOW-FROM, or ALLOW-FROM has no domain.
E_BLACKHOLE_INVALID_CORSCORS combines a wildcard origin with credentials: true, or its origin list holds something other than non-empty strings.
E_BLACKHOLE_INVALID_DURATIONA duration (HSTS maxAge, a limiter duration) is negative or unreadable.
E_BLACKHOLE_INVALID_RATE_LIMITA rate limit or limiter is given a request count or duration that is not a positive whole number.
E_BLACKHOLE_INVALID_XSS_ATTRIBUTExss.allowAttributes names an attribute outside the opt-in list (id, is, slot, part, data-*, class); the native engine refuses it.
E_BLACKHOLE_INVALID_XSS_LIMITxss.maxInputBytes or xss.maxDepth is not a positive whole number.
E_BLACKHOLE_HTML_TOO_LARGEsanitizeResponse() (or the xss pass) was given a text/html body longer than xss.maxInputBytes (1 MiB by default).
E_BLACKHOLE_HTML_TOO_DEEPThe HTML to sanitize nests elements deeper than xss.maxDepth (512 by default), measured on the tree the parser builds.
E_BLACKHOLE_HTML_TOO_COMPLEXThe HTML to sanitize builds more than four times its size plus 64 KiB (misnested formatting tags re-created in every block).
E_BLACKHOLE_UNKNOWN_CHECKcheck() was asked for a check the engine does not have (only rateLimit, shield and csrf exist).
E_BLACKHOLE_INVALID_HEADERScheck() was given request headers that are not an object of strings.
E_BLACKHOLE_INTERNAL_PANICThe native engine panicked while checking a request; cause carries the native error.
E_BLACKHOLE_SECRET_MISSINGCSRF is enabled and no secret was given.
E_BLACKHOLE_WEAK_SECRETThe CSRF secret is the scaffolding placeholder, or shorter than 16 characters.
E_BLACKHOLE_INVALID_SECRETThe CSRF secret is neither a string nor a Secret wrapping one (a TypeError).
E_BLACKHOLE_NAPI_REQUIREDThe blackhole native engine is not loaded for this platform; cause carries the load failure.
E_BLACKHOLE_NOT_REGISTEREDThe middleware ran without a Blackhole instance: BlackholeProvider is not registered, or the host exposes no ctx.containerResolver.
E_BLACKHOLE_RATE_LIMIT_STORE_MISSINGcheckRateLimit() was called without a configured rateLimit.store.
E_BLACKHOLE_EMPTY_RATE_LIMIT_KEYcheckRateLimit() was given an empty key, which would count every request in one bucket.
E_BLACKHOLE_UNKNOWN_STOREA limiter or rateLimit names a store that is not declared in stores.
E_BLACKHOLE_NO_DEFAULT_STORErateLimit declares stores but no default naming the one that counts.
E_BLACKHOLE_LIMITER_OPTIONS_MISSINGlimiter.use() or limiter.multi() was called without the requests and duration of its limiter(s).
E_BLACKHOLE_INCOMPLETE_THROTTLEA throttle middleware ran on a limiter without allowRequests() and every().
E_BLACKHOLE_INVALID_TABLE_NAMEThe database limiter store's tableName is not a table name.
E_BLACKHOLE_QUASAR_MISSINGA Redis store names a connection and @c9up/quasar is not installed.
E_BLACKHOLE_INCOMPLETE_CONNECTIONThe named quasar connection lacks the commands the counter issues.

An invalid option (a CSP source left unquoted, CORS credentials with a wildcard origin, a fractional rate limit, a missing or public secret) throws when the filter is built. Each thrown code is a class of the errors namespace and a @c9up/flare Exception with status 500: branch on error.code or on error instanceof errors.E_BLACKHOLE_*, with import { errors } from '@c9up/blackhole'. The codes of a refused request (E_TOO_MANY_REQUESTS from the global rate limit, E_BLACKHOLE_PATH_TRAVERSAL…) are not thrown: they are the code of the response body. E_BAD_CSRF_TOKEN is the exception: the Ream middleware throws it (below).

errors.E_TOO_MANY_REQUESTS is the class errors.ThrottleException under its code: status 429, response (the LimiterResponse), setMessage, setStatus, setHeaders, t(identifier, data?), getDefaultHeaders(). Its handle shapes the 429 with request.accepts(['html', 'application/vnd.api+json', 'json']) when the host has it: the message as text, { errors: [{ message, retryAfter }] }, or a JSON:API error object.

errors.E_BAD_CSRF_TOKEN is the class errors.InvalidCSRFToken under its code: status 403, message "Invalid or expired CSRF token", identifier (errors.E_BAD_CSRF_TOKEN, the translation key), getResponseMessage(error, ctx) and handle(error, ctx). Its handle writes the refusal: for a browser with a session, it flashes the form back (without _csrf, _method and passwords), flashes the message under error and E_BAD_CSRF_TOKEN, and redirects back; otherwise it answers 403 { "error": { "code": "E_BAD_CSRF_TOKEN", "message": "..." } }. The Ream middleware throws it, as @adonisjs/shield does: Ream renders an error thrown inside the middleware chain there, so the session middleware still commits the flash, and an application's exception handler can catch or customise it. The Express, Fastify and Hono adapters answer the same refusal in place.

errors.E_BLACKHOLE_UNKNOWN_CSP_KEYWORD and errors.E_BLACKHOLE_INVALID_CSP_KEYWORD_VALUE carry keyword, errors.E_BLACKHOLE_UNSUPPORTED_DATABASE carries dialect.

Migration ​

securityHeaders is no longer an option, and a config that still sets it throws. Move each header to its own top-level key: xFrame, hsts, contentTypeSniffing, csp, referrerPolicy, permissionsPolicy, each { enabled, ... }.

  • BlackholeError and BlackholeTypeError are gone: test error.code, or error instanceof errors.E_BLACKHOLE_* with import { errors } from '@c9up/blackhole'. E_BLACKHOLE_INVALID_SECRET is no longer a TypeError.
  • The error classes are no longer exported one by one: E_BLACKHOLE_SESSION_NOT_READY becomes errors.E_BLACKHOLE_SESSION_NOT_READY, and so on; ThrottleException becomes errors.ThrottleException, also errors.E_TOO_MANY_REQUESTS.
  • E_BLACKHOLE_RATE_LIMITED is now E_TOO_MANY_REQUESTS: the global rate limit answers its 429 with the code a throttle raises. The body keeps its shape, { "error": { "code": "E_TOO_MANY_REQUESTS", "message": "Too many requests" } }.
  • The Ream middleware now throws errors.E_BAD_CSRF_TOKEN on a CSRF refusal instead of answering it in place. The response is unchanged (the error renders itself through its handle), but an application's exception handler now sees it and can catch or customise it. blackhole therefore needs @c9up/ream 0.2.32 or later, which renders an error thrown inside the middleware chain.
  • The CSRF check now reads the method on the request line as well as the overridden one. A POST whose body carries _method=GET (or HEAD, OPTIONS, any verb outside csrf.methods) used to skip the check; it now needs a valid token, like any POST. Send the token with such forms, or list the route in csrf.exceptRoutes if it really must accept cross-site posts.
  • class is now dropped from untrusted markup, like id and data-*: on a page with utility classes it drew overlays the style filter refuses. Add 'class' to xss.allowAttributes if your page reads no class from untrusted markup.
  • sanitizeResponse() now throws on a text/html body past its bounds — E_BLACKHOLE_HTML_TOO_LARGE over 1 MiB, E_BLACKHOLE_HTML_TOO_DEEP past 512 nested elements, E_BLACKHOLE_HTML_TOO_COMPLEX when the parse would build more than four times the input. Catch them where you sanitize user input, or raise xss.maxInputBytes / xss.maxDepth. With xss: true, such a response now ends in a 500.
  • A _csrf field holding a lone UTF-16 surrogate is now a missing token (403 E_BAD_CSRF_TOKEN on a guarded request) instead of a 500.
  • penalize() now counts the attempt before the callback runs and clears it on success. Requests sent together can no longer all run their callback: at most requests callbacks run per key. An attempt refused because the key had none left now stays counted.
  • A limiter's stored key now carries its budget: <requests>/<duration>/<blockDuration>:<key> (in Redis under the store's prefix, in the database key column). Two budgets on the same key no longer share one counter. Counters written before the upgrade are not read again: every key starts from zero once. Code that reads the Redis keys or the table directly must use the new shape.

See also ​