import { NextResponse, NextRequest, NextFetchEvent, NextMiddleware } from 'next/server'; export { NextMiddleware } from 'next/server'; declare type ActionSource = "strict-dynamic" | "report-sample"; declare type BaseSource = "self" | "unsafe-eval" | "unsafe-hashes" | "unsafe-inline" | "none"; declare type CryptoSource = `${"nonce" | "sha256" | "sha384" | "sha512"}-${string}`; declare type FrameSource = HostSource | SchemeSource | "self" | "none"; declare type HostNameScheme = `${string}.${string}` | "localhost"; declare type HostSource = `${HostProtocolSchemes}${HostNameScheme}${PortScheme}`; declare type HostProtocolSchemes = `${string}://` | ""; declare type HttpDelineator = "/" | "?" | "#" | "\\"; declare type PortScheme = `:${number}` | "" | ":*"; declare type SchemeSource = "http:" | "https:" | "data:" | "mediastream:" | "blob:" | "filesystem:"; declare type Source = HostSource | SchemeSource | CryptoSource | BaseSource; declare type Sources = Source[]; declare type UriPath = `${HttpDelineator}${string}` | `${HostSource}${HttpDelineator}${string}`; interface CspDirectives { "child-src"?: Sources; "default-src"?: Array; "frame-src"?: Sources; "worker-src"?: Sources; "connect-src"?: Sources; "font-src"?: Sources; "img-src"?: Sources; "manifest-src"?: Sources; "media-src"?: Sources; "object-src"?: Sources; "prefetch-src"?: Sources; "script-src"?: Array; "script-src-elem"?: Sources; "script-src-attr"?: Sources; "style-src"?: Array; "style-src-elem"?: Sources; "style-src-attr"?: Sources; "base-uri"?: Array; sandbox?: boolean | Array<"allow-downloads-without-user-activation" | "allow-forms" | "allow-modals" | "allow-orientation-lock" | "allow-pointer-lock" | "allow-popups" | "allow-popups-to-escape-sandbox" | "allow-presentation" | "allow-same-origin" | "allow-scripts" | "allow-storage-access-by-user-activation" | "allow-top-navigation" | "allow-top-navigation-by-user-activation">; "form-action"?: Array; "frame-ancestors"?: Array; "navigate-to"?: Array; "report-uri"?: UriPath[]; "report-to"?: string[]; "require-trusted-types-for"?: Array<"script">; "trusted-types"?: Array<"none" | "allow-duplicates" | "*" | string>; "upgrade-insecure-requests"?: boolean; /** @deprecated */ "require-sri-for"?: Array<"script" | "style" | "script style">; /** @deprecated */ "block-all-mixed-content"?: boolean; /** @deprecated */ "plugin-types"?: Array<`${string}/${string}` | "none">; /** @deprecated */ referrer?: Array<"no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url" | "none">; } declare type BooleanDirectives = "upgrade-insecure-requests" | "block-all-mixed-content"; declare type CspDirectivesLenient = Partial>>; declare type CspFilter = { [K in Exclude]?: RegExp | CspDirectives[K]; }; declare const extendCsp: (csp?: CspDirectives | CspDirectivesLenient, cspExtension?: CspDirectives | CspDirectivesLenient, mergedDirectiveValues?: "append" | "prepend" | "override") => CspDirectives; declare const filterCsp: (directives: CspDirectives | CspDirectivesLenient, excludePatterns: CspFilter) => CspDirectives; declare const cspDirectiveHas: (directives: CspDirectives | CspDirectivesLenient, directive: Exclude, patternOrValue: RegExp | string) => boolean; declare type BuilderConstructorObj = { directives?: CspDirectives | CspDirectivesLenient | string; reportOnly?: boolean; }; declare type CspBuilderConstructorParam = BuilderConstructorObj | CspBuilder | string | [string, string]; declare class CspBuilder { protected _csp: { directives: CspDirectives; reportOnly?: boolean; }; constructor(param?: CspBuilderConstructorParam); withDirectives(cspDirectives?: CspDirectives | CspDirectivesLenient | string, mergeDirectiveValues?: "append" | "prepend" | "override"): this; withoutDirectives(excludeDirectives: (keyof CspDirectives)[]): this; withoutDirectiveValues(excludePatterns: CspFilter): this; withReportOnly(reportOnly?: boolean): this; hasDirective(directive: keyof CspDirectives): boolean; hasDirectiveWithPattern(directive: Exclude, pattern: RegExp | string): void; toHeaderValue(): string; toHeaderKeyValue(): [string, string]; toString(): string; withNonceApplied(nonce: string): this; csp(): { directives: CspDirectives; reportOnly?: boolean; }; withStrictDynamic(hashesOrNonce: string | string[], fallback?: CspDirectives["script-src"], extendScriptSrc?: boolean): this; withStyleHashes(elemHashes?: string[], attrHashes?: string[]): this; } declare type NextMiddlewareResult = NextResponse | Response | null | undefined; declare type ChainFinalizer = (req: NextRequest, evt: NextFetchEvent, ctx: Readonly) => void | Promise; declare type MiddlewareChainContext = { readonly res: { readonly get: () => Response | NextResponse; readonly set: (res: Response | NextResponse) => void; }; readonly cache: { readonly get: (key: string) => unknown; readonly set: (key: string, value: unknown) => void; }; readonly finalize: { readonly addCallback: (finalizer: ChainFinalizer) => void; }; }; declare type ChainableMiddleware = (...params: [ ...spec: Parameters, ctx: MiddlewareChainContext ]) => NextMiddlewareResult | void | Promise; declare type Middleware = ChainableMiddleware; declare type MiddlewareChain = (...middlewares: (ChainableMiddleware | Promise)[]) => NextMiddleware; declare type NextRequestPredicate = (req: NextRequest) => boolean; declare type ChainMatcher = NextRequestPredicate; declare const memoizeInChainCache: (key: string, f: (ctx: MiddlewareChainContext, ...args: Args) => T | Promise) => (ctx: MiddlewareChainContext) => (...args: Args) => Promise; declare const memoizeInGlobalCache: (key: string, f: (...args: Args) => T | Promise) => (...args: Args) => Promise; declare const memoizeResponseHeader: (header: string, fromHeaderValue: (x: string) => T, toHeaderValue: (x: T) => string, merger?: (x1: T, x2: T) => T) => (ctx: MiddlewareChainContext) => [T, (value: T) => void]; declare const matchNot: (matcher: NextRequestPredicate) => NextRequestPredicate; declare const matchAnd: (...matchers: NextRequestPredicate[]) => NextRequestPredicate; declare const matchOr: (...matchers: NextRequestPredicate[]) => NextRequestPredicate; declare const isPagePathRequest: NextRequestPredicate; declare const isPreviewModeRequest: NextRequestPredicate; declare const isNextJsDataRequest: NextRequestPredicate; declare const isPageRequest: NextRequestPredicate; /** * * @param middlewares the middlewares to chain in sequence * @returns * the chained middlewares as a single Next.js middleware * to export from `middleware.js` * */ declare const chain: (...middlewares: Parameters) => NextMiddleware; /** * * @param matcher * predicate on a NextRequest, whether a middleware chain should run on it * @returns * a matched chain function that will only run chained middlewares on matched requests * @example * import { csp, strictDynamic, chainMatch, isPageRequest } from `@next-safe/middleware` * * const securityMiddlewares = [csp(), strictDynamic()]; * * export default chainMatch(isPageRequest)(...securityMiddlewares); * */ declare const chainMatch: (matcher: ChainMatcher) => MiddlewareChain; declare const chainableMiddleware: (middleware: ChainableMiddleware) => ChainableMiddleware; /** * * @param nextMiddleware * a Next.js middleware, according to spec * @returns * a chainable middleware that continues * the response (if any) of `nextMiddleware` to a chain context */ declare const continued: (nextMiddleware: NextMiddleware) => ChainableMiddleware; interface NextUserAgent { isBot: boolean; ua: string; browser: { name?: string; version?: string; }; device: { model?: string; type?: string; vendor?: string; }; engine: { name?: string; version?: string; }; os: { name?: string; version?: string; }; cpu: { architecture?: string; }; } declare type ConfigInitializerParams = { req: NextRequest; evt: NextFetchEvent; ctx: MiddlewareChainContext; userAgent: NextUserAgent; }; declare type ConfigInitalizer> = (params: ConfigInitializerParams) => Config | Promise; declare type MiddlewareConfig> = Config | ConfigInitalizer; /** * A CSP Directive Poroperty */ declare type CSPDirective = string | string[]; /** * A CSP Config */ declare type CSPConfig = { "base-uri"?: CSPDirective; "child-src"?: CSPDirective; "connect-src"?: CSPDirective; "default-src"?: CSPDirective; "font-src"?: CSPDirective; "form-action"?: CSPDirective; "frame-ancestors"?: CSPDirective; "frame-src"?: CSPDirective; "img-src"?: CSPDirective; "manifest-src"?: CSPDirective; "media-src"?: CSPDirective; "object-src"?: CSPDirective; "prefetch-src"?: CSPDirective; "script-src"?: CSPDirective; "style-src"?: CSPDirective; "worker-src"?: CSPDirective; "block-all-mixed-content"?: CSPDirective; "plugin-types"?: CSPDirective; "navigate-to"?: CSPDirective; "require-sri-for"?: CSPDirective; "require-trusted-types-for"?: CSPDirective; sandbox?: CSPDirective; "script-src-attr"?: CSPDirective; "script-src-elem"?: CSPDirective; "style-src-attr"?: CSPDirective; "style-src-elem"?: CSPDirective; "trusted-types"?: CSPDirective; "upgrade-insecure-requests"?: CSPDirective; "report-to"?: CSPDirective; "report-uri"?: CSPDirective; reportOnly?: boolean; }; declare type HeaderConfig = string | false; declare type PermPolicyDirectiveList = "experimental" | "legacy" | "proposed" | "standard"; /** * nextSafe's primary config object */ declare type NextSafeConfig = { contentTypeOptions?: HeaderConfig; /** * @deprecated to configure a CSP, use the `csp` middleware instead and * and set `disableCsp` to `true`. * * violation reporting cannot be set up properly for both directives * @see https://github.com/trezy/next-safe/issues/41 * */ contentSecurityPolicy?: CSPConfig | false; frameOptions?: HeaderConfig; permissionsPolicy?: { [key: string]: string | false; } | false; permissionsPolicyDirectiveSupport?: PermPolicyDirectiveList[]; isDev?: boolean; referrerPolicy?: HeaderConfig; xssProtection?: HeaderConfig; }; declare type NextSafeCfg = NextSafeConfig & { /** * set this flag to prevent next-safe to set any CSP header. * * For CSPs, use the `csp` middleware instead and set this to `true`. * You can use `nextSafeMiddleware` for other security headers if you need them. * * * @default false */ disableCsp?: boolean; }; /** * @param cfg config object for next-safe https://trezy.gitbook.io/next-safe/usage/configuration * @returns a middleware that adds HTTP response headers the same way next-safe does. * * To configure a CSP, use the `csp` middleware instead and and set `disableCsp` to `true` in cfg, * * next-safe adds CSP legacy headers and set up of reporting could be problematic * @see https://github.com/trezy/next-safe/issues/41 * * You can use the `nextSafe` middleware for other security headers if you need them. * * @example * import { * chainMatch, * isPageRequest, * csp, * nextSafe, * strictDynamic, * } from "@next-safe/middleware"; * * const securityMiddleware = [ * nextSafe({ disableCsp: true }), * csp(), * strictDynamic(), * ]; * * export default chainMatch(isPageRequest)(...securityMiddleware); * */ declare const nextSafeMiddleware: (cfg?: MiddlewareConfig) => ChainableMiddleware; /** * @see https://developers.google.com/web/updates/2018/09/reportingapi#fields */ declare type ReportTo = { group?: string; max_age: number; /** * @see https://developers.google.com/web/updates/2018/09/reportingapi#load */ endpoints: { url: string; priority?: number; weight?: number; }[]; includeSubdomains?: boolean; }; declare type ReportingCSP = { /** endpoint for the `report-uri` directive */ reportUri?: UriPath; /** * group name for the `report-to` directive. * * Must match a group name in the Report-To header * * @default "default" * * @see https://canhas.report/csp-report-to * * Will be ommitted from CSP if no match for this group name is present in the Report-To header. * To unset the `report-to` directive from CSP, set to empty string * */ reportTo?: string | "default"; /** * adds `report-sample` to supported directives * * e.g. if added to script-src, the first 40 characters of a blocked script will be added * to the CSP violation report * * @default true * @see https://csper.io/blog/csp-report-filtering */ reportSample?: boolean; }; declare type ReportingCfg = { /** * object/object array representing valid Report-To header(s) * @see https://developers.google.com/web/updates/2018/09/reportingapi#header */ reportTo?: ReportTo | ReportTo[]; /** * configuration of CSP directives concerned with reporting * @see https://canhas.report/csp-report-to */ csp?: ReportingCSP | false; }; /** * @param cfg a configuration object to set up reporting according to the Reporting API spec * @returns a middleware that sets response headers according to the configured reporting capabilites * @see https://developers.google.com/web/updates/2018/09/reportingapi * * @example * import { * chainMatch, * isPageRequest, * csp, * strictDynamic, * reporting, * } from "@next-safe/middleware"; * * const securityMiddleware = [ * csp(), * strictDynamic(), * reporting({ * csp: { * reportUri: "/api/reporting" * }, * reportTo: { * max_age: 1800, * endpoints: [{ url: "/api/reporting" }], * }, * }), * ]; * * export default chainMatch(isPageRequest)(...securityMiddleware); * */ declare const reporting: (cfg?: MiddlewareConfig) => ChainableMiddleware; declare type SupportInfo = { /** * Whether the browser supports`strict-dynamic`. */ supportsStrictDynamic?: boolean; /** * Whether the browser supports the `integrity` attribute on