Sensitive information reference for Bun
Arcjet Sensitive Information Detection stops clients from sending you data that you don’t want to handle, such as personally identifiable information (PII). The SDK rule runs detection in your own environment with the on-device Rampart named-entity recognition (NER) model. It detects names, addresses, and government or financial identifiers, as well as structured types such as email addresses and card numbers. The text it scans never leaves your environment.
Configuration
Section titled “Configuration”You configure sensitive information detection with a list of entity types to allow or to deny. The two lists are mutually exclusive:
- With
allow, Arcjet denies any detected type that the list doesn’t name. - With
deny, Arcjet denies only the types that the list names.
Add one or more sensitive information rules to the arcjet client. Create them
with sensitiveInfo(...) in JavaScript / TypeScript,
detect_sensitive_info(...) in Python, or
arcjet.SensitiveInfo(arcjet.SensitiveInfoOptions{...}) in Go. The lists are
allow and deny in JS and Python, and Allow and Deny in Go.
Set backend to the Rampart backend:
rampart() in JS and Python, or the Backend that you create with
rampart.New in Go.
Without a backend, the rule runs the analyzer bundled with the SDK, which
detects EMAIL, PHONE_NUMBER, IP_ADDRESS, and CREDIT_CARD_NUMBER only.
Listing any other type without a backend that supports it, or a
custom detect function that identifies it, is a
configuration error.
type SensitiveInfoOptionsAllow = { mode?: "LIVE" | "DRY_RUN"; allow?: Array<ArcjetSensitiveInfoType | string>; // The detection backend. Pass `rampart()` from // `@arcjet/sensitive-info-rampart` (see below). backend?: SensitiveInfoBackend; // Custom detection for the bundled analyzer: one entry per token, the // entity name or `undefined`. Rampart ignores it. detect?: (tokens: string[]) => ReadonlyArray<string | undefined>; // Tokens passed to `detect` at a time (default: 1). contextWindowSize?: number;};type SensitiveInfoOptionsDeny = { mode?: "LIVE" | "DRY_RUN"; deny?: Array<ArcjetSensitiveInfoType | string>; // The detection backend. Pass `rampart()` from // `@arcjet/sensitive-info-rampart` (see below). backend?: SensitiveInfoBackend; // Custom detection for the bundled analyzer: one entry per token, the // entity name or `undefined`. Rampart ignores it. detect?: (tokens: string[]) => ReadonlyArray<string | undefined>; // Tokens passed to `detect` at a time (default: 1). contextWindowSize?: number;};# Signature for arcjet.detect_sensitive_infodef detect_sensitive_info( *, # Required. Mode.LIVE blocks requests; Mode.DRY_RUN logs only. mode: Mode, # Entity types to allow (report only). Mutually exclusive with `deny`. allow: Sequence[str | SensitiveInfoEntityType] = (), # Entity types to deny. Mutually exclusive with `allow`. deny: Sequence[str | SensitiveInfoEntityType] = (), # The detection backend. Pass `rampart()` from # `arcjet_sensitive_info_rampart` (see below). backend: SensitiveInfoBackend | None = None, # Custom detection for the bundled analyzer: one entry per token, the # entity name or None. Rampart ignores it. detect: Callable[[list[str]], Sequence[str | None]] | None = None, # Tokens passed to `detect` at a time (default: 1). context_window_size: int | None = None, # Rate-limit characteristics (see https://docs.arcjet.com/fingerprints) characteristics: Sequence[str] = (),) -> SensitiveInfoDetection: ...
# Entity types are SensitiveInfoEntityType enum members or their string# names, such as SensitiveInfoEntityType.EMAIL or "GIVEN_NAME". See the entity# types table later on this page for the full list.type SensitiveInfoOptions struct { Mode Mode Allow []EntityType // mutually exclusive with Deny Deny []EntityType Backend SensitiveInfoBackend // create with rampart.New // Tokens passed to Config.SensitiveInfoDetect at a time (default: 1). ContextWindowSize int}
// Entity types: SensitiveInfoEmail, SensitiveInfoPhoneNumber,// SensitiveInfoIPAddress, SensitiveInfoCreditCardNumber, SensitiveInfoURL,// SensitiveInfoSSN, SensitiveInfoGivenName, SensitiveInfoSurname,// SensitiveInfoTaxID, SensitiveInfoBankAccount, SensitiveInfoRoutingNumber,// SensitiveInfoGovernmentID, SensitiveInfoPassport,// SensitiveInfoDriversLicense, SensitiveInfoBuildingNumber,// SensitiveInfoStreetName, SensitiveInfoSecondaryAddress, SensitiveInfoCity,// SensitiveInfoState, SensitiveInfoZipCode.// Or deny the whole set with rampart.Entities().type ArcjetSensitiveInfoType = | "EMAIL" | "PHONE_NUMBER" | "IP_ADDRESS" | "CREDIT_CARD_NUMBER" | "GIVEN_NAME" | "SURNAME" | "SSN" | "URL" | "TAX_ID" | "BANK_ACCOUNT" | "ROUTING_NUMBER" | "GOVERNMENT_ID" | "PASSPORT" | "DRIVERS_LICENSE" | "BUILDING_NUMBER" | "STREET_NAME" | "SECONDARY_ADDRESS" | "CITY" | "STATE" | "ZIP_CODE";On-device detection with Rampart
Section titled “On-device detection with Rampart”Sensitive information detection runs on-device. It pairs the Rampart NER model, which understands context, with deterministic recognizers for structured types. Card numbers come from the same card detection as the SDK’s bundled analyzer. The rule detects names, addresses, and government or financial identifiers, as well as email addresses, card numbers, IP addresses, and phone numbers. For the full list, see Entity types.
The model weights ship with the package, so nothing is fetched at runtime and the text you scan stays in your environment. JavaScript and Python run a ~15 MB quantized ONNX model. Go embeds the same ~15 MB of weights in a nested module and runs inference in pure Go, with no cgo or ONNX runtime.
Install the Rampart package alongside your Arcjet SDK. In JavaScript, Rampart
detects card numbers with @arcjet/analyze, which the SDK installs, so it
needs a runtime with WebAssembly. In Python, Rampart ships as the
sensitive-info-rampart extra of the arcjet package. In Go, it’s a nested
module that bundles the model weights:
npm install @arcjet/sensitive-info-rampartpip install "arcjet[sensitive-info-rampart]"go get github.com/arcjet/arcjet-go/sensitiveinfo/rampartThen import rampart and pass it as the rule’s backend. Create the Go
backend once at startup with rampart.New and reuse it. List the entity types
to detect in allow or deny:
import arcjet, { sensitiveInfo } from "@arcjet/bun";import { rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY!, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ mode: "LIVE", // Will block requests, use "DRY_RUN" to log only // Rampart detects names and addresses as well as email addresses // and government identifiers. See the reference for the full list. deny: ["EMAIL", "GIVEN_NAME", "SURNAME", "STREET_NAME", "SSN"], // Run detection on-device with the Rampart NER model. backend: rampart(), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const result of decision.results) { console.log("Rule Result", result);
if (result.reason.isSensitiveInfo()) { console.log("Sensitive info rule", result); } }
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
// Fail closed when Arcjet couldn't complete the check. if (decision.isErrored()) { return new Response("Service unavailable", { status: 503 }); }
return new Response("Hello world"); }),};import arcjet, { sensitiveInfo } from "@arcjet/bun";import { rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ mode: "LIVE", // Will block requests, use "DRY_RUN" to log only // Rampart detects names and addresses as well as email addresses // and government identifiers. See the reference for the full list. deny: ["EMAIL", "GIVEN_NAME", "SURNAME", "STREET_NAME", "SSN"], // Run detection on-device with the Rampart NER model. backend: rampart(), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const result of decision.results) { console.log("Rule Result", result);
if (result.reason.isSensitiveInfo()) { console.log("Sensitive info rule", result); } }
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
// Fail closed when Arcjet couldn't complete the check. if (decision.isErrored()) { return new Response("Service unavailable", { status: 503 }); }
return new Response("Hello world"); }),};To deny every type that Rampart detects, use the full set: rampartEntities in
JS, rampart_entities in Python, or rampart.Entities() in Go:
import { rampart, rampartEntities } from "@arcjet/sensitive-info-rampart";
sensitiveInfo({ mode: "LIVE", deny: rampartEntities, backend: rampart() });from arcjet import Mode, detect_sensitive_infofrom arcjet_sensitive_info_rampart import rampart, rampart_entities
detect_sensitive_info(mode=Mode.LIVE, deny=rampart_entities, backend=rampart())backend, err := rampart.New(rampart.Options{})arcjet.SensitiveInfo(arcjet.SensitiveInfoOptions{ Mode: arcjet.ModeLive, Deny: rampart.Entities(), Backend: backend,})Model accuracy and performance
Section titled “Model accuracy and performance”Rampart is compact enough to run inline on each request: a ~14.7 MB
artifact with ~18.5M parameters, quantized to 4-bit weights, and a 512-token
context window. Longer text is scanned in overlapping windows. On the Node.js
CPU runtime, median (p50) inference is ~6.6 ms, and ~3.9 ms on
WebGPU (set device: "webgpu" in the JS SDK). The Python backend runs on
ONNX Runtime and uses the CPU by default. To use a
different accelerator, set the providers option. In Go, create the backend
once with rampart.New and reuse it.
On the model author’s held-out benchmark, Rampart recalls ~98% of private terms across the seven Latin-script languages it supports: English, Spanish, French, German, Italian, Portuguese, and Dutch.
Accuracy depends on the input. Keep the following trade-offs in mind:
- Non-Latin scripts (for example Chinese, Japanese, Korean, Arabic, Hindi, or Cyrillic) have much lower recall – the model is trained on Latin-script text.
- Government or financial identifiers without a checksum (passports, licenses, case numbers) are recognized less reliably than the structured types the deterministic checks validate, such as emails and card numbers.
- Latency – inference runs synchronously in the request path, so it adds to the response time of each request that the rule scans.
These figures come from the Rampart model card, which has the full per-language breakdown, calibration data, and limitations.
Rampart options
Section titled “Rampart options”The rampart() factory (JS and Python) or rampart.New (Go) accepts these
options:
import { defaultRecognizers, rampart } from "@arcjet/sensitive-info-rampart";
rampart({ // Execution device (default: "cpu"). Set to "webgpu" to use a GPU when the // runtime supports it. device: "webgpu", // Minimum confidence score for a model token to count (default: 0.5). threshold: 0.6, // Largest input to scan, in characters (default: 100_000). The rule denies // longer input without scanning it. Lower it to bound latency, or raise it // to accept longer text. maxInputChars: 100_000, // Deterministic recognizers to run alongside the model (default: // `defaultRecognizers`). This is the extension point for custom detection – // add a recognizer that returns matched spans. Pass `[]` to turn off the // recognizers. The model and card detection still run. recognizers: [ ...defaultRecognizers, (value) => { // Return an array of `{ start, end, type }` spans. return []; }, ],});The rampart() factory takes an optional RampartOptions:
from arcjet_sensitive_info_rampart import ( RampartOptions, default_recognizers, rampart,)
rampart( RampartOptions( # Minimum confidence score for a model token to count (default: 0.5). threshold=0.6, # ONNX Runtime execution providers (default: ("CPUExecutionProvider",)). # Set this to use a GPU or other accelerator supported by your ONNX # Runtime install. providers=("CPUExecutionProvider",), # Largest input to scan, in characters (default: 100,000). The rule # denies longer input without scanning it. Lower it to bound latency, # or raise it to accept longer text. max_input_chars=100_000, # Deterministic recognizers to run alongside the model (default: # `default_recognizers`). This is the extension point for custom # detection – add a recognizer that returns matched spans. Pass () to # turn off the recognizers. The model and card detection still run. recognizers=( *default_recognizers, lambda value: [], # Return a list of DetectedSpan(start, end, type). ), # Directory containing the model files (default: the bundled models). model_path=None, ))rampart.New takes rampart.Options. Inference is pure Go (no cgo). There is
no device or ONNX provider option:
backend, err := rampart.New(rampart.Options{ // Minimum confidence score for a model token to count (default: 0.5). Threshold: 0.6, // Largest input to scan, in characters (default: 4096). The rule denies // longer input without scanning it. Raise it to accept longer text if you // can afford the extra latency. MaxInputChars: 4096, // Deterministic recognizers run alongside the model (default: // rampart.DefaultRecognizers). Pass an empty non-nil slice to turn off // the recognizers. The model and card detection still run. Phone numbers // are left to the model because their digit shape overlaps with financial // and government identifiers. // Recognizers: []rampart.Recognizer{},})Long input
Section titled “Long input”Rampart scans text on the request path, so longer text takes longer to scan. To keep that cost predictable, each SDK sets a maximum length, which you can change:
| SDK | Default limit | Option |
|---|---|---|
| JavaScript | 100,000 characters | maxInputChars |
| Python | 100,000 characters | max_input_chars |
| Go | 4,096 characters | MaxInputChars |
Go’s default is lower because the Go backend runs the model in pure Go, with no native runtime to install, and each scan takes several times longer than with the ONNX Runtime that JavaScript and Python use.
A live rule denies text longer than the limit without scanning any of it, so
nothing past the limit goes through unchecked. A rule in dry run mode doesn’t
block: with protect() the SDK logs a warning, and with Guards Arcjet records
the denial. The limit applies to protect(), to the local
sensitive information rule in Arcjet Guards, and to a LOCAL input in an
agent guard policy. JavaScript counts UTF-16 code units, and Python and Go
count Unicode code points.
With protect(), the denial has an error reason instead of a sensitive
information reason, because Arcjet didn’t scan the text. A scan that fails
part way after finding a denied entity is also a denial with an error reason;
for more information, see Error handling. The examples on
this page block both with their catch-all denial. To return a different
response for them, check for the error reason: decision.reason.isError() in
JavaScript, decision.reason_v2.type == "ERROR" in Python, or
decision.Reason.IsError() in Go:
if (decision.isDenied() && decision.reason.isError()) { // Arcjet didn't complete a scan: the text is longer than the backend's // scan limit, or the scan failed after finding a denied entity. return Response.json({ error: "Message not accepted" }, { status: 400 });}With Guards, the rule result is a denial with no detected entity types.
Choose a limit that fits your latency budget. Each window of about 480 characters is one inference pass, so a higher limit raises the worst-case scan time for a request. The limit must be a positive whole number, and there’s no setting that turns it off. Also cap the request body size in your server or framework, so your application doesn’t read an oversized body into memory.
On-device detection suits typical request fields. For longer text, you have the following options:
- Raise the limit when the extra latency fits your budget.
- Use a server input. Send the text as a
SERVERinput to the server-side sensitive information detector in an agent guard policy, which screens values up to about 100 KB on Arcjet’s servers. Arcjet allows up to 3 seconds to screen a value over 2 KB, and a live rule fails closed if the scan doesn’t finish. Set the client timeout above 3 seconds (the default is 2), so the client doesn’t stop waiting first. Deny the request when the conclusion isDENY, which is what a client timeout returns in JS SDK 1.17.0, Python SDK 1.7.0, Go SDK 1.5.0, and later. In earlier releases, a client timeout fails open, so also deny whenhasFailedOpen()/has_failed_open()/HasFailedOpen()is true. - Split the text into parts, for text longer than about 100 KB, and
screen each part in its own
guard()call. Each call screens up to twoSERVERinputs longer than 2 KB.
To detect protected health information (PHI), always use the server-side detector. On-device detection doesn’t detect the PHI types. For more information, see Detect protected health information (PHI).
In JavaScript and Python, the model loads on first use and is reused for every
later request. In Go, rampart.New loads and dequantizes the weights, so
create the backend once at startup and reuse it. It’s safe for concurrent use.
To detect your own entity types, add a recognizer. See Custom entity detection.
Bundlers and runtimes
Section titled “Bundlers and runtimes”In JavaScript and Python, the Rampart backend loads a native ONNX runtime and reads its model weights from disk at runtime. This affects where it runs and how you build your application:
- It requires a server runtime with filesystem and native-addon access. In JS that means Node.js, Bun, or Deno – it does not run on edge runtimes. In Python it runs anywhere the SDK does (Python 3.11+).
- A server build must not bundle it, or the runtime fails to locate the model weights.
In Go, inference is pure Go. The weights are compiled into the nested module, so there’s no ONNX runtime, native addon, or extra file for a bundler to miss. An application that imports the module adds about 15 MB of weights to its binary.
In Python, the backend depends on
ONNX Runtime (onnxruntime), which the
arcjet[sensitive-info-rampart] extra installs. Inference is synchronous, so
it adds to the response time of each request that the rule scans, with either
the arcjet() or the arcjet_sync() client.
For Next.js, mark the package and its native dependencies as
server external packages,
so that Next.js loads them from node_modules at runtime instead of bundling
them. Run each route handler that uses the backend on the Node.js runtime, which
is the default, rather than the Edge runtime:
/** @type {import('next').NextConfig} */const nextConfig = { serverExternalPackages: [ "@arcjet/sensitive-info-rampart", "@huggingface/transformers", "onnxruntime-node", ],};
module.exports = nextConfig;export const runtime = "nodejs";Decision
Section titled “Decision”To evaluate your rules, call the protect function.
The function returns a Promise that resolves to an ArcjetDecision object
with the following properties:
id(string) – The unique ID for the request. Use it to look up the request in the Arcjet Console. Decisions that involve the Arcjet cloud API have the prefixreq_, and decisions made locally have the prefixlreq_.conclusion(ArcjetConclusion) – The overall conclusion from evaluating every configured rule. To follow Arcjet’s recommended action, use this property.reason(ArcjetReason) – More detail about the conclusion.results(ArcjetRuleResult[]) – The result of each rule that ran.ip(ArcjetIpDetails) – Arcjet’s analysis of the client IP address. For more information, see the SDK reference.
To check whether a sensitive information rule denied the request, use
decision.isDenied() and decision.reason.isSensitiveInfo() (JS) /
decision.is_denied() and decision.reason_v2.type == "SENSITIVE_INFO"
(Python) / decision.IsDenied() and decision.Reason.IsSensitiveInfo() (Go).
To see what each rule found, iterate through the results:
for (const result of decision.results) { console.log("Rule Result", result);}for result in decision.results: print("Rule Result", result)for _, result := range decision.Results { log.Printf("Rule Result %+v", result)}The following example logs each result and the sensitive information rule:
import arcjet, { sensitiveInfo } from "@arcjet/bun";import { rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY!, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ deny: ["EMAIL"], mode: "LIVE", backend: rampart(), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const result of decision.results) { console.log("Rule Result", result);
if (result.reason.isSensitiveInfo()) { console.log("Sensitive info rule", result); } }
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
return new Response("Hello world"); }),};import arcjet, { sensitiveInfo } from "@arcjet/bun";import { rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ deny: ["EMAIL"], mode: "LIVE", backend: rampart(), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const result of decision.results) { console.log("Rule Result", result);
if (result.reason.isSensitiveInfo()) { console.log("Sensitive info rule", result); } }
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
return new Response("Hello world"); }),};Entity types
Section titled “Entity types”For every entity type that Arcjet detects, grouped by category with its
taxonomy version and where Arcjet detects it, see
Sensitive information entity types.
The SDK detects the version 1 types with Rampart. List any of them in allow or
deny. Each one is an ArcjetSensitiveInfoType in JS, a
SensitiveInfoEntityType in Python, and an arcjet.EntityType in Go.
To list every type at once, use rampartEntities (JS), rampart_entities
(Python), or rampart.Entities() (Go).
Structured type matching
Section titled “Structured type matching”The default recognizers match these structured types with deterministic, validated patterns. Card numbers use Arcjet’s own card detection instead of a recognizer.
Card numbers
Section titled “Card numbers”Card numbers come from Arcjet’s card detection, the same definition the default
backend and the Arcjet server use, whatever recognizers you configure. Arcjet
detects card numbers of 13 to 19 digits from a known card issuer, at that
issuer’s lengths, when the final digit is a valid
Luhn check digit. The values
4242424242424242, 4000 0566 5566 5556, and 3782-8224-6310005 match, but
4242424242424241 doesn’t. The model’s own card labels aren’t used.
Arcjet detects the following issuers:
| Issuer | Prefixes | Lengths |
|---|---|---|
| Visa | 4 | 13, 16, 19 |
| Mastercard | 51–55, 2221–2720 | 16 |
| American Express | 34, 37 | 15 |
| Discover | 6011, 644–649, 65 | 16 to 19 |
| JCB | 3528–3589 | 16 to 19 |
| Diners Club | 300–305, 309, 36, 38, 39 | 14 to 19 |
| UnionPay | 62, 8100–8171 | 16 to 19 |
| Mir | 2200–2204 | 16 to 19 |
| Maestro | 50, 56–69 | 16 to 19 |
Maestro also issues 12 to 15 digit numbers, but at those lengths its prefixes match many tax IDs, national IDs, and phone numbers, so Arcjet doesn’t detect them.
People often split a card number into groups. Arcjet accepts groups separated
by up to three whitespace or dash characters, including Unicode spaces and
dashes, and groups separated by single dots, such as 4111.1111.1111.1111.
Arcjet also finds a card inside a longer run of numbers, such as a card after
an order number. To keep unrelated numbers from forming a card, a card inside a
longer run must be one unbroken group of digits or use a printed card layout,
such as 4-4-4-4. Four groups that read as a screen size, such as
360 640 1080 1920, aren’t a card.
Email addresses
Section titled “Email addresses”The values alice@example.com and bob.smith@subdomain.example.com match, but
alice.example.com and alice@localhost don’t, because the domain needs a
dot.
IP addresses
Section titled “IP addresses”Arcjet detects IPv4 and IPv6 addresses. The values 127.0.0.1 and ::1 match.
Social Security numbers
Section titled “Social Security numbers”Arcjet detects US Social Security numbers in the dashed form, such as
123-45-6789.
Arcjet detects URLs that start with http://, https://, or www.. The model
also detects URLs from context.
Labeled fields
Section titled “Labeled fields”The default recognizers also match the following labeled fields:
- Bank account numbers: 6 to 17 digits after
Bank account:orAccount number:, as inAccount number: 12345678. - Routing numbers: 9 digits with a valid ABA check digit after
Routing number:, as inRouting number: 021000021. - Secondary addresses: a unit after
Apartment,Apt,Suite,Ste, orUnit, as inSuite 400. - US states: a state name after
State:, as inState: New York.
For bank accounts, routing numbers, and states, Arcjet reports the value
without its label. The label can end with :, #, or is, except for
states, which need :. The model detects these types without a label from
context.
Phone numbers
Section titled “Phone numbers”The Rampart model detects phone numbers from context. The default recognizers don’t include a phone number pattern, because a run of digits can also be a bank account, routing number, or government identifier, and a pattern can’t tell which one it is.
Custom entity detection
Section titled “Custom entity detection”To detect patterns that the default recognizers don’t cover, add your own
recognizer to the Rampart recognizers option. A recognizer receives the full
text and returns the spans it matched, each with a start offset, an end offset,
and an entity type. Recognizer results take precedence over the model where
they overlap.
Keep the default recognizers in the list unless you want to replace them. The
following example adds a recognizer that reports IBANs as BANK_ACCOUNT:
import arcjet, { sensitiveInfo } from "@arcjet/bun";import { defaultRecognizers, rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
// A recognizer receives the full text and returns the spans it matched. This// one reports IBANs as bank account numbers.function detectIban(value) { const spans = []; for (const match of value.matchAll(/\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b/g)) { spans.push({ start: match.index, end: match.index + match[0].length, type: "BANK_ACCOUNT", }); } return spans;}
const aj = arcjet({ key: env.ARCJET_KEY, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ deny: ["EMAIL", "BANK_ACCOUNT"], mode: "LIVE", backend: rampart({ // Keep the default recognizers and add your own. recognizers: [...defaultRecognizers, detectIban], }), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const result of decision.results) { console.log("Rule Result", result);
if (result.reason.isSensitiveInfo()) { console.log("Sensitive info rule", result); } }
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
return new Response("Hello world"); }),};import arcjet, { sensitiveInfo } from "@arcjet/bun";import { defaultRecognizers, type DetectedSpan, rampart,} from "@arcjet/sensitive-info-rampart";import { env } from "bun";
// A recognizer receives the full text and returns the spans it matched. This// one reports IBANs as bank account numbers.function detectIban(value: string): DetectedSpan[] { const spans: DetectedSpan[] = []; for (const match of value.matchAll(/\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b/g)) { spans.push({ start: match.index, end: match.index + match[0].length, type: "BANK_ACCOUNT", }); } return spans;}
const aj = arcjet({ key: env.ARCJET_KEY!, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ deny: ["EMAIL", "BANK_ACCOUNT"], mode: "LIVE", backend: rampart({ // Keep the default recognizers and add your own. recognizers: [...defaultRecognizers, detectIban], }), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const result of decision.results) { console.log("Rule Result", result);
if (result.reason.isSensitiveInfo()) { console.log("Sensitive info rule", result); } }
if (decision.isDenied()) { return new Response("Forbidden", { status: 403 }); }
return new Response("Hello world"); }),};Offsets are UTF-16 code unit indexes in JS, code point indexes in Python, and byte offsets in Go – the indexes each language’s regular expression matches return.
Without a backend, the bundled analyzer can call a custom detect function
instead: detect in JS and Python, or Config.SensitiveInfoDetect on the Go
client. It receives the text split into tokens and returns one entry per token,
in the same order: the entity name, or undefined (JS), None (Python), or
an empty string (Go) for a token that isn’t sensitive. Set contextWindowSize
(JS), context_window_size (Python), or ContextWindowSize (Go) to pass more
than one token at a time. A detect function can report names that aren’t
built-in types, and you list those names in allow or deny. Rampart doesn’t
call detect.
Error handling
Section titled “Error handling”By default, Arcjet fails open, so a service issue or misconfiguration doesn’t block your users. The SDK times out and fails open after 2,000 ms. Most responses take 20 ms to 30 ms.
If a rule hits an error, Arcjet returns an ERROR result for that rule. The
message property on the result describes the error. If every other rule that
ran returned ALLOW, the overall conclusion is ERROR. In JavaScript and Go,
an ERROR decision counts as allowed. In Python, an ERROR decision is
neither is_allowed() nor is_denied().
The text you scan never leaves your environment, so the Arcjet cloud API can’t
detect what a failed local scan missed. When the backend fails, the rule result
is an ERROR. Its message doesn’t include the backend’s own error message,
which can include the scanned text. If the scan
fails after it found a denied entity, a live rule denies the request with an
error reason instead. A rule in dry run mode lists the failure in the results
and doesn’t change the decision. With Guards, a local sensitive information rule
whose scan fails after it found a denied entity reports a denial, and one whose
scan fails without finding one reports an error.
Sensitive information detection protects data, so deny the request when the decision is errored, unless your application can accept data that Arcjet didn’t screen. The following example logs each error and fails closed:
import arcjet, { sensitiveInfo } from "@arcjet/bun";import { rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY!, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ deny: ["EMAIL"], mode: "LIVE", backend: rampart(), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const { reason } of decision.results) { if (reason.isError()) { console.warn("Arcjet error", reason.message); } }
if (decision.isErrored()) { // Arcjet couldn't screen the text, so fail closed. return new Response("Service unavailable", { status: 503 }); }
if (decision.isDenied()) { return new Response( "Your request contained unexpected sensitive information!", { status: 400 }, ); }
return new Response("Hello world"); }),};import arcjet, { sensitiveInfo } from "@arcjet/bun";import { rampart } from "@arcjet/sensitive-info-rampart";import { env } from "bun";
const aj = arcjet({ key: env.ARCJET_KEY, // Get your Arcjet key from https://console.arcjet.com rules: [ sensitiveInfo({ deny: ["EMAIL"], mode: "LIVE", backend: rampart(), }), ],});
export default { port: 3000, fetch: aj.handler(async (req) => { const decision = await aj.protect(req, { sensitiveInfoValue: await req.text(), });
for (const { reason } of decision.results) { if (reason.isError()) { console.warn("Arcjet error", reason.message); } }
if (decision.isErrored()) { // Arcjet couldn't screen the text, so fail closed. return new Response("Service unavailable", { status: 503 }); }
if (decision.isDenied()) { return new Response( "Your request contained unexpected sensitive information!", { status: 400 }, ); }
return new Response("Hello world"); }),};Testing
Section titled “Testing”Arcjet runs the same way in every environment, including locally and in CI. To
log the results of your rules without blocking any requests, set mode to
DRY_RUN.
You can test your rules automatically with a test framework, or with a sample of your traffic. For more information, see Testing.