-
Notifications
You must be signed in to change notification settings - Fork 33
test: property-check env boolean parsing and policy key/tag contracts #601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ndycode
wants to merge
3
commits into
main
Choose a base branch
from
claude/audit-82-env-policy-property
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import * as fc from "fast-check"; | ||
| import { parseBooleanEnv } from "../../lib/env-parsing.js"; | ||
| import { | ||
| getAccountPolicyKey, | ||
| normalizeAccountPolicyTag, | ||
| } from "../../lib/account-policy.js"; | ||
|
|
||
| const TRUE_LITERALS = ["1", "true", "yes"] as const; | ||
| const FALSE_LITERALS = ["0", "false", "no"] as const; | ||
|
|
||
| const arbWhitespace = fc | ||
| .array(fc.constantFrom(" ", "\t"), { minLength: 0, maxLength: 4 }) | ||
| .map((chars) => chars.join("")); | ||
|
|
||
| const arbCasing = fc.constantFrom( | ||
| (text: string) => text, | ||
| (text: string) => text.toUpperCase(), | ||
| (text: string) => | ||
| [...text] | ||
| .map((char, index) => (index % 2 === 0 ? char.toUpperCase() : char)) | ||
| .join(""), | ||
| ); | ||
|
|
||
| describe("parseBooleanEnv property invariants", () => { | ||
| it("recognized literals parse to their boolean under any casing and padding", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| fc.constantFrom(...TRUE_LITERALS, ...FALSE_LITERALS), | ||
| arbCasing, | ||
| arbWhitespace, | ||
| arbWhitespace, | ||
| (literal, casing, before, after) => { | ||
| const parsed = parseBooleanEnv(`${before}${casing(literal)}${after}`); | ||
| expect(parsed).toBe( | ||
| (TRUE_LITERALS as readonly string[]).includes(literal), | ||
| ); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("everything else returns undefined, never a boolean", () => { | ||
| const recognized = new Set<string>([...TRUE_LITERALS, ...FALSE_LITERALS]); | ||
| fc.assert( | ||
| fc.property(fc.string({ maxLength: 12 }), (value) => { | ||
| fc.pre(!recognized.has(value.trim().toLowerCase())); | ||
| expect(parseBooleanEnv(value)).toBeUndefined(); | ||
| }), | ||
| ); | ||
| expect(parseBooleanEnv(undefined)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("account policy key/tag property invariants", () => { | ||
| it("policy keys are stable sha256 handles: identity-insensitive to email case, never leak the identity", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| // " " exercises the accountId?.trim() || fall-through: a defined | ||
| // but whitespace-only accountId must defer to the email identity. | ||
| fc.option(fc.constantFrom("acc-1", "acc-2", "ACC-1 ", " "), { | ||
| nil: undefined, | ||
| }), | ||
| fc.option(fc.constantFrom("user@x.test", "other@x.test"), { | ||
| nil: undefined, | ||
| }), | ||
| arbCasing, | ||
| (accountId, email, casing) => { | ||
| const key = getAccountPolicyKey({ accountId, email }); | ||
| expect(key).toMatch(/^sha256:[0-9a-f]{64}$/); | ||
| // The key must not leak the raw identity (it is written to a | ||
| // policy file keyed by hash precisely to avoid that). | ||
| if (email) expect(key.includes(email)).toBe(false); | ||
|
|
||
| // accountId wins over email; email matches case-insensitively; | ||
| // no identity at all degrades to the shared "unknown" bucket. | ||
| if (accountId?.trim()) { | ||
| expect( | ||
| getAccountPolicyKey({ accountId, email: "different@x.test" }), | ||
| ).toBe(key); | ||
| } else if (email) { | ||
| expect( | ||
| getAccountPolicyKey({ accountId: undefined, email: casing(email) }), | ||
| ).toBe(key); | ||
| } else { | ||
| expect(getAccountPolicyKey({})).toBe(key); | ||
| } | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("normalized tags are idempotent fixpoints in the [a-z0-9._-]{1,64} language or null", () => { | ||
| fc.assert( | ||
| fc.property(fc.string({ maxLength: 100 }), (raw) => { | ||
| const tag = normalizeAccountPolicyTag(raw); | ||
| // Forward direction of the iff: whitespace-only input must | ||
| // normalize away (the branch below pins the reverse). | ||
| if (raw.trim() === "") { | ||
| expect(tag).toBeNull(); | ||
| } | ||
| if (tag === null) { | ||
| // Disallowed runs are replaced WITH a dash rather than dropped, | ||
| // so the only way to normalize away entirely is an input that | ||
| // trims to nothing. | ||
| expect(raw.trim()).toBe(""); | ||
| return; | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
| expect(tag.length).toBeGreaterThan(0); | ||
| expect(tag.length).toBeLessThanOrEqual(64); | ||
| expect(/^[a-z0-9._-]+$/.test(tag)).toBe(true); | ||
| // Idempotence: a normalized tag survives re-normalization verbatim. | ||
| expect(normalizeAccountPolicyTag(tag)).toBe(tag); | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.