Guard Mime

Media-type identifier-safety guard. Validates user-supplied RFC 6838 media-type strings destined for Accept-shape comparison, content-type allowlists, and dispatch routing. KIND="identifier" — the gate consumes ctx.identifier (or ctx.mime).

Threat catalog: shape malformation (not RFC 6838 type/subtype grammar); bad token characters (RFC 6838 §4.2 restricts type and subtype to ALPHA / DIGIT / !#$&-^_.+ — spaces / quotes / Unicode reject); parameter injection through pass-through text/plain; charset=... shapes; wildcard *‍/‍* / type/* (Accept-only — refused as content-type at strict); vendor tree application/vnd. and personal tree application/prs.* plus unregistered x.* flagged so operators audit the namespace; risky types refuse list (application/x-msdownload, .x-msdos-program, .x-sh, .x-csh, application/javascript, text/javascript) when handed off to a script-host; BIDI / zero-width / C0-control / null-byte universal-refuse.

Magic-byte verification and polyglot rejection are performed by the operator-side fixture pipeline: the gate emits the asserted identifier; downstream content guards (b.guardSvg / b.guardPdf / b.guardImage) compare it against inspectMagic(buffer) and refuse mismatches.

Profiles: strict / balanced / permissive. Compliance postures: hipaa / pci-dss / gdpr / soc2.

b.guardMime.validate(input, opts?) #

stable0.7.47hipaapci-dssgdprsoc2
{
  profile:                "strict"|"balanced"|"permissive",
  compliancePosture: "hipaa"|"pci-dss"|"gdpr"|"soc2",
  bidiPolicy:             "reject"|"strip"|"audit"|"allow",
  controlPolicy:          "reject"|"strip"|"allow",
  nullBytePolicy:         "reject"|"strip"|"allow",
  zeroWidthPolicy:        "reject"|"strip"|"allow",
  wildcardPolicy:         "reject"|"audit"|"allow",
  vendorTreePolicy:       "reject"|"audit"|"allow",
  personalTreePolicy:     "reject"|"audit"|"allow",
  unregisteredTreePolicy: "reject"|"audit"|"allow",
  riskyTypesPolicy:       "reject"|"audit"|"allow",
  parameterPolicy:        "reject"|"audit"|"allow",
  maxBytes:               number,    // default 256 (RFC-recommended cap)
}

Inspect a media-type string against the resolved profile and return { ok, issues }. Each issue carries kind / severity (critical | high | medium | low) / ruleId / snippet. Non-string input returns a single mime.bad-input issue rather than throwing — callers that prefer an exception use b.guardMime.sanitize.

var rv = b.guardMime.validate("application/json", { profile: "strict" });
rv.ok;                                             // → true
rv.issues.length;                                  // → 0

var bad = b.guardMime.validate("application/x-msdownload", { profile: "strict" });
bad.ok;                                            // → false
bad.issues[0].ruleId;                              // → "mime.risky-type"

b.guardMime.sanitize(input, opts?) #

stable0.7.47
{
  profile:                "strict"|"balanced"|"permissive",
  compliancePosture: "hipaa"|"pci-dss"|"gdpr"|"soc2",
  ...:                    same shape as b.guardMime.validate opts,
}

Lower-case the canonical type/subtype while preserving parameter-value case (some parameter values are case-significant — e.g. multipart boundary tokens). Throws GuardMimeError when any critical or high issue fires (risky-type, parameter-injection, BIDI / null-byte / control). Use validate to inspect issues without throwing.

var safe = b.guardMime.sanitize("Application/JSON; charset=UTF-8",
                                { profile: "balanced" });
safe;                                              // → "application/json; charset=UTF-8"

try {
  b.guardMime.sanitize("application/javascript", { profile: "strict" });
} catch (e) {
  e.code;                                          // → "mime.risky-type"
}

b.guardMime.compliancePosture(name) #

stable0.7.47hipaapci-dssgdprsoc2

Look up a compliance-posture overlay by name (one of "hipaa" / "pci-dss" / "gdpr" / "soc2"). Returns a fresh clone of the posture overlay so the caller may mutate it freely without disturbing the shared table. Throws GuardMimeError with code "mime.bad-posture" when the name is not one this guard maps. Wired by gateContract.defineGuard through gateContract.lookupCompliancePosture, so the clone semantics and error code are identical across every guard in the family.

var posture = b.guardMime.compliancePosture("hipaa");
posture;                                             // → overlay clone (mutable)

try {
  b.guardMime.compliancePosture("not-a-regime");
} catch (e) {
  e.code;                                            // → "mime.bad-posture"
}

b.guardMime.buildProfile(opts) #

stable0.7.47
{
  extends:   string|string[],   // base profile name(s) to compose
  ...:       any guard key,      // inline override of resolved keys
}

Compose a derived profile from one or more named bases plus inline overrides, resolving names through this guard's own PROFILES table. opts.extends is a base profile name ("strict" / "balanced" / "permissive") or an array of names — later entries shadow earlier ones, and inline opts keys win last. Wired by gateContract.defineGuard through gateContract.makeProfileBuilder, so operator-defined profiles stay traceable to a baseline instead of a hand-typed dictionary.

var custom = b.guardMime.buildProfile({ extends: "strict" });
custom;                                              // → composed profile object

b.guardMime.loadRulePack(pack) #

stable0.7.47

Register an operator-supplied rule pack with this guard's rule-pack registry. The pack is identified by pack.id (a non-empty string) and stored for later dispatch by gates that opt in via opts.rulePackId. Returns the pack unchanged on success; throws GuardMimeError with code "mime.bad-opt" when pack is missing or pack.id is not a non-empty string. Wired by gateContract.defineGuard through gateContract.makeRulePackLoader, so storage shape and validation are identical across the family.

var pack = b.guardMime.loadRulePack({ id: "tenant-policy", rules: [] });
pack.id;                                             // → "tenant-policy"

b.guardMime.gate(opts?) #

stable0.7.47
{
  profile:           string,    // one of PROFILES; default this guard's default
  compliancePosture: string,    // overlay one of hipaa/pci-dss/gdpr/soc2
  mode:              string,    // one of gateContract MODES; default "enforce"
}

Build the guard's request-boundary gate — a contract-shaped object exposing check(ctx) that host primitives call at their byte moment. This is the factory default chain: serve when no issue, audit-only for info / warn issues, and refuse for any high / critical issue, dispatched to the right ctx field by the guard's KIND. Wired by gateContract.defineGuard through gateContract.buildGuardGate; a guard whose gate diverges (a bespoke sanitize-and-reserialize chain, for example) ships its own gate block instead of this template.

var gate = b.guardMime.gate({ profile: "strict" });
var decision = await gate.check({ bytes: Buffer.from("...") });
decision.action;                                     // → "serve" | "refuse" | …

b.guardMime.resolveOpts(opts?) #

stable0.7.47
{
  profile:           string,    // one of PROFILES; default this guard's default
  compliancePosture: string,    // overlay one of hipaa/pci-dss/gdpr/soc2
}

Resolve caller opts against this guard's PROFILES + compliance-posture overlays into the fully-defaulted option set the guard runs on — the same resolution validate / sanitize / gate apply internally. Wired by gateContract.defineGuard from the guard's binding config (profiles / postures / defaults / error class), so a guard's bespoke gate calls resolveOpts instead of re-declaring the per-guard resolver wrapper. Throws GuardMimeError with code "mime.bad-opt" / "mime.bad-posture" on an unknown profile or posture name.

var resolved = b.guardMime.resolveOpts({ profile: "strict" });
resolved.profile;                                    // → "strict"

Last updated 2026-08-08T16:39:49.652Z by seeder.