|
| 1 | +// A health check writes its response sample into `connection.last_health`, so |
| 2 | +// whatever the sample carries is persisted. The operation being probed is |
| 3 | +// user-chosen from the plugin's catalog, which means it can be a key-listing |
| 4 | +// endpoint just as easily as a `/me`. |
| 5 | +// |
| 6 | +// These use real response bodies of the shape those endpoints return, because |
| 7 | +// the property under test is what survives the walk into the database. |
| 8 | + |
| 9 | +import { describe, expect, it } from "@effect/vitest"; |
| 10 | + |
| 11 | +import { extractResponseFields, REDACTED_SAMPLE_VALUE } from "./health-check"; |
| 12 | + |
| 13 | +/** The sample as a path -> value lookup, which is how the assertions read. */ |
| 14 | +const byPath = (data: unknown): Record<string, string> => |
| 15 | + Object.fromEntries(extractResponseFields(data).map((f) => [f.path, f.value])); |
| 16 | + |
| 17 | +describe("health-check response sample redaction", () => { |
| 18 | + it("redacts credential-named leaves while keeping the identity fields", () => { |
| 19 | + const fields = byPath({ |
| 20 | + email: "alex@example.com", |
| 21 | + login: "alex", |
| 22 | + id: 4711, |
| 23 | + api_key: "sk-live-must-not-be-persisted", |
| 24 | + refresh_token: "rt-must-not-be-persisted", |
| 25 | + session: "sess-must-not-be-persisted", |
| 26 | + }); |
| 27 | + |
| 28 | + // The reason the sample exists still works. |
| 29 | + expect(fields.email).toBe("alex@example.com"); |
| 30 | + expect(fields.login).toBe("alex"); |
| 31 | + expect(fields.id).toBe("4711"); |
| 32 | + |
| 33 | + expect(fields.api_key).toBe(REDACTED_SAMPLE_VALUE); |
| 34 | + expect(fields.refresh_token).toBe(REDACTED_SAMPLE_VALUE); |
| 35 | + expect(fields.session).toBe(REDACTED_SAMPLE_VALUE); |
| 36 | + }); |
| 37 | + |
| 38 | + it("redacts nested and array-borne credentials, not just top-level ones", () => { |
| 39 | + // What a key-listing endpoint actually returns. This is the case a scrub |
| 40 | + // of the connection's own value cannot catch: these are different secrets. |
| 41 | + const fields = byPath({ |
| 42 | + keys: [ |
| 43 | + { name: "prod", token: "sk-prod-must-not-be-persisted" }, |
| 44 | + { name: "staging", token: "sk-staging-must-not-be-persisted" }, |
| 45 | + ], |
| 46 | + account: { billing: { secret: "whsec-must-not-be-persisted" } }, |
| 47 | + }); |
| 48 | + |
| 49 | + expect(fields["keys.0.name"]).toBe("prod"); |
| 50 | + expect(fields["keys.0.token"]).toBe(REDACTED_SAMPLE_VALUE); |
| 51 | + expect(fields["keys.1.token"]).toBe(REDACTED_SAMPLE_VALUE); |
| 52 | + expect(fields["account.billing.secret"]).toBe(REDACTED_SAMPLE_VALUE); |
| 53 | + }); |
| 54 | + |
| 55 | + it("redacts a bare array of secrets, where only the enclosing key names them", () => { |
| 56 | + // `{"tokens": ["sk-…"]}` yields the paths `tokens.0`, `tokens.1`. Their last |
| 57 | + // segment is an array index, so a check that reads the literal leaf finds |
| 58 | + // "0" and lets the secret straight through into `connection.last_health`. |
| 59 | + const fields = byPath({ |
| 60 | + tokens: ["sk-live-must-not-be-persisted", "sk-test-must-not-be-persisted"], |
| 61 | + names: ["prod", "staging"], |
| 62 | + }); |
| 63 | + |
| 64 | + expect(fields["tokens.0"]).toBe(REDACTED_SAMPLE_VALUE); |
| 65 | + expect(fields["tokens.1"]).toBe(REDACTED_SAMPLE_VALUE); |
| 66 | + |
| 67 | + // The index walk-back stops at the nearest NAMED segment, so an innocent |
| 68 | + // collection is still shown in full. |
| 69 | + expect(fields["names.0"]).toBe("prod"); |
| 70 | + expect(fields["names.1"]).toBe("staging"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("redacts camelCase credential keys, in both spellings", () => { |
| 74 | + // `accessToken` has no non-letter before `Token`, so a matcher that only |
| 75 | + // looks for a separator reads it as innocent. camelCase is the dominant |
| 76 | + // spelling in JSON APIs, so this is not an edge case. |
| 77 | + const fields = byPath({ |
| 78 | + accessToken: "at-must-not-be-persisted", |
| 79 | + refreshToken: "rt-must-not-be-persisted", |
| 80 | + clientSecret: "cs-must-not-be-persisted", |
| 81 | + privateKey: "pk-must-not-be-persisted", |
| 82 | + sessionId: "sid-must-not-be-persisted", |
| 83 | + // Still matched as one contiguous word, which is how it has always |
| 84 | + // matched: splitting on the case boundary alone would lose it. |
| 85 | + apiKey: "ak-must-not-be-persisted", |
| 86 | + }); |
| 87 | + |
| 88 | + for (const value of Object.values(fields)) { |
| 89 | + expect(value).toBe(REDACTED_SAMPLE_VALUE); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it("does not redact camelCase keys that merely start with a matching word", () => { |
| 94 | + // The other direction of the same change: exposing the case boundary must |
| 95 | + // not turn ordinary fields into blanks. |
| 96 | + const fields = byPath({ |
| 97 | + author: "alex", |
| 98 | + authors: "alex, sam", |
| 99 | + authorName: "Alex", |
| 100 | + privacyLevel: "public", |
| 101 | + tokenizer: "bpe", |
| 102 | + }); |
| 103 | + |
| 104 | + expect(fields.author).toBe("alex"); |
| 105 | + expect(fields.authors).toBe("alex, sam"); |
| 106 | + expect(fields.authorName).toBe("Alex"); |
| 107 | + expect(fields.privacyLevel).toBe("public"); |
| 108 | + expect(fields.tokenizer).toBe("bpe"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("redacts a secret under an innocent leaf inside a credential-named array", () => { |
| 112 | + // The shape a key-listing endpoint actually returns. The nearest named |
| 113 | + // segment is the harmless `value`; only the array's own key says what the |
| 114 | + // collection holds. |
| 115 | + const fields = byPath({ |
| 116 | + api_keys: [{ name: "prod", value: "sk-live-must-not-be-persisted" }], |
| 117 | + names: ["prod", "staging"], |
| 118 | + results: [{ value: "42" }], |
| 119 | + }); |
| 120 | + |
| 121 | + expect(fields["api_keys.0.value"]).toBe(REDACTED_SAMPLE_VALUE); |
| 122 | + |
| 123 | + // An array whose key names nothing is untouched, at either depth. |
| 124 | + expect(fields["names.0"]).toBe("prod"); |
| 125 | + expect(fields["results.0.value"]).toBe("42"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("scrubs a known credential value before truncating, not after", () => { |
| 129 | + // A credential can sit under a key that names nothing — the body echoing |
| 130 | + // back the key it was authenticated with. Only the caller can recognise it, |
| 131 | + // by exact value. Truncating first cuts it to a 120-char prefix that the |
| 132 | + // exact-value scrub no longer matches, and that prefix is what reaches |
| 133 | + // `connection.last_health`. |
| 134 | + const secret = `sk-live-${"x".repeat(200)}`; |
| 135 | + expect(secret.length).toBeGreaterThan(120); |
| 136 | + |
| 137 | + const sample = extractResponseFields( |
| 138 | + { data: secret }, |
| 139 | + { scrub: (value) => value.split(secret).join(REDACTED_SAMPLE_VALUE) }, |
| 140 | + ); |
| 141 | + |
| 142 | + expect(sample[0]?.value).toBe(REDACTED_SAMPLE_VALUE); |
| 143 | + // Not merely "shorter than the secret": assert no recognisable prefix of it |
| 144 | + // survived, which is the actual leak. |
| 145 | + expect(sample[0]?.value).not.toContain("sk-live-"); |
| 146 | + }); |
| 147 | + |
| 148 | + it("still truncates a long value the scrub does not recognise", () => { |
| 149 | + // Reordering must not disable the cap for everything else. |
| 150 | + const long = "y".repeat(400); |
| 151 | + const sample = extractResponseFields({ blob: long }, { scrub: (value) => value }); |
| 152 | + |
| 153 | + expect(sample[0]?.value).toBe(`${"y".repeat(120)}...`); |
| 154 | + }); |
| 155 | + |
| 156 | + it("keeps the field visible so the preview still shows the shape", () => { |
| 157 | + // Dropping the row would change what the picker displays. Redacting the |
| 158 | + // value keeps the response shape legible without persisting the secret. |
| 159 | + const sample = extractResponseFields({ api_key: "sk-live-x" }); |
| 160 | + |
| 161 | + expect(sample).toHaveLength(1); |
| 162 | + expect(sample[0]?.path).toBe("api_key"); |
| 163 | + }); |
| 164 | + |
| 165 | + it("does not redact identity keys that merely contain a matching substring", () => { |
| 166 | + // `author` contains "auth". Matching it would silently blank a normal |
| 167 | + // field, which is how an over-eager redactor makes the feature useless. |
| 168 | + const fields = byPath({ author: "alex", authorization: "Bearer x" }); |
| 169 | + |
| 170 | + expect(fields.author).toBe("alex"); |
| 171 | + expect(fields.authorization).toBe(REDACTED_SAMPLE_VALUE); |
| 172 | + }); |
| 173 | + |
| 174 | + it("POSITIVE CONTROL: an unredacted body does come through verbatim", () => { |
| 175 | + // Proves these assertions can fail. Without it, an extractor that returned |
| 176 | + // nothing, or redacted everything, would satisfy the checks above. |
| 177 | + const fields = byPath({ email: "alex@example.com", plan: "pro" }); |
| 178 | + |
| 179 | + expect(fields.email).toBe("alex@example.com"); |
| 180 | + expect(fields.plan).toBe("pro"); |
| 181 | + expect(Object.values(fields)).not.toContain(REDACTED_SAMPLE_VALUE); |
| 182 | + }); |
| 183 | +}); |
0 commit comments