Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

test(backend): the machine credential tamper test always changes the value (#1958)

0xken1586cfb4a6489b
diff --git a/backend/src/lib/machineCredential.test.ts b/backend/src/lib/machineCredential.test.ts
index 4ae3595b8..24c2458f3 100644
--- a/backend/src/lib/machineCredential.test.ts
+++ b/backend/src/lib/machineCredential.test.ts
@@ -6,12 +6,30 @@ vi.mock('../config/env.js', () => ({
import { decryptMachineCredential, encryptMachineCredential } from './machineCredential.js'
+/** The envelope with the first character of its ciphertext changed to another: a first base64url character carries six
+ * whole bits, so the bytes always differ. Writing a fixed character changed nothing in the 1 run in 64 where the
+ * random value already had it (#1941's CI, 2026-10-28). */
+const tampered = (envelope: string) => {
+ const at = envelope.lastIndexOf('.') + 1
+ return `${envelope.slice(0, at)}${envelope[at] === 'A' ? 'B' : 'A'}${envelope.slice(at + 1)}`
+}
+
describe('machine credential envelope', () => {
it('round-trips without storing plaintext and rejects tampering', () => {
const secret = 'campaign-secret'
const encrypted = encryptMachineCredential(secret)
expect(encrypted).not.toContain(secret)
expect(decryptMachineCredential(encrypted)).toBe(secret)
- expect(() => decryptMachineCredential(`${encrypted.slice(0, -1)}x`)).toThrow()
+ expect(() => decryptMachineCredential(tampered(encrypted))).toThrow()
+ })
+
+ it('rejects tampering whatever character the random value has where it is changed', () => {
+ // Each first character a ciphertext may start with, the one the tamper writes among them, found by encrypting again.
+ for (const first of ['A', 'B', 'x']) {
+ let encrypted = encryptMachineCredential('campaign-secret')
+ for (let i = 0; encrypted[encrypted.lastIndexOf('.') + 1] !== first && i < 10_000; i++) encrypted = encryptMachineCredential('campaign-secret')
+ expect(encrypted[encrypted.lastIndexOf('.') + 1]).toBe(first)
+ expect(() => decryptMachineCredential(tampered(encrypted))).toThrow()
+ }
})
})