Skip to content

Security

rut.ts is built to sit on the path where untrusted input becomes a trusted identity. It is designed specifically to make that path safe. This page explains the threat model and how to use the library defensively.

Found a vulnerability? Please report it privately via the repository's security policy rather than a public issue.

Threat model#

A RUT validator typically runs on attacker-controlled input — form fields, API bodies, query strings. That exposes three concrete risks:

  1. Algorithmic complexity (ReDoS): a naive regex can be driven into catastrophic backtracking by a crafted string, stalling the event loop.
  2. Data leakage: echoing the offending value into an error message leaks a Chilean national ID into logs, traces, and error trackers.
  3. Weak acceptance: treating “looks like a RUT” as “is a valid RUT” lets placeholders and malformed values through.

rut.ts addresses each one directly.

How rut.ts is hardened#

Bounded input parsing#

Every entry point caps input length before any parsing or regex runs, so oversized strings fail fast instead of spending time in the matcher. This neutralizes ReDoS-style abuse.

TypeScript
import { validate } from 'rut.ts'
 
// A megabyte of digits doesn't run the matcher — it's rejected up front.
validate('9'.repeat(1_000_000)) // false, immediately

Strict, canonical validation#

validate() accepts only canonical RUT shapes and rejects ambiguous dot grouping. strict: true additionally rejects repeated-digit placeholders that are technically valid but never real.

TypeScript
import { validate } from 'rut.ts'
 
validate('12.345.678-5')                   // true
validate('12.345678-5')                    // false (ambiguous grouping)
validate('11.111.111-1')                   // true  (valid check digit)
validate('11.111.111-1', { strict: true }) // false (placeholder)

Use validate(input, { strict: true }) as the final acceptance gate for real identities. clean(), decompose(), getBody(), and getVerifier() are permissive normalizers — a non-null result does not mean the verifier digit is correct.

Generic error messages#

Errors are intentionally generic (Invalid RUT input) and never include the offending value, so national ID numbers cannot leak through exception messages into your observability stack.

TypeScript
import { clean } from 'rut.ts'
 
try {
  clean('invalid')
} catch (e) {
  console.error((e as Error).message) // "Invalid RUT input" — no value echoed
}

Options read only from your object#

Since 5.1.2, every function reads its options only from the object you pass and ignores inherited values. If another bug in your app pollutes Object.prototype (for example Object.prototype.requireValid = false), an options object such as {} still means the defaults, so no check gets switched off from outside. Only incremental: true turns on incremental formatting, which skips the Modulo 11 check: a truthy stand-in such as the string 'false' (from a query string or a config file) formats normally, with the check.

Crypto-backed generation#

generate() uses the Web Crypto API when available, produces non-suspicious bodies, and every result passes validate(rut, { strict: true }).

TypeScript
import { generate, validate } from 'rut.ts'
 
validate(generate(), { strict: true }) // always true

count has no upper limit: if it comes from user input, cap it before calling, because a very large count blocks the thread while it runs.

Generated RUTs are for tests, demos, and seed data only. They cover the real RUT range, so any one of them may belong to a real person: never publish one as a sample identity or assign it as a real person's identity.

A safe-by-default acceptance pipeline#

The recommended shape for any endpoint that accepts a RUT from the outside world:

TypeScript
import { validate, clean } from 'rut.ts'
 
export function acceptRut(input: unknown):
  | { ok: true; rut: string }
  | { ok: false } {
  // 1. Type guard — non-strings are invalid, no TypeError thrown.
  if (typeof input !== 'string') return { ok: false }
 
  // 2. Strict acceptance gate: bounded, canonical, verifier-checked,
  //    no placeholders.
  if (!validate(input, { strict: true })) return { ok: false }
 
  // 3. Normalize to one canonical stored shape.
  return { ok: true, rut: clean(input) }
}

Defensive checklist#

1

Validate strictly before you trust

validate(input, { strict: true }) is the gate. Everything else is normalization or display.

2

Use safe mode at boundaries

Pass { throwOnError: false } to clean/format/decompose on untrusted input and branch on null.

3

Never log raw input

Keep error messages generic; do not add the offending value back into your own logs around the call site. A mask()ed value is not anonymous either: treat it as personal data.

4

Store one canonical shape

clean() (or decompose()) after validation so comparisons and lookups are consistent. validate() accepts several spellings of one RUT (dots or not, k or K, surrounding whitespace), so never key on the raw string.

Supply chain#

Releases are published only by CI, from a vX.Y.Z tag on canary, through npm Trusted Publishing (no long-lived npm token exists) and with npm provenance, a Sigstore attestation that links each tarball to its source commit and build. Release tags are not signed. You can verify an installed copy with:

Shell
npm audit signatures

That confirms the attestation is valid, not where it came from. To check the source, open the version on npmjs.com: its provenance must name the arrowsw/rut.ts repository, the .github/workflows/publish.yml workflow and the matching refs/tags/vX.Y.Z.

Maintenance#

rut.ts is actively maintained: a 655-case test suite (18 suites, 100% pass) plus property-based tests, a seeded differential harness that diffs behavior against frozen reference snapshots, and periodic Stryker mutation testing (~94% on the Modulo 11 hot path), with ongoing edge-case and benchmark work. See Testing for how coverage is structured.