Skip to content

Commit 96d5fbd

Browse files
fix(server): harden Bitbucket credential handling
- Ignore saved or env tokens that are not safe for an HTTP header. The HTTP stack rejects them with an error quoting the header, and that error reaches clients as a cause. - When a client echoes the redaction marker, move a plaintext token hand-edited into settings.json into the secret store instead of dropping it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 96a3b20 commit 96d5fbd

4 files changed

Lines changed: 65 additions & 4 deletions

File tree

‎apps/server/src/serverSettings.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,35 @@ it.layer(NodeServices.layer)("server settings", (it) => {
13421342
),
13431343
);
13441344

1345+
it.effect(
1346+
"moves a hand-edited Bitbucket token into the secret store when a client echoes the marker",
1347+
() =>
1348+
Effect.gen(function* () {
1349+
const serverConfig = yield* ServerConfig.ServerConfig;
1350+
const fileSystem = yield* FileSystem.FileSystem;
1351+
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1352+
yield* fileSystem.writeFileString(
1353+
serverConfig.settingsPath,
1354+
'{"bitbucket":{"email":"[email protected]","apiToken":"hand-edited-token"}}',
1355+
);
1356+
1357+
// The form resends the redacted token when only the email changes.
1358+
const forClient = ServerSettingsModule.redactServerSettingsForClient(
1359+
yield* serverSettings.getSettings,
1360+
).bitbucket;
1361+
const updated = yield* serverSettings.updateSettings({
1362+
bitbucket: { email: "[email protected]", apiToken: forClient.apiToken },
1363+
});
1364+
1365+
assert.equal(updated.bitbucket.apiToken, "hand-edited-token");
1366+
assert.equal((yield* serverSettings.getSettings).bitbucket.apiToken, "hand-edited-token");
1367+
assert.notInclude(
1368+
yield* fileSystem.readFileString(serverConfig.settingsPath),
1369+
"hand-edited-token",
1370+
);
1371+
}).pipe(Effect.provide(makeServerSettingsLayer())),
1372+
);
1373+
13451374
it.effect("materializes provider secrets for terminal environment resolution", () =>
13461375
Effect.gen(function* () {
13471376
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

‎apps/server/src/serverSettings.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,14 @@ const make = Effect.gen(function* () {
882882

883883
const bitbucket = { ...next.bitbucket };
884884
for (const field of BITBUCKET_SECRET_FIELDS) {
885-
const value = bitbucket[field];
886-
if (value === SECRET_REDACTED) continue;
885+
let value = bitbucket[field];
886+
if (value === SECRET_REDACTED) {
887+
// The marker keeps what is saved. A plaintext value hand-edited into settings.json
888+
// is not in the secret store yet, so move it there instead of dropping it.
889+
const inline = current.bitbucket[field];
890+
if (inline === SECRET_REDACTED || inline.length === 0) continue;
891+
value = inline;
892+
}
887893
if (value.length === 0 && current.bitbucket[field].length === 0) continue;
888894
const secretName = BITBUCKET_SECRET_NAMES[field];
889895
if (value.length === 0) {

‎apps/server/src/sourceControl/BitbucketApi.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,26 @@ it.effect("prefers credentials saved in settings over the environment, without a
542542
}).pipe(Effect.provide(layer));
543543
});
544544

545+
it.effect("never puts a saved token that is unsafe for an HTTP header on the wire", () => {
546+
const { execute, layer } = makeLayer({
547+
response: () => Response.json({ username: "bitbucket-user" }),
548+
});
549+
550+
return Effect.gen(function* () {
551+
const bitbucket = yield* BitbucketApi.BitbucketApi;
552+
const settings = yield* ServerSettings.ServerSettingsService;
553+
554+
// Fetch would reject this header with an error quoting the token, and that error reaches
555+
// clients. The unusable token is ignored, so the environment credential is used instead.
556+
yield* settings.updateSettings({ bitbucket: { accessToken: "saved\ntoken" } });
557+
yield* bitbucket.probeAuth;
558+
assert.strictEqual(
559+
execute.mock.calls.at(-1)?.[0].headers.authorization,
560+
`Basic ${btoa("[email protected]:token")}`,
561+
);
562+
}).pipe(Effect.provide(layer));
563+
});
564+
545565
it.effect("reports saved credentials as configured when Bitbucket cannot confirm them", () => {
546566
const { layer } = makeLayer({
547567
response: () => new Response(null, { status: 401 }),

‎apps/server/src/sourceControl/BitbucketApi.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,21 @@ type BitbucketCredential =
544544
| { readonly kind: "access-token"; readonly accessToken: string }
545545
| { readonly kind: "api-token"; readonly email: string; readonly apiToken: string };
546546

547+
/**
548+
* Visible ASCII only. A value the HTTP stack rejects makes it throw an error quoting the whole
549+
* header, and that error travels to clients as a cause, so an unusable token is treated as unset.
550+
*/
551+
const HEADER_SAFE = /^[\x21-\x7e]+$/u;
552+
547553
function credentialFrom(input: {
548554
readonly accessToken: string;
549555
readonly email: string;
550556
readonly apiToken: string;
551557
}): BitbucketCredential | null {
552-
if (input.accessToken.length > 0) {
558+
if (HEADER_SAFE.test(input.accessToken)) {
553559
return { kind: "access-token", accessToken: input.accessToken };
554560
}
555-
if (input.email.length > 0 && input.apiToken.length > 0) {
561+
if (HEADER_SAFE.test(input.email) && HEADER_SAFE.test(input.apiToken)) {
556562
return { kind: "api-token", email: input.email, apiToken: input.apiToken };
557563
}
558564
return null;

0 commit comments

Comments
 (0)