docs/generated/contracts.mdgenerated from code

Reference · 4 min read

secret-formats

Contracts: secret-formats.

Source: crates/lvcs/src/secrets/formats.rs.

ruston main
1/// The characters a token's body may hold, all ASCII.2#[derive(Debug, Clone, Copy, PartialEq, Eq)]3enum Body {4    /// `A-Z a-z 0-9`.5    Alnum,6    /// `A-Z 0-9`.7    UpperAlnum,8    /// `0-9 a-f`.9    LowerHex,10    /// `A-Z a-z 0-9 _`.11    AlnumUnderscore,12    /// `A-Z a-z 0-9 -`.13    AlnumDash,14    /// `A-Z a-z 0-9 _ -` (base64url).15    Base64Url,16    /// `A-Z a-z 0-9 /`.17    AlnumSlash,18}1920/// How many code points a token's body holds.21#[derive(Debug, Clone, Copy, PartialEq, Eq)]22enum Len {23    Exactly(usize),24    AtLeast(usize),25}2627/// How a known token format is recognized. A token starts only where the28/// code point before it is not an ASCII letter, digit, `_`, or `-`, so a29/// prefix inside a longer word (`task_live_`) is not a token.30#[derive(Debug, Clone, Copy, PartialEq, Eq)]31enum Shape {32    /// One of `prefixes`, then the longest run of `body` characters, whose33    /// length must fit `len`, followed by no ASCII letter or digit. With34    /// `mixed`, the run must also hold an uppercase letter, a lowercase35    /// letter, and a digit, as a random token does and a word does not.36    Prefixed {37        prefixes: &'static [&'static str],38        body: Body,39        len: Len,40        mixed: bool,41    },42    /// A JSON Web Token: three base64url segments of at least43    /// [`JWT_SEGMENT_MIN`] code points joined by `.`, the first two44    /// starting `eyJ` (a base64url-encoded JSON object).45    Jwt,46}4748/// A known token format; `rule` names it in `SecretHit::rule` and the49/// API's `SecretData.rule`.50#[derive(Debug, Clone, Copy, PartialEq, Eq)]51struct TokenFormat {52    rule: &'static str,53    shape: Shape,54}5556/// The rule of a PEM private key: `-----BEGIN <label>-----` whose label57/// ends in `PRIVATE KEY` or `PRIVATE KEY BLOCK`, through the first58/// `-----END ` after it when that closes the same label, with at least59/// [`PEM_BODY_MIN`] code points between them. A header with no body, as60/// code that parses PEM names it, is not a hit; a body written without61/// its footer is left to the high-entropy heuristic, line by line. A PEM62/// block with any other label (`CERTIFICATE`, `PUBLIC KEY`) is public63/// data: the heuristic skips it.64const PEM_PRIVATE_KEY_RULE: &str = "private-key";6566/// Fewest code points between a PEM private key's header and footer.67const PEM_BODY_MIN: usize = 64;6869/// Fewest code points in each segment of a JSON Web Token.70const JWT_SEGMENT_MIN: usize = 10;7172/// The known token formats, tried in this order at each position: the73/// first that matches names the hit, so a format comes before a more74/// general one sharing its prefix (`anthropic-key` before `sk-key`).75const TOKEN_FORMATS: &[TokenFormat] = &[76    TokenFormat {77        rule: "aws-access-key-id",78        shape: Shape::Prefixed {79            prefixes: &["AKIA", "ASIA", "ABIA", "ACCA"],80            body: Body::UpperAlnum,81            len: Len::Exactly(16),82            mixed: false,83        },84    },85    TokenFormat {86        rule: "github-token",87        shape: Shape::Prefixed {88            prefixes: &["ghp_", "gho_", "ghu_", "ghs_", "ghr_"],89            body: Body::Alnum,90            len: Len::AtLeast(36),91            mixed: false,92        },93    },94    TokenFormat {95        rule: "github-pat",96        shape: Shape::Prefixed {97            prefixes: &["github_pat_"],98            body: Body::AlnumUnderscore,99            len: Len::AtLeast(40),100            mixed: false,101        },102    },103    TokenFormat {104        rule: "gitlab-token",105        shape: Shape::Prefixed {106            prefixes: &["glpat-"],107            body: Body::Base64Url,108            len: Len::AtLeast(20),109            mixed: false,110        },111    },112    TokenFormat {113        rule: "slack-token",114        shape: Shape::Prefixed {115            prefixes: &["xoxa-", "xoxb-", "xoxe-", "xoxp-", "xoxr-", "xoxs-"],116            body: Body::AlnumDash,117            len: Len::AtLeast(10),118            mixed: false,119        },120    },121    TokenFormat {122        rule: "slack-webhook",123        shape: Shape::Prefixed {124            prefixes: &["https://hooks.slack.com/services/"],125            body: Body::AlnumSlash,126            len: Len::AtLeast(20),127            mixed: false,128        },129    },130    TokenFormat {131        rule: "stripe-key",132        shape: Shape::Prefixed {133            prefixes: &["sk_live_", "rk_live_", "sk_test_", "rk_test_"],134            body: Body::Alnum,135            len: Len::AtLeast(16),136            mixed: false,137        },138    },139    TokenFormat {140        rule: "anthropic-key",141        shape: Shape::Prefixed {142            prefixes: &["sk-ant-"],143            body: Body::Base64Url,144            len: Len::AtLeast(32),145            mixed: true,146        },147    },148    TokenFormat {149        rule: "sk-key",150        shape: Shape::Prefixed {151            prefixes: &["sk-"],152            body: Body::Base64Url,153            len: Len::AtLeast(32),154            mixed: true,155        },156    },157    TokenFormat {158        rule: "google-api-key",159        shape: Shape::Prefixed {160            prefixes: &["AIza"],161            body: Body::Base64Url,162            len: Len::Exactly(35),163            mixed: false,164        },165    },166    TokenFormat {167        rule: "huggingface-token",168        shape: Shape::Prefixed {169            prefixes: &["hf_"],170            body: Body::Alnum,171            len: Len::AtLeast(34),172            mixed: false,173        },174    },175    TokenFormat {176        rule: "npm-token",177        shape: Shape::Prefixed {178            prefixes: &["npm_"],179            body: Body::Alnum,180            len: Len::AtLeast(36),181            mixed: false,182        },183    },184    TokenFormat {185        rule: "pypi-token",186        shape: Shape::Prefixed {187            prefixes: &["pypi-AgEIcHlwaS5vcmc"],188            body: Body::Base64Url,189            len: Len::AtLeast(50),190            mixed: false,191        },192    },193    TokenFormat {194        rule: "digitalocean-token",195        shape: Shape::Prefixed {196            prefixes: &["dop_v1_", "doo_v1_", "dor_v1_"],197            body: Body::LowerHex,198            len: Len::Exactly(64),199            mixed: false,200        },201    },202    TokenFormat {203        rule: "age-secret-key",204        shape: Shape::Prefixed {205            prefixes: &["AGE-SECRET-KEY-1"],206            body: Body::UpperAlnum,207            len: Len::Exactly(58),208            mixed: false,209        },210    },211    TokenFormat {212        rule: "jwt",213        shape: Shape::Jwt,214    },215];