UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

90 lines (89 loc) • 5.04 kB
/** * Inbound trust for the proxy listeners and the SDK-mounted proxy routes. * * Both pool engines spend the operator's subscriptions on behalf of whoever * reaches the port. On loopback that is the operator; anywhere else it is * anyone. This module holds the three decisions that keep that honest: * * - **Bind policy.** A listener may bind off-loopback only when something * authenticates callers: the share gate, an inbound token, or the operator's * explicit `--allow-unauthenticated`. * - **Inbound token.** `NEUROLINK_PROXY_INBOUND_TOKEN` (or `--inbound-token`) * is a bearer every caller must present, compared in constant time. A share * token is an alternative credential, judged by the share gate. * - **Operator-only surfaces.** `/accounts`, `/limits` and `/status` name the * pool's accounts; they are refused to borrowers and, when a token is * configured, to anyone without it. * * @module proxy/inboundAuth */ import type { ProxyBindPolicyDecision, ProxyBindPolicyInput, ProxyInboundAuthOptions, ProxyShareRefusalResponse } from "../types/index.js"; /** Dedicated header, so the inbound token never collides with a client credential. */ export declare const INBOUND_TOKEN_HEADER = "x-neurolink-inbound-token"; /** Set on every refusal so a client can tell "token required" from "bad request". */ export declare const INBOUND_AUTH_HEADER = "x-neurolink-inbound-auth"; export declare const INBOUND_TOKEN_ENV = "NEUROLINK_PROXY_INBOUND_TOKEN"; export declare const ALLOW_UNAUTHENTICATED_ENV = "NEUROLINK_PROXY_ALLOW_UNAUTHENTICATED"; /** * Compare two secrets without leaking where they diverge. Hand-rolled rather * than `crypto.timingSafeEqual` so the proxy routes stay loadable from a build * whose `node:crypto` stub does not carry it. */ export declare function secretsMatch(expected: string, presented: string): boolean; /** The inbound token in force: an explicit value wins over the environment. */ export declare function resolveInboundToken(explicit?: string): string | undefined; /** `NEUROLINK_PROXY_ALLOW_UNAUTHENTICATED` as a boolean. */ export declare function isUnauthenticatedBindAllowedByEnv(): boolean; /** Loopback addresses in every spelling a bind flag accepts. */ export declare function isLoopbackHost(host: string): boolean; /** * May the proxy bind to `host`? * * Loopback always may. Anything else needs the share gate, an inbound token, * or the operator's explicit acknowledgement — and the acknowledgement still * earns a warning, because the subscriptions are then one port scan away. */ export declare function evaluateProxyBindPolicy(input: ProxyBindPolicyInput): ProxyBindPolicyDecision; /** True when the request presents the configured inbound token. */ export declare function hasValidInboundToken(headers: Record<string, string | undefined>, token: string | undefined): boolean; /** The 401 an unauthenticated caller receives, in the Anthropic error shape. */ export declare function buildInboundAuthRefusal(): ProxyShareRefusalResponse; /** * Decide an inbound request against the configured token. * * A request carrying a share token is handed to the share gate instead: a * borrower holds a grant, not the operator's token, and the gate is what * judges a grant. Everything else must present the token. */ export declare function authorizeInboundRequest(headers: Record<string, string | undefined>, token: string | undefined): { ok: true; } | { ok: false; response: ProxyShareRefusalResponse; }; /** `Response` form of the refusal, for route factories whose adapters pass one through. */ export declare function inboundAuthRefusalResponse(): Response; /** * Say, loudly and once per engine, that a proxy route group was mounted with * nothing authenticating its callers. `logger.always` on purpose: the ordinary * warn level is invisible outside debug runs, and an open proxy is exactly the * thing an operator must not be able to miss. Suppressed when the caller * recorded the decision (`allowUnauthenticated`), which the CLI does after its * own bind check. */ export declare function warnIfProxyRoutesUnauthenticated(engine: string, options: ProxyInboundAuthOptions | undefined, token: string | undefined): void; /** Test isolation only. */ export declare function resetInboundAuthWarningsForTests(): void; /** Paths that stay open on an authenticated listener: liveness and self-authenticating peers. */ export declare function isInboundAuthExemptPath(path: string): boolean; /** * How many pooled accounts one inbound request may be tried against. * * Without a cap a single request fans out to every account in the pool — N * upstream posts, N× quota — on any rotation trigger. Default 3; * `NEUROLINK_PROXY_MAX_ACCOUNTS_PER_REQUEST=0` restores the unbounded * behaviour. */ export declare const MAX_ACCOUNTS_PER_REQUEST_ENV = "NEUROLINK_PROXY_MAX_ACCOUNTS_PER_REQUEST"; export declare const DEFAULT_MAX_ACCOUNTS_PER_REQUEST = 3; export declare function resolveMaxAccountsPerRequest(): number;