@tobi-or-not/quorum 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/bench/fingerprint.d.ts +8 -5
- package/dist/bench/fingerprint.js +28 -9
- package/dist/bench/records.d.ts +168 -0
- package/dist/bench/records.js +106 -1
- package/dist/bench/runner.d.ts +18 -0
- package/dist/bench/runner.js +30 -4
- package/dist/cli/bench.d.ts +11 -4
- package/dist/cli/bench.js +34 -17
- package/dist/cli/bin.js +2 -2
- package/dist/cli/io.d.ts +5 -0
- package/dist/cli/main.d.ts +67 -24
- package/dist/cli/main.js +288 -59
- package/dist/cli/print-config.d.ts +8 -6
- package/dist/cli/print-config.js +26 -8
- package/dist/cli/render.d.ts +9 -3
- package/dist/cli/render.js +43 -2
- package/dist/config/claude-code.d.ts +2 -6
- package/dist/config/claude-code.js +6 -44
- package/dist/config/command.d.ts +10 -0
- package/dist/config/command.js +47 -0
- package/dist/config/env.d.ts +32 -19
- package/dist/config/env.js +95 -32
- package/dist/config/from-config.js +21 -3
- package/dist/config/resolve.d.ts +13 -1
- package/dist/config/resolve.js +27 -1
- package/dist/config/schema.d.ts +152 -5
- package/dist/config/schema.js +156 -12
- package/dist/core/clock.d.ts +17 -0
- package/dist/core/clock.js +14 -0
- package/dist/core/coverage.d.ts +74 -0
- package/dist/core/coverage.js +221 -0
- package/dist/core/diff.d.ts +101 -0
- package/dist/core/diff.js +178 -35
- package/dist/core/errors.d.ts +12 -3
- package/dist/core/errors.js +21 -2
- package/dist/core/estimate.d.ts +45 -0
- package/dist/core/estimate.js +188 -0
- package/dist/core/exclude.d.ts +42 -0
- package/dist/core/exclude.js +189 -0
- package/dist/core/flags.d.ts +14 -1
- package/dist/core/flags.js +56 -0
- package/dist/core/inputs.d.ts +38 -0
- package/dist/core/inputs.js +228 -0
- package/dist/core/limits.d.ts +83 -0
- package/dist/core/limits.js +169 -0
- package/dist/core/output.d.ts +3 -2
- package/dist/core/output.js +3 -2
- package/dist/core/parts.d.ts +146 -0
- package/dist/core/parts.js +388 -0
- package/dist/core/ports.d.ts +67 -2
- package/dist/core/ports.js +36 -1
- package/dist/core/prompts.d.ts +22 -3
- package/dist/core/prompts.js +43 -7
- package/dist/core/quorum.d.ts +26 -8
- package/dist/core/quorum.js +310 -73
- package/dist/core/retry.d.ts +36 -0
- package/dist/core/retry.js +46 -0
- package/dist/core/secrets.d.ts +98 -0
- package/dist/core/secrets.js +507 -0
- package/dist/core/stages/context.d.ts +131 -23
- package/dist/core/stages/context.js +331 -46
- package/dist/core/stages/panel.d.ts +13 -6
- package/dist/core/stages/panel.js +79 -19
- package/dist/core/stages/research.d.ts +110 -25
- package/dist/core/stages/research.js +338 -44
- package/dist/core/tokens.d.ts +31 -0
- package/dist/core/tokens.js +81 -0
- package/dist/core/types.d.ts +280 -25
- package/dist/core/verify.d.ts +6 -0
- package/dist/core/verify.js +6 -3
- package/dist/git/run.d.ts +223 -0
- package/dist/git/run.js +608 -0
- package/dist/git/tree.d.ts +43 -0
- package/dist/git/tree.js +171 -0
- package/dist/index.d.ts +26 -2
- package/dist/index.js +34 -2
- package/dist/providers/anthropic.d.ts +3 -1
- package/dist/providers/anthropic.js +10 -3
- package/dist/providers/claude-code.d.ts +56 -11
- package/dist/providers/claude-code.js +70 -25
- package/dist/providers/gemini.d.ts +5 -1
- package/dist/providers/gemini.js +65 -11
- package/dist/providers/http.d.ts +49 -4
- package/dist/providers/http.js +155 -14
- package/dist/providers/ollama.d.ts +4 -1
- package/dist/providers/ollama.js +13 -5
- package/dist/providers/openai-compatible.d.ts +4 -1
- package/dist/providers/openai-compatible.js +13 -5
- package/dist/providers/options.d.ts +12 -3
- package/dist/providers/options.js +30 -10
- package/dist/providers/size.d.ts +19 -15
- package/dist/providers/size.js +33 -73
- package/dist/sources/added.d.ts +49 -0
- package/dist/sources/added.js +134 -0
- package/dist/sources/diff.d.ts +13 -0
- package/dist/sources/diff.js +63 -0
- package/dist/sources/local.d.ts +41 -0
- package/dist/sources/local.js +251 -0
- package/dist/sources/paths.d.ts +45 -0
- package/dist/sources/paths.js +230 -0
- package/dist/sources/range.d.ts +21 -0
- package/dist/sources/range.js +75 -0
- package/dist/sources/read.d.ts +69 -0
- package/dist/sources/read.js +37 -0
- package/dist/sources/repository.d.ts +9 -0
- package/dist/sources/repository.js +28 -0
- package/dist/sources/skips.d.ts +49 -0
- package/dist/sources/skips.js +126 -0
- package/package.json +1 -1
- package/schema/quorum.schema.json +49 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The retry policy: which failures a call retries and how long it waits before the next attempt.
|
|
3
|
+
* It is plain data in and a number out, so it holds no clock and no thrown value: the call path
|
|
4
|
+
* reads a failure's facts and the call's state, and gives it the randomness.
|
|
5
|
+
*/
|
|
6
|
+
/** The longest wait a server may ask for, in milliseconds: a longer hint ends the call. */
|
|
7
|
+
export const MAX_HINT_MS = 60_000;
|
|
8
|
+
/** The backoff before retry n is random from 0 to the lesser of `BACKOFF_CAP_MS` and `BACKOFF_BASE_MS` times 2 to the n. */
|
|
9
|
+
const BACKOFF_BASE_MS = 500;
|
|
10
|
+
const BACKOFF_CAP_MS = 8000;
|
|
11
|
+
/** What a server's hint is stretched by, at most, so calls that were told the same wait do not retry together. */
|
|
12
|
+
const HINT_JITTER_MS = 1000;
|
|
13
|
+
/** The only failures a call retries, whatever a failure says of itself. */
|
|
14
|
+
const RETRIED_KINDS = new Set(['network', 'timeout', 'http']);
|
|
15
|
+
/** The statuses of a gateway that gave up waiting for the server, which count as a timeout. */
|
|
16
|
+
const GATEWAY_TIMEOUTS = new Set([504, 524]);
|
|
17
|
+
/** True for the deadline of the core's own, or a gateway's 504 or 524: a slow request is likely slow again, and may be billed each time. */
|
|
18
|
+
export function isTimeout(failure) {
|
|
19
|
+
return failure.kind === 'timeout' || (failure.kind === 'http' && GATEWAY_TIMEOUTS.has(failure.status));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* How long to wait before the next attempt of a call, in milliseconds, or undefined when the call
|
|
23
|
+
* ends with this failure. It ends, in this order, when the retries are spent, the failure is not a
|
|
24
|
+
* `network`, `timeout` or `http` one, it does not say `retryable` is `true`, it is a second
|
|
25
|
+
* timeout of the call, or the server asks for more than `MAX_HINT_MS` or for more than the time the
|
|
26
|
+
* review has left. Otherwise the wait is the server's hint, when it is a finite number above 0, plus
|
|
27
|
+
* up to a second; else a random time from 0 to the lesser of 8 s and 0.5 s times 2 to the number of
|
|
28
|
+
* retries made. The call path asks this first, with infinite time left once the review's stop is aborted, so a
|
|
29
|
+
* retry that only the stop prevents is the stop's doing, and it checks the stop after.
|
|
30
|
+
*/
|
|
31
|
+
export function retryWait(failure, call, random) {
|
|
32
|
+
// Not `>=`: a retry count that is not a number compares false both ways, and must end the call.
|
|
33
|
+
if (!(call.retried < call.retries))
|
|
34
|
+
return undefined;
|
|
35
|
+
if (!RETRIED_KINDS.has(failure.kind) || failure.retryable !== true)
|
|
36
|
+
return undefined;
|
|
37
|
+
if (call.timedOut && isTimeout(failure))
|
|
38
|
+
return undefined;
|
|
39
|
+
const hint = failure.retryAfterMs;
|
|
40
|
+
if (typeof hint === 'number' && Number.isFinite(hint) && hint > 0) {
|
|
41
|
+
return hint > MAX_HINT_MS || hint > call.timeLeftMs
|
|
42
|
+
? undefined
|
|
43
|
+
: hint + random() * HINT_JITTER_MS;
|
|
44
|
+
}
|
|
45
|
+
return random() * Math.min(BACKOFF_CAP_MS, BACKOFF_BASE_MS * 2 ** call.retried);
|
|
46
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* The credential rules below are gitleaks 8.30.1's, and the private-key markers are
|
|
3
|
+
* pre-commit-hooks v5.0.0's. Both are MIT licensed:
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2019 Zachary Rice
|
|
6
|
+
* Copyright (c) 2014 pre-commit dev team: Anthony Sottile, Ken Struys
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
import type { DiffSection } from './diff.ts';
|
|
27
|
+
import type { CredentialKind, SecretRule } from './types.ts';
|
|
28
|
+
/** The names of files that hold a secret, matched whole against a path's last segment, in any case. */
|
|
29
|
+
export declare const SECRET_NAMES: readonly string[];
|
|
30
|
+
/** A name that starts with this is a secret's: `.env.local`, and `.env.defaults`, which a loader reads as values at run time. In any case. */
|
|
31
|
+
export declare const SECRET_PREFIX = ".env.";
|
|
32
|
+
/** What the name of a file that holds a secret ends with, in any case. */
|
|
33
|
+
export declare const SECRET_SUFFIXES: readonly string[];
|
|
34
|
+
/** What ends the name of a template, after `SECRET_PREFIX`: `.env.example` is committed with no value in it, and is sent. In any case. */
|
|
35
|
+
export declare const TEMPLATE_SUFFIXES: readonly string[];
|
|
36
|
+
/** Every rule a file can be held by, in the order a reader lists them. */
|
|
37
|
+
export declare const SECRET_RULES: readonly ["name", "private-key", "aws-key", "github-token", "slack-token", "google-key", "stripe-key", "anthropic-key", "openai-key"];
|
|
38
|
+
/** What a kind of credential is called in a sentence, after "holds what reads as". */
|
|
39
|
+
export declare const CREDENTIAL_PHRASES: Readonly<Record<CredentialKind, string>>;
|
|
40
|
+
/** One rule of gitleaks' default config: what it reads, and what makes a match no hit. */
|
|
41
|
+
export interface CredentialRule {
|
|
42
|
+
/** The kind a hit of this rule reports. */
|
|
43
|
+
readonly kind: CredentialKind;
|
|
44
|
+
/** The rule's id in gitleaks' config. */
|
|
45
|
+
readonly id: string;
|
|
46
|
+
/** Gitleaks' regex, as JavaScript: its `(?i)` is the `i` flag. */
|
|
47
|
+
readonly pattern: RegExp;
|
|
48
|
+
/** A match whose secret has an entropy at or below this is no hit; none when the rule has no floor. */
|
|
49
|
+
readonly entropy?: number;
|
|
50
|
+
/** A match whose secret this matches is no hit: the rule's own allowlist. */
|
|
51
|
+
readonly allow?: RegExp;
|
|
52
|
+
/** A line that holds none of these, in any case, runs no rule. */
|
|
53
|
+
readonly keywords: readonly string[];
|
|
54
|
+
}
|
|
55
|
+
/** The 21 rules of gitleaks 8.30.1 for AWS, GitHub, Slack, Google, Stripe, Anthropic and OpenAI, grouped by kind, in the order of `SECRET_RULES`. */
|
|
56
|
+
export declare const CREDENTIAL_RULES: readonly CredentialRule[];
|
|
57
|
+
/**
|
|
58
|
+
* Whether the name of a file is a secret's: its last segment is one of `SECRET_NAMES`, starts with
|
|
59
|
+
* `SECRET_PREFIX` or ends in one of `SECRET_SUFFIXES`, in any case, and is no template (a `.env.` name
|
|
60
|
+
* that ends in one of `TEMPLATE_SUFFIXES`, or a `secrets.yaml` or `secrets.yml` in a directory named
|
|
61
|
+
* `templates`). It reads a path as the diff writes it, so an `a/` or `b/` prefix changes nothing.
|
|
62
|
+
* Generous: only the name decides, never the directory, a template apart.
|
|
63
|
+
*/
|
|
64
|
+
export declare function isSecretName(path: string): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* The kind of the first credential a text holds, read line by line as gitleaks reads a fragment, or
|
|
67
|
+
* undefined. A line is first tested for a private-key marker, which counts only when a body run
|
|
68
|
+
* follows it, on its own line or within `BODY_REACH` lines; then, when it holds a keyword of the rules, for each rule in
|
|
69
|
+
* the table's order and every match of it on the line, a match whose secret has an entropy at or below
|
|
70
|
+
* the rule's floor, or that the rule's own allowlist matches, being no hit. It reads every row alike
|
|
71
|
+
* (added, removed, context, or none), a line of any length and a line that ends with `\r`, and it is
|
|
72
|
+
* linear in the text. Generous: a removed line and a key in a comment hold as any other.
|
|
73
|
+
*/
|
|
74
|
+
export declare function credentialIn(text: string): CredentialKind | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Why a section is held back, or undefined: `name` when its header's path (a non-empty one), a name
|
|
77
|
+
* its `diff --git` line holds (each word of it, so a header that cannot be read, or that is forged as
|
|
78
|
+
* `diff --git a/.env` or `diff --git a/.env b/.env c`, still names the `.env` it spells), or a path its
|
|
79
|
+
* own lines name is a secret's, else the kind of the first credential its text holds. The name is
|
|
80
|
+
* taken before the content, so a `.env` is held by its name whatever it holds. Generous: any one path
|
|
81
|
+
* the section names, a rename's old side included, holds it.
|
|
82
|
+
*/
|
|
83
|
+
export declare function secretRuleOf(section: DiffSection): SecretRule | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Whether a section deletes its file whole: it starts with its `diff --git` header, holds a `deleted
|
|
86
|
+
* file mode` line, and every other line of it is one git writes for a deletion: `deleted file mode`,
|
|
87
|
+
* `index`, a `-` row (the `---` line is one), `+++ /dev/null`, `Binary files <path> and /dev/null
|
|
88
|
+
* differ`, a hunk header whose new side is `+0,0`, ``, and an empty last
|
|
89
|
+
* piece, each less a trailing `\r`. Any other line (a `+` or context row, a blank line in the
|
|
90
|
+
* middle, a mail's headers in a `format-patch` series, a `GIT binary patch`) makes it no whole
|
|
91
|
+
* deletion, so a forged or mixed section is a gap and never passes as one, and a file truncated to
|
|
92
|
+
* empty, which has no `deleted file mode`, is no deletion. Its first `--- ` line, when it has one (an
|
|
93
|
+
* empty or a binary file has none), also names the old side of a header that names one path on both
|
|
94
|
+
* sides (`namesOldSide`): a `.env` header over a `--- a/src/auth.ts` line is a forged section that
|
|
95
|
+
* would hide the removal of another file, so it is no deletion and a gap. Only the first such line
|
|
96
|
+
* counts: a later one is a removed row of the file.
|
|
97
|
+
*/
|
|
98
|
+
export declare function isWholeDeletion(text: string): boolean;
|
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* The credential rules below are gitleaks 8.30.1's, and the private-key markers are
|
|
3
|
+
* pre-commit-hooks v5.0.0's. Both are MIT licensed:
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2019 Zachary Rice
|
|
6
|
+
* Copyright (c) 2014 pre-commit dev team: Anthony Sottile, Ken Struys
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* What holds a file back from every review: its name, or a credential it holds. Pure functions over a
|
|
28
|
+
* path and a diff's text, which the sources and the review apply before any model is sent a line.
|
|
29
|
+
* Every check is generous (a hunk row that reads like a header names a path, a removed line holds a
|
|
30
|
+
* key as an added one does), so a doubtful file is held and never sent.
|
|
31
|
+
*
|
|
32
|
+
* @see ../../docs/20-using/15-sources.md#files-held-back-as-secrets for what a reader sees.
|
|
33
|
+
*/
|
|
34
|
+
import { headerNames, namedPaths, namesOldSide } from "./diff.js";
|
|
35
|
+
/** The names of files that hold a secret, matched whole against a path's last segment, in any case. */
|
|
36
|
+
export const SECRET_NAMES = Object.freeze([
|
|
37
|
+
'.env',
|
|
38
|
+
'.npmrc',
|
|
39
|
+
'.pypirc',
|
|
40
|
+
'.netrc',
|
|
41
|
+
'_netrc',
|
|
42
|
+
'.git-credentials',
|
|
43
|
+
'.pgpass',
|
|
44
|
+
'.htpasswd',
|
|
45
|
+
'id_rsa',
|
|
46
|
+
'id_dsa',
|
|
47
|
+
'id_ecdsa',
|
|
48
|
+
'id_ed25519',
|
|
49
|
+
'credentials',
|
|
50
|
+
'credentials.json',
|
|
51
|
+
'secrets.json',
|
|
52
|
+
'secrets.toml',
|
|
53
|
+
'secrets.yaml',
|
|
54
|
+
'secrets.yml',
|
|
55
|
+
]);
|
|
56
|
+
/** A name that starts with this is a secret's: `.env.local`, and `.env.defaults`, which a loader reads as values at run time. In any case. */
|
|
57
|
+
export const SECRET_PREFIX = '.env.';
|
|
58
|
+
/** What the name of a file that holds a secret ends with, in any case. */
|
|
59
|
+
export const SECRET_SUFFIXES = Object.freeze([
|
|
60
|
+
'.pem',
|
|
61
|
+
'.key',
|
|
62
|
+
'.p12',
|
|
63
|
+
'.pfx',
|
|
64
|
+
'.jks',
|
|
65
|
+
'.keystore',
|
|
66
|
+
'.ppk',
|
|
67
|
+
'.kdbx',
|
|
68
|
+
'.tfstate',
|
|
69
|
+
'.tfstate.backup',
|
|
70
|
+
]);
|
|
71
|
+
/** What ends the name of a template, after `SECRET_PREFIX`: `.env.example` is committed with no value in it, and is sent. In any case. */
|
|
72
|
+
export const TEMPLATE_SUFFIXES = Object.freeze([
|
|
73
|
+
'.example',
|
|
74
|
+
'.sample',
|
|
75
|
+
'.template',
|
|
76
|
+
'.dist',
|
|
77
|
+
]);
|
|
78
|
+
/** The directory of a Helm chart's templates: its `secrets.yaml` and `secrets.yml` are templates, not secrets. */
|
|
79
|
+
const HELM_TEMPLATES = 'templates';
|
|
80
|
+
const HELM_SECRET_TEMPLATES = ['secrets.yaml', 'secrets.yml'];
|
|
81
|
+
/** Every rule a file can be held by, in the order a reader lists them. */
|
|
82
|
+
export const SECRET_RULES = Object.freeze([
|
|
83
|
+
'name',
|
|
84
|
+
'private-key',
|
|
85
|
+
'aws-key',
|
|
86
|
+
'github-token',
|
|
87
|
+
'slack-token',
|
|
88
|
+
'google-key',
|
|
89
|
+
'stripe-key',
|
|
90
|
+
'anthropic-key',
|
|
91
|
+
'openai-key',
|
|
92
|
+
]);
|
|
93
|
+
/** What a kind of credential is called in a sentence, after "holds what reads as". */
|
|
94
|
+
export const CREDENTIAL_PHRASES = Object.freeze({
|
|
95
|
+
'private-key': 'a private key',
|
|
96
|
+
'aws-key': 'an AWS key',
|
|
97
|
+
'github-token': 'a GitHub token',
|
|
98
|
+
'slack-token': 'a Slack token or webhook',
|
|
99
|
+
'google-key': 'a Google API key',
|
|
100
|
+
'stripe-key': 'a Stripe key',
|
|
101
|
+
'anthropic-key': 'an Anthropic key',
|
|
102
|
+
'openai-key': 'an OpenAI key',
|
|
103
|
+
});
|
|
104
|
+
/* eslint-disable regexp/prefer-w, regexp/prefer-d, regexp/optimal-quantifier-concatenation -- gitleaks 8.30.1's patterns, kept as it ships them so they compare with its config */
|
|
105
|
+
/** The 21 rules of gitleaks 8.30.1 for AWS, GitHub, Slack, Google, Stripe, Anthropic and OpenAI, grouped by kind, in the order of `SECRET_RULES`. */
|
|
106
|
+
export const CREDENTIAL_RULES = Object.freeze([
|
|
107
|
+
{
|
|
108
|
+
kind: 'aws-key',
|
|
109
|
+
id: 'aws-access-token',
|
|
110
|
+
pattern: /\b((?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16})\b/,
|
|
111
|
+
entropy: 3,
|
|
112
|
+
allow: /.+EXAMPLE$/,
|
|
113
|
+
keywords: ['a3t', 'akia', 'asia', 'abia', 'acca'],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
kind: 'aws-key',
|
|
117
|
+
id: 'aws-amazon-bedrock-api-key-long-lived',
|
|
118
|
+
pattern: /\b(ABSK[A-Za-z0-9+/]{109,269}={0,2})(?:[\x60'"\s;]|\\[nr]|$)/,
|
|
119
|
+
entropy: 3,
|
|
120
|
+
keywords: ['absk'],
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
kind: 'github-token',
|
|
124
|
+
id: 'github-pat',
|
|
125
|
+
pattern: /ghp_[0-9a-zA-Z]{36}/,
|
|
126
|
+
entropy: 3,
|
|
127
|
+
keywords: ['ghp_'],
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
kind: 'github-token',
|
|
131
|
+
id: 'github-fine-grained-pat',
|
|
132
|
+
pattern: /github_pat_\w{82}/,
|
|
133
|
+
entropy: 3,
|
|
134
|
+
keywords: ['github_pat_'],
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
kind: 'github-token',
|
|
138
|
+
id: 'github-oauth',
|
|
139
|
+
pattern: /gho_[0-9a-zA-Z]{36}/,
|
|
140
|
+
entropy: 3,
|
|
141
|
+
keywords: ['gho_'],
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
kind: 'github-token',
|
|
145
|
+
id: 'github-app-token',
|
|
146
|
+
pattern: /(?:ghu|ghs)_[0-9a-zA-Z]{36}/,
|
|
147
|
+
entropy: 3,
|
|
148
|
+
keywords: ['ghu_', 'ghs_'],
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
kind: 'github-token',
|
|
152
|
+
id: 'github-refresh-token',
|
|
153
|
+
pattern: /ghr_[0-9a-zA-Z]{36}/,
|
|
154
|
+
entropy: 3,
|
|
155
|
+
keywords: ['ghr_'],
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
kind: 'slack-token',
|
|
159
|
+
id: 'slack-bot-token',
|
|
160
|
+
pattern: /xoxb-[0-9]{10,13}-[0-9]{10,13}[a-zA-Z0-9-]*/,
|
|
161
|
+
entropy: 3,
|
|
162
|
+
keywords: ['xoxb'],
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
kind: 'slack-token',
|
|
166
|
+
id: 'slack-user-token',
|
|
167
|
+
pattern: /xox[pe](?:-[0-9]{10,13}){3}-[a-zA-Z0-9-]{28,34}/,
|
|
168
|
+
entropy: 2,
|
|
169
|
+
keywords: ['xoxp-', 'xoxe-'],
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
kind: 'slack-token',
|
|
173
|
+
id: 'slack-app-token',
|
|
174
|
+
pattern: /xapp-\d-[A-Z0-9]+-\d+-[a-z0-9]+/i,
|
|
175
|
+
entropy: 2,
|
|
176
|
+
keywords: ['xapp'],
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
kind: 'slack-token',
|
|
180
|
+
id: 'slack-config-access-token',
|
|
181
|
+
pattern: /xoxe.xox[bp]-\d-[A-Z0-9]{163,166}/i,
|
|
182
|
+
entropy: 2,
|
|
183
|
+
keywords: ['xoxe.xoxb-', 'xoxe.xoxp-'],
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
kind: 'slack-token',
|
|
187
|
+
id: 'slack-config-refresh-token',
|
|
188
|
+
pattern: /xoxe-\d-[A-Z0-9]{146}/i,
|
|
189
|
+
entropy: 2,
|
|
190
|
+
keywords: ['xoxe-'],
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
kind: 'slack-token',
|
|
194
|
+
id: 'slack-legacy-bot-token',
|
|
195
|
+
pattern: /xoxb-[0-9]{8,14}-[a-zA-Z0-9]{18,26}/,
|
|
196
|
+
entropy: 2,
|
|
197
|
+
keywords: ['xoxb'],
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
kind: 'slack-token',
|
|
201
|
+
id: 'slack-legacy-token',
|
|
202
|
+
pattern: /xox[os]-\d+-\d+-\d+-[a-fA-F\d]+/,
|
|
203
|
+
entropy: 2,
|
|
204
|
+
keywords: ['xoxo', 'xoxs'],
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
kind: 'slack-token',
|
|
208
|
+
id: 'slack-legacy-workspace-token',
|
|
209
|
+
pattern: /xox[ar]-(?:\d-)?[0-9a-zA-Z]{8,48}/,
|
|
210
|
+
entropy: 2,
|
|
211
|
+
keywords: ['xoxa', 'xoxr'],
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
kind: 'slack-token',
|
|
215
|
+
id: 'slack-webhook-url',
|
|
216
|
+
pattern: /(?:https?:\/\/)?hooks.slack.com\/(?:services|workflows|triggers)\/[A-Za-z0-9+/]{43,56}/,
|
|
217
|
+
keywords: ['hooks.slack.com'],
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
kind: 'google-key',
|
|
221
|
+
id: 'gcp-api-key',
|
|
222
|
+
pattern: /\b(AIza[\w-]{35})(?:[\x60'"\s;]|\\[nr]|$)/,
|
|
223
|
+
entropy: 4,
|
|
224
|
+
keywords: ['aiza'],
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
kind: 'stripe-key',
|
|
228
|
+
id: 'stripe-access-token',
|
|
229
|
+
pattern: /\b((?:sk|rk)_(?:test|live|prod)_[a-zA-Z0-9]{10,99})(?:[\x60'"\s;]|\\[nr]|$)/,
|
|
230
|
+
entropy: 2,
|
|
231
|
+
keywords: [
|
|
232
|
+
'sk_test',
|
|
233
|
+
'sk_live',
|
|
234
|
+
'sk_prod',
|
|
235
|
+
'rk_test',
|
|
236
|
+
'rk_live',
|
|
237
|
+
'rk_prod',
|
|
238
|
+
],
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
kind: 'anthropic-key',
|
|
242
|
+
id: 'anthropic-api-key',
|
|
243
|
+
pattern: /\b(sk-ant-api03-[a-zA-Z0-9_\-]{93}AA)(?:[\x60'"\s;]|\\[nr]|$)/,
|
|
244
|
+
keywords: ['sk-ant-api03'],
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
kind: 'anthropic-key',
|
|
248
|
+
id: 'anthropic-admin-api-key',
|
|
249
|
+
pattern: /\b(sk-ant-admin01-[a-zA-Z0-9_\-]{93}AA)(?:[\x60'"\s;]|\\[nr]|$)/,
|
|
250
|
+
keywords: ['sk-ant-admin01'],
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
kind: 'openai-key',
|
|
254
|
+
id: 'openai-api-key',
|
|
255
|
+
pattern: /\b(sk-(?:proj|svcacct|admin)-(?:[A-Za-z0-9_-]{74}|[A-Za-z0-9_-]{58})T3BlbkFJ(?:[A-Za-z0-9_-]{74}|[A-Za-z0-9_-]{58})\b|sk-[a-zA-Z0-9]{20}T3BlbkFJ[a-zA-Z0-9]{20})(?:[\x60'"\s;]|\\[nr]|$)/,
|
|
256
|
+
entropy: 3,
|
|
257
|
+
keywords: ['t3blbkfj'],
|
|
258
|
+
},
|
|
259
|
+
]);
|
|
260
|
+
/* eslint-enable regexp/prefer-w, regexp/prefer-d, regexp/optimal-quantifier-concatenation */
|
|
261
|
+
/**
|
|
262
|
+
* The private-key markers of pre-commit-hooks v5.0.0, matched as substrings, case and all. A source
|
|
263
|
+
* that spells one holds itself, so each is built from pieces and none is contiguous here.
|
|
264
|
+
*/
|
|
265
|
+
const PRIVATE_KEY_MARKERS = Object.freeze([
|
|
266
|
+
...[
|
|
267
|
+
'RSA ',
|
|
268
|
+
'DSA ',
|
|
269
|
+
'EC ',
|
|
270
|
+
'OPENSSH ',
|
|
271
|
+
'',
|
|
272
|
+
'SSH2 ENCRYPTED ',
|
|
273
|
+
'ENCRYPTED ',
|
|
274
|
+
].map(kind => `BEGIN ${kind}PRIVATE KEY`),
|
|
275
|
+
['BEGIN PGP ', 'PRIVATE KEY BLOCK'].join(''),
|
|
276
|
+
['PuTTY-User-', 'Key-File-2'].join(''),
|
|
277
|
+
['BEGIN OpenVPN ', 'Static key V1'].join(''),
|
|
278
|
+
]);
|
|
279
|
+
/** A text as a pattern that matches only it: the dot of a keyword is a dot, as gitleaks takes it. */
|
|
280
|
+
const escapeLiteral = (text) => text.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw `\$&`);
|
|
281
|
+
/** The markers as one test. */
|
|
282
|
+
const MARKER = new RegExp(PRIVATE_KEY_MARKERS.map(escapeLiteral).join('|'));
|
|
283
|
+
/** How many lines after a marker one may hold the key's body. A PEM key's body follows its marker, and PuTTY's after three header lines. */
|
|
284
|
+
const BODY_REACH = 6;
|
|
285
|
+
/** The fewest characters of a body run: a PEM line is 64 of them, and OpenVPN's is 32 hex digits. */
|
|
286
|
+
const BODY_RUN = 32;
|
|
287
|
+
/** The keywords of every rule as one test, in any case: a line that holds none runs no rule, as gitleaks prefilters a fragment. */
|
|
288
|
+
const KEYWORDS = new RegExp([
|
|
289
|
+
...new Set(CREDENTIAL_RULES.flatMap(rule => rule.keywords)),
|
|
290
|
+
].map(escapeLiteral).join('|'), 'i');
|
|
291
|
+
/** Each rule's pattern with the `g` flag, to read every match of a line. */
|
|
292
|
+
const SCANNERS = CREDENTIAL_RULES.map(rule => ({
|
|
293
|
+
rule,
|
|
294
|
+
all: new RegExp(rule.pattern, `${rule.pattern.flags}g`),
|
|
295
|
+
}));
|
|
296
|
+
/** The last segment of a path, lower-cased, and the directory above it. */
|
|
297
|
+
function splitName(path) {
|
|
298
|
+
const cut = path.lastIndexOf('/');
|
|
299
|
+
const above = cut > 0
|
|
300
|
+
? path.lastIndexOf('/', cut - 1)
|
|
301
|
+
: -1;
|
|
302
|
+
return {
|
|
303
|
+
name: path.slice(cut + 1).toLowerCase(),
|
|
304
|
+
parent: cut > 0
|
|
305
|
+
? path.slice(above + 1, cut)
|
|
306
|
+
: '',
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/** Whether a name is a template that holds no value: `.env.example` and its kind, or a Helm chart's `secrets.yaml`. */
|
|
310
|
+
function isTemplate(name, parent) {
|
|
311
|
+
if (name.startsWith(SECRET_PREFIX))
|
|
312
|
+
return TEMPLATE_SUFFIXES.some(suffix => name.endsWith(suffix));
|
|
313
|
+
return parent === HELM_TEMPLATES && HELM_SECRET_TEMPLATES.includes(name);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Whether the name of a file is a secret's: its last segment is one of `SECRET_NAMES`, starts with
|
|
317
|
+
* `SECRET_PREFIX` or ends in one of `SECRET_SUFFIXES`, in any case, and is no template (a `.env.` name
|
|
318
|
+
* that ends in one of `TEMPLATE_SUFFIXES`, or a `secrets.yaml` or `secrets.yml` in a directory named
|
|
319
|
+
* `templates`). It reads a path as the diff writes it, so an `a/` or `b/` prefix changes nothing.
|
|
320
|
+
* Generous: only the name decides, never the directory, a template apart.
|
|
321
|
+
*/
|
|
322
|
+
export function isSecretName(path) {
|
|
323
|
+
const { name, parent } = splitName(path);
|
|
324
|
+
if (isTemplate(name, parent))
|
|
325
|
+
return false;
|
|
326
|
+
return SECRET_NAMES.includes(name) || name.startsWith(SECRET_PREFIX) || SECRET_SUFFIXES.some(suffix => name.endsWith(suffix));
|
|
327
|
+
}
|
|
328
|
+
/** Whether a character code is one of the base64 alphabet: `A`-`Z`, `a`-`z`, `0`-`9`, `+`, `/` and `=`. */
|
|
329
|
+
function isBase64(code) {
|
|
330
|
+
return (code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 43 || code === 47 || code === 61;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Whether a line holds a body run from `from` on: a maximal run of at least `BODY_RUN` characters of
|
|
334
|
+
* the base64 alphabet that holds a digit and a letter. Any other character ends a run (a space, a tab,
|
|
335
|
+
* a quote, a backslash, a comma, `\r`), so an indented, a quoted and a JSON-escaped body is read, and
|
|
336
|
+
* code and prose are not: spaces and punctuation break their runs, and a long identifier has no
|
|
337
|
+
* digit. One pass, each character looked at once.
|
|
338
|
+
*/
|
|
339
|
+
function holdsBodyRun(line, from = 0) {
|
|
340
|
+
let length = 0;
|
|
341
|
+
let digit = false;
|
|
342
|
+
let letter = false;
|
|
343
|
+
for (let at = from; at < line.length; at++) {
|
|
344
|
+
const code = line.charCodeAt(at);
|
|
345
|
+
if (!isBase64(code)) {
|
|
346
|
+
length = 0;
|
|
347
|
+
digit = false;
|
|
348
|
+
letter = false;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
length++;
|
|
352
|
+
if (code >= 48 && code <= 57)
|
|
353
|
+
digit = true;
|
|
354
|
+
else if (code >= 65)
|
|
355
|
+
letter = true; // `+`, `/` and `=` sit below `A`, so what is left up here is a letter
|
|
356
|
+
if (length >= BODY_RUN && digit && letter)
|
|
357
|
+
return true;
|
|
358
|
+
}
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
/** Whether a line within `BODY_REACH` lines after `at` holds a body run, reading a line at most once in a window for all the markers of a text. */
|
|
362
|
+
function holdsBodyAfter(lines, at, search) {
|
|
363
|
+
const last = Math.min(at + BODY_REACH, lines.length - 1);
|
|
364
|
+
for (let next = Math.max(search.scanned, at) + 1; next <= last; next++) {
|
|
365
|
+
if (holdsBodyRun(lines[next] ?? ''))
|
|
366
|
+
search.lastBody = next;
|
|
367
|
+
search.scanned = next;
|
|
368
|
+
}
|
|
369
|
+
return search.lastBody > at;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Whether the line at `at` holds a marker with a body run after it: on its own line, read once from the
|
|
373
|
+
* end of its first marker, or on one of the next `BODY_REACH` lines.
|
|
374
|
+
*/
|
|
375
|
+
function isPrivateKeyAt(lines, at, search) {
|
|
376
|
+
const line = lines[at] ?? '';
|
|
377
|
+
const marker = MARKER.exec(line);
|
|
378
|
+
if (marker === null)
|
|
379
|
+
return false;
|
|
380
|
+
return holdsBodyRun(line, marker.index + marker[0].length) || holdsBodyAfter(lines, at, search);
|
|
381
|
+
}
|
|
382
|
+
/** The Shannon entropy of a text in bits per character, over its code points, as gitleaks measures a secret. */
|
|
383
|
+
function entropyOf(text) {
|
|
384
|
+
const counts = new Map();
|
|
385
|
+
let length = 0;
|
|
386
|
+
for (const character of text) {
|
|
387
|
+
counts.set(character, (counts.get(character) ?? 0) + 1);
|
|
388
|
+
length++;
|
|
389
|
+
}
|
|
390
|
+
let entropy = 0;
|
|
391
|
+
for (const count of counts.values()) {
|
|
392
|
+
const share = count / length;
|
|
393
|
+
entropy -= share * Math.log2(share);
|
|
394
|
+
}
|
|
395
|
+
return entropy;
|
|
396
|
+
}
|
|
397
|
+
/** The part of a match gitleaks judges: its first non-empty capture group, else the whole match. */
|
|
398
|
+
function secretOf(match) {
|
|
399
|
+
return match.slice(1).find(group => group !== undefined && group !== '') ?? match[0];
|
|
400
|
+
}
|
|
401
|
+
/** The kind of the first match on a line that a rule keeps: every match of every rule, in the table's order. */
|
|
402
|
+
function ruleHitOn(line) {
|
|
403
|
+
if (!KEYWORDS.test(line))
|
|
404
|
+
return undefined;
|
|
405
|
+
for (const { rule, all } of SCANNERS) {
|
|
406
|
+
for (const match of line.matchAll(all)) {
|
|
407
|
+
const secret = secretOf(match);
|
|
408
|
+
if (rule.entropy !== undefined && entropyOf(secret) <= rule.entropy)
|
|
409
|
+
continue;
|
|
410
|
+
if (rule.allow?.test(secret))
|
|
411
|
+
continue;
|
|
412
|
+
return rule.kind;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return undefined;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* The kind of the first credential a text holds, read line by line as gitleaks reads a fragment, or
|
|
419
|
+
* undefined. A line is first tested for a private-key marker, which counts only when a body run
|
|
420
|
+
* follows it, on its own line or within `BODY_REACH` lines; then, when it holds a keyword of the rules, for each rule in
|
|
421
|
+
* the table's order and every match of it on the line, a match whose secret has an entropy at or below
|
|
422
|
+
* the rule's floor, or that the rule's own allowlist matches, being no hit. It reads every row alike
|
|
423
|
+
* (added, removed, context, or none), a line of any length and a line that ends with `\r`, and it is
|
|
424
|
+
* linear in the text. Generous: a removed line and a key in a comment hold as any other.
|
|
425
|
+
*/
|
|
426
|
+
export function credentialIn(text) {
|
|
427
|
+
const lines = text.split('\n');
|
|
428
|
+
const search = { scanned: -1, lastBody: -1 };
|
|
429
|
+
for (let at = 0; at < lines.length; at++) {
|
|
430
|
+
if (isPrivateKeyAt(lines, at, search))
|
|
431
|
+
return 'private-key';
|
|
432
|
+
const kind = ruleHitOn(lines[at] ?? '');
|
|
433
|
+
if (kind !== undefined)
|
|
434
|
+
return kind;
|
|
435
|
+
}
|
|
436
|
+
return undefined;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Why a section is held back, or undefined: `name` when its header's path (a non-empty one), a name
|
|
440
|
+
* its `diff --git` line holds (each word of it, so a header that cannot be read, or that is forged as
|
|
441
|
+
* `diff --git a/.env` or `diff --git a/.env b/.env c`, still names the `.env` it spells), or a path its
|
|
442
|
+
* own lines name is a secret's, else the kind of the first credential its text holds. The name is
|
|
443
|
+
* taken before the content, so a `.env` is held by its name whatever it holds. Generous: any one path
|
|
444
|
+
* the section names, a rename's old side included, holds it.
|
|
445
|
+
*/
|
|
446
|
+
export function secretRuleOf(section) {
|
|
447
|
+
const { path, text } = section;
|
|
448
|
+
const headerNamed = path !== undefined && path !== '' && isSecretName(path);
|
|
449
|
+
return headerNamed || headerNames(text).some(isSecretName) || namedPaths(text).some(named => isSecretName(named))
|
|
450
|
+
? 'name'
|
|
451
|
+
: credentialIn(text);
|
|
452
|
+
}
|
|
453
|
+
const GIT_HEADER = 'diff --git ';
|
|
454
|
+
const DELETED_MODE = 'deleted file mode ';
|
|
455
|
+
/** What opens the line that names the old side of a file. */
|
|
456
|
+
const OLD_SIDE = '--- ';
|
|
457
|
+
/** A hunk header whose new side is empty: the lines it counts are all removed. */
|
|
458
|
+
const EMPTIED_HUNK = /^@@ -\d+(?:,\d+)? \+0,0 @@/;
|
|
459
|
+
/** Whether a line, less its `\r`, is one git writes in the diff of a file it deletes whole, after the header. */
|
|
460
|
+
function isDeletionLine(line) {
|
|
461
|
+
return line.startsWith(DELETED_MODE)
|
|
462
|
+
|| line.startsWith('index ')
|
|
463
|
+
|| line.startsWith('-')
|
|
464
|
+
|| line === '+++ /dev/null'
|
|
465
|
+
|| line === '\'
|
|
466
|
+
|| (line.startsWith('Binary files ') && line.endsWith(' and /dev/null differ'))
|
|
467
|
+
|| EMPTIED_HUNK.test(line);
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Whether a section deletes its file whole: it starts with its `diff --git` header, holds a `deleted
|
|
471
|
+
* file mode` line, and every other line of it is one git writes for a deletion: `deleted file mode`,
|
|
472
|
+
* `index`, a `-` row (the `---` line is one), `+++ /dev/null`, `Binary files <path> and /dev/null
|
|
473
|
+
* differ`, a hunk header whose new side is `+0,0`, ``, and an empty last
|
|
474
|
+
* piece, each less a trailing `\r`. Any other line (a `+` or context row, a blank line in the
|
|
475
|
+
* middle, a mail's headers in a `format-patch` series, a `GIT binary patch`) makes it no whole
|
|
476
|
+
* deletion, so a forged or mixed section is a gap and never passes as one, and a file truncated to
|
|
477
|
+
* empty, which has no `deleted file mode`, is no deletion. Its first `--- ` line, when it has one (an
|
|
478
|
+
* empty or a binary file has none), also names the old side of a header that names one path on both
|
|
479
|
+
* sides (`namesOldSide`): a `.env` header over a `--- a/src/auth.ts` line is a forged section that
|
|
480
|
+
* would hide the removal of another file, so it is no deletion and a gap. Only the first such line
|
|
481
|
+
* counts: a later one is a removed row of the file.
|
|
482
|
+
*/
|
|
483
|
+
export function isWholeDeletion(text) {
|
|
484
|
+
const [header, ...rest] = text.split('\n');
|
|
485
|
+
if (!header?.startsWith(GIT_HEADER))
|
|
486
|
+
return false;
|
|
487
|
+
let deletesFile = false;
|
|
488
|
+
let oldSideChecked = false;
|
|
489
|
+
for (const [at, raw] of rest.entries()) {
|
|
490
|
+
const line = raw.endsWith('\r')
|
|
491
|
+
? raw.slice(0, -1)
|
|
492
|
+
: raw;
|
|
493
|
+
const allowed = line === ''
|
|
494
|
+
? at === rest.length - 1
|
|
495
|
+
: isDeletionLine(line);
|
|
496
|
+
if (!allowed)
|
|
497
|
+
return false;
|
|
498
|
+
if (line.startsWith(DELETED_MODE))
|
|
499
|
+
deletesFile = true;
|
|
500
|
+
if (!oldSideChecked && line.startsWith(OLD_SIDE)) {
|
|
501
|
+
if (!namesOldSide(header, line))
|
|
502
|
+
return false;
|
|
503
|
+
oldSideChecked = true;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return deletesFile;
|
|
507
|
+
}
|