README
¶
rules/
Scope (ADR 0083): this rule format and its matcher are the TypeScript provider's detection mechanism, not codefit's cross-language architecture. Each language owns its detector; Go, for example, uses hand-written
go/astanalysis and has no rule files.
Declarative detection rules in a subset of the Semgrep rule format (see PRD
section 17). These are codefit's deterministic security/best-practice rules:
versioned with the binary, contributed by the community without writing Go, and
matched by codefit's own pure-Go matcher (internal/core/ruleengine) over the
provider's AST — the OCaml Semgrep/OpenGrep engine is not embedded.
Layout (per language):
rules/
go/ # rules for the Go provider
typescript/ # rules for the TypeScript provider (Fase 1)
...
Supported operators (core subset): pattern, pattern-either, pattern-not,
pattern-inside, metavariables ($VAR), metavariable-regex,
metavariable-name, metavariable-value-veto. No mode: taint / pattern-sources/sinks/sanitizers — that role is covered by the
agent reasoning over mapped surface.
An unknown key is a load error. The loader decodes rule files strictly, at
every level: a misspelled operator such as metavariable-regx: fails the load
naming the file, the line and the key. It used to be skipped in silence, and the
rule simply lost the constraint it was written to carry.
metavariable-name: match an identifier by name COMPONENT
Not a Semgrep operator — codefit's own, and it exists because a regex cannot do the job.
metavariable-name:
$NAME: credential
$NAME matches only if the identifier carries a member of the named vocabulary
as a name component: accessToken and API_KEY fire, tokenizer and
subtokenizer do not.
Why not a regex. SEC-001 used an unanchored metavariable-regex for $NAME,
so any identifier merely containing a credential word was affirmed at certainty
1.0 — const tokenizer = "whitespace" was reported as a hardcoded secret. Fixing
it needs the word anchored to a component boundary, and Go's regexp is RE2: no
lookbehind, no lookahead. Asserting what precedes token is exactly what RE2
cannot express, and enumerating the case variants multiplies every alternative by
every boundary.
The vocabularies are a closed set — today only credential, which is
namematch.Credential(), the same words Go's name gate consumes. An unknown
vocabulary is a compile error, because a rule that named one and matched
nothing would tell nobody. Adding a vocabulary means adding it to
metavarVocabularies in internal/core/ruleengine/engine.go.
It composes. A metavariable may carry a name constraint and a regex at once;
both must hold. And like metavariable-regex, it is consulted at bind time, so
it steers the object-subset search rather than filtering its first answer.
metavariable-value-veto: drop a match whose value proves it is something else
Also codefit's own, and it exists for the same reason metavariable-name does:
the question cannot be asked in a regex.
metavariable-value-veto:
$VALUE: not-a-credential
The match is dropped when the source text bound to $VALUE is one
JavaScript string literal whose decoded value the named predicate rejects.
not-a-credential is valueshape.NotACredential (internal/core/valueshape),
the same function the Go provider calls: a URL with no secret in it, a URN, an
environment-variable name, a path ending in a known file extension (ADR 0086).
- The literal is decoded first, as JavaScript. Single and double quotes,
\nand the other single-letter escapes,\0,\xHH,\uHHHH(surrogate pairs joined),\u{…}, line continuations and identity escapes such as\/. Decoding is not cosmetic:"…?api_key\x3dabc"denotes a URL carryingapi_key=abc, and reading the characters between the quotes would see a parameter with no value and veto a credential. - The decoder only sees what the rule lets through. A
metavariable-regexon the same metavariable is checked as well, and in RE2 the dot does not match a line feed: a rule that writes.+between the quotes never matches a literal holding a line feed, so a line continuation that ends in LF or CR LF is never decoded and the rule is silent on it whatever it holds. (A continuation that ends in a bare CR, U+2028 or U+2029 did match: the dot admits those.) SEC-001's regex therefore writes the middle as(?:.|\\\r?\n)+, which admits a line feed only right after a backslash. The regex does not know whether that backslash is inside a literal: a line comment that ends in one admits the line feed too, so a concatenation broken across lines right after such a comment is bound, refused by the decoder as not one literal, and reported. - What does not decode proves nothing, and the match is kept. A
concatenation (
"a" + "b"), a legacy octal escape (\1), a lone surrogate, a malformed escape. Every failure falls toward "keeps firing". - It runs at bind time, like the other two metavariable constraints, so it
steers the object search: in
{secretFile: "lib/config.ts", apiKey: "s3cr3t"}the first pair is rejected while it is being bound, and the search reaches the credential behind it. A filter after the match would have dropped the object. - It is checked at compile time. An unknown predicate, a key that is not a
metavariable, a metavariable that some base alternative never binds (the
...$RESTmarker included), and a metavariable at a spot where the compile check cannot write a whole string literal are compile errors. A constraint on a metavariable that is never bound rejects every match, so that alternative would go silent. And a metavariable written inside a string pattern —f("$V")— binds the string's contents without their quotes, which never decode, so the veto would never fire; the same holds inside a template or a regex, and a shorthand property, a member name or a declaration name never holds a string. Put the metavariable where the whole literal goes:f($V). The check writes a string literal at the spot and asks the parser, so it is the embedded TypeScript grammar that decides, not the language. A spot the grammar rejects is a located compile error, never a veto that silently does nothing. A veto on a default parameter value (function $F($P = $V) {}) compiles and vetoes since the parser moved to gotreesitterv0.54.0(ADR 0088); the previous parser reported an untyped parameter with a literal default as a syntax error, so it was rejected until then. No shipped rule puts a veto there. - The predicates are a closed set — today only
not-a-credential. Adding one means adding it tovalueVetoesininternal/core/ruleengine/valueveto.go. Full semantics in[valueveto]there, and the decision in ADR 0087.
The one ellipsis: object properties, spelled ...$REST
Semgrep's general ellipsis is still not supported. Objects are the single
exception, because without it an object pattern was unusable in practice: the
matcher compares the property count, so ({apiKey: $V}) reached only an
object with exactly one property. A census across two real TypeScript projects
measured every object holding a credential-named string property at arity 5,
5, 5 and 3 — four of four multi-property, none at arity 1.
- "({...$REST, $NAME: $VALUE})" # any position, object of any size
It is spelled ...$REST, not Semgrep's ..., and that was measured rather
than chosen. Since the compile gate, a pattern whose parse tree contains an
ERROR node is rejected — and the TypeScript parser cannot parse a bare ellipsis
inside an object literal:
| written as | parses |
|---|---|
{..., $NAME: $VALUE} |
HasError → rejected at compile |
{$NAME: $VALUE, ...} |
HasError → rejected at compile |
{...$REST, $NAME: $VALUE} |
clean → object[spread_element, pair] |
A spread of a metavariable is ordinary TypeScript, so it survives the gate.
Three things to know when you use it:
- It is zero-or-more, so one alternative covers
{apiKey: "x"}and a credential buried in a five-property config object. Never write a separate single-pair alternative. - The marker does not bind.
$RESTis punctuation; ametavariable-regexnaming it constrains nothing. - It is opt-in per pattern. A pattern without a spread keeps exact-arity matching, unchanged. That is what makes it safe: adding it changed the behaviour of no existing rule.
Full semantics and the reasoning are in [objectsubset] in
internal/core/ruleengine/matcher.go.
When is something a rule, and when is it surface? (read before adding a rule)
A deterministic rule and mapped surface draw a hard line (ADR 0004):
- A rule matches a dangerous pattern that is conclusive over a local
subtree, with metavariables filling the holes of single nodes.
md5($X)is a rule: it matchesmd5(password),md5(someVar),md5(anything)— the metavariable generalizes the argument, and md5 is weak regardless of it. - Surface is anything that, to conclude, must follow the data through
variables or intervening code (taint).
const q = "..." + x; db.query(q)is surface — the rule cannot followqback to its construction.
The guardrail: if, while writing a rule, you find yourself wanting to express "and somewhere inside/after" (arbitrary intervening code) or "and this value comes from such a source" (follow a variable), that is the signal that the case is surface, not a rule. A rule that tries to follow data it cannot follow is narrow and dishonest — a mutilated rule is worse than an absent one, because it gives false confidence. Send it to surface.
A category can split: SQL injection built inline in the call is a rule; SQL assembled through an intermediate variable is surface. Both halves are declared in the coverage manifest.
Rule fields
rules:
- id: SEC-052
message: "MD5/SHA-1 is weak for security"
suggestion: "Use SHA-256+ for integrity, bcrypt/argon2 for passwords"
severity: medium # critical | high | medium | low | info
dimension: security
pattern-either: # one of: pattern | pattern-either
- "md5($X)"
- "sha1($X)"
# optional conditions: pattern-not, pattern-inside, metavariable-regex,
# metavariable-name, metavariable-value-veto
Severity is the rule's natural severity; the sensor adjusts it by
path_criticality (a finding in a test file is re-weighted by
sensors.security.test_severity — forced to info by default, RF-10), so rules
never encode path context.
A pattern that does not parse is rejected, not accepted
Every pattern string is parsed with the provider's own parser at compile time, and a pattern whose parsed tree contains a syntax error fails the whole compile, naming the rule id, the operator, and the offending pattern text.
This is worth stating because the failure it prevents is invisible. tree-sitter
is error-recovering: given "eval($X" it does not fail — it returns a tree
containing an ERROR node. Without the check the rule compiles cleanly and then
matches nothing, for the life of the process, with no error anywhere. A
silent rule is a silent vulnerability, so a typo in a pattern is loud instead.
Practical consequence for rule authors: if a rule fails to compile, the pattern
is not valid code in that language. Wrap a fragment in whatever syntax makes it
a complete expression — the way the XSS rules write an object literal as
"({__html: $Q})", since the parentheses are semantically transparent and get
peeled off before matching.
Documentation
¶
Overview ¶
Package rules embeds codefit's declarative detection rules so they ship inside the single binary, versioned with it. It holds NO logic: ruleengine.LoadFS reads and validates the YAML, the provider compiles it — this package only carries the bytes. Contributors add rules as YAML here without writing Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var FS embed.FS
FS is the embedded rule tree, organised as <language>/<dimension>/*.yaml.
Functions ¶
This section is empty.
Types ¶
This section is empty.