diff --git a/server.json b/server.json index 18de93b..ee9bf08 100644 --- a/server.json +++ b/server.json @@ -48,11 +48,6 @@ "name": "DCD_API_URL", "description": "Override the API base URL (e.g. for a non-production environment).", "isRequired": false - }, - { - "name": "DCD_ENCRYPT", - "description": "Uploads (app binary, flows, env vars) are encrypted on your machine by default. Set to 0 to upload them without envelope encryption; a run's encrypt input overrides this.", - "isRequired": false } ] } diff --git a/src/commands/cloud.ts b/src/commands/cloud.ts index ff6cd73..59cc44f 100644 --- a/src/commands/cloud.ts +++ b/src/commands/cloud.ts @@ -197,9 +197,8 @@ export const cloudCommand = defineCommand({ const googlePlay = Boolean(args['google-play']); const ignoreShaCheck = Boolean(args['ignore-sha-check']); // Client-side envelope encryption of every sensitive artifact: the - // binary (#1138), the flow zip (#1151), and env vars (#1152). Kept as - // given (--no-encrypt is an explicit false) and resolved once the - // compatibility data, which carries the server default, is in. + // binary (#1138), the flow zip (#1151), and env vars (#1152). Always + // on; read only so --no-encrypt can be reported as ignored. const encryptFlag = args.encrypt as boolean | undefined; const includeTags = coerceArray( collectRepeatedFlag(rawArgs, ['--include-tags']), @@ -390,10 +389,7 @@ export const cloudCommand = defineCommand({ } const encryption = await checkKekForEncryption( - resolveEncryption({ - flag: encryptFlag, - serverDefault: compatibilityData.encryption?.defaultOn, - }), + resolveEncryption({ flag: encryptFlag }), apiUrl, ); const encrypt = encryption.enabled; @@ -811,10 +807,6 @@ export const cloudCommand = defineCommand({ return; } - if (encrypt && encryption.source === 'server') { - out(ui.note('Encrypting uploads; opt out with --no-encrypt')); - } - if (!finalBinaryId) { if (!finalAppFile) { throw new CliError( diff --git a/src/commands/upload.ts b/src/commands/upload.ts index 3aca6f2..7681a1a 100644 --- a/src/commands/upload.ts +++ b/src/commands/upload.ts @@ -8,7 +8,6 @@ import { uploadBinary, verifyAppZip } from '../methods.js'; import { telemetry } from '../services/telemetry.service.js'; import { resolveAuth } from '../utils/auth.js'; import { CliError, logger } from '../utils/cli.js'; -import { fetchCompatibilityData } from '../utils/compatibility.js'; import { resolveApiUrl } from '../utils/config-store.js'; import { checkKekForEncryption, resolveEncryption } from '../utils/envelope.js'; import { downloadExpoUrl, extractTarGz, findAppBundle, isUrl } from '../utils/expo.js'; @@ -46,31 +45,16 @@ export const uploadCommand = defineCommand({ const apiUrl = resolveApiUrl(args['api-url'] as string | undefined); const appUrl = args['app-url'] as string | undefined; const ignoreShaCheck = Boolean(args['ignore-sha-check']); - // Kept as given, so --no-encrypt is an explicit false and a missing flag - // leaves DCD_ENCRYPT and then the server default to decide. + // Uploads are always encrypted. Read only so --no-encrypt can be + // reported as ignored. const encryptFlag = args.encrypt as boolean | undefined; const debug = Boolean(args.debug); const positional = args.appFile as string | undefined; const auth = await resolveAuth({ apiKeyFlag }); - // Same order as `dcd cloud`, whose compatibility fetch carries the org's - // encryption default. `dcd upload` never needed that data before, so a - // failed fetch leaves the default off rather than failing the upload. - let serverDefault: boolean | undefined; - try { - const compatibilityData = await fetchCompatibilityData(apiUrl, auth); - serverDefault = compatibilityData.encryption?.defaultOn; - } catch (error) { - if (debug) { - out( - `[DEBUG] Failed to fetch compatibility data, so the org's encryption default is off: ${error instanceof Error ? error.message : String(error)}`, - ); - } - } - const encryption = await checkKekForEncryption( - resolveEncryption({ flag: encryptFlag, serverDefault }), + resolveEncryption({ flag: encryptFlag }), apiUrl, ); if (encryption.notice) out(ui.note(encryption.notice)); @@ -112,12 +96,7 @@ export const uploadCommand = defineCommand({ out(ui.section('Uploading app binary')); out( - ui.branch([ - ...ui.fields([['file', colors.highlight(resolvedFile)]]), - ...(encryption.enabled && encryption.source === 'server' - ? [ui.note('Encrypting uploads; opt out with --no-encrypt')] - : []), - ]), + ui.branch(ui.fields([['file', colors.highlight(resolvedFile)]])), ); const appBinaryId = await uploadBinary({ diff --git a/src/config/flags/binary.flags.ts b/src/config/flags/binary.flags.ts index 96111f3..83a4cfa 100644 --- a/src/config/flags/binary.flags.ts +++ b/src/config/flags/binary.flags.ts @@ -27,6 +27,6 @@ export const binaryFlags = { encrypt: { type: 'boolean', description: - 'Encrypt the app binary, flow zip, and env vars before upload (client-side envelope encryption; each gets its own key). On by default; --no-encrypt or DCD_ENCRYPT=0 turns it off. The binary is still deduplicated across runs, so an unchanged app is not re-uploaded.', + 'No longer needed: the app binary, flow zip and env vars are always encrypted before upload (client-side envelope encryption; each gets its own key). Kept so existing scripts still run. --no-encrypt and DCD_ENCRYPT=0 are ignored with a warning.', }, } as const satisfies ArgsDef; diff --git a/src/mcp/tools/run-cloud-test.ts b/src/mcp/tools/run-cloud-test.ts index 1910a82..4db0fc1 100644 --- a/src/mcp/tools/run-cloud-test.ts +++ b/src/mcp/tools/run-cloud-test.ts @@ -128,12 +128,6 @@ export function registerRunCloudTest(server: McpServer): void { .optional() .describe('Force re-upload of the binary even if an identical one exists'), dryRun: z.boolean().optional().describe('Preview flows that would run without submitting'), - encrypt: z - .boolean() - .optional() - .describe( - 'true encrypts the binary, flow zip and env vars client-side before upload; false never does. Omit to follow DCD_ENCRYPT, then the default, which is on.', - ), wait: z .boolean() .optional() @@ -232,15 +226,9 @@ export function registerRunCloudTest(server: McpServer): void { }); } - // Client-side envelope encryption (binary/flow/env), in the same order - // as `dcd cloud`: the encrypt input, DCD_ENCRYPT, then the server default. - const encryption = await checkKekForEncryption( - resolveEncryption({ - flag: args.encrypt, - serverDefault: compatibilityData.encryption?.defaultOn, - }), - apiUrl, - ); + // Client-side envelope encryption (binary/flow/env), always on, as + // in `dcd cloud`. + const encryption = await checkKekForEncryption(resolveEncryption(), apiUrl); const encrypt = encryption.enabled; if (encryption.notice) logStderr(encryption.notice); // Filled in as each part is encrypted; sent on this call's telemetry. @@ -251,9 +239,6 @@ export function registerRunCloudTest(server: McpServer): void { source: encryption.source, }; telemetryExtra.encrypt = encryptTelemetry; - if (encrypt && encryption.source === 'server') { - logStderr('Encrypting uploads; opt out with encrypt: false'); - } // Resolve the binary: existing id, or upload the local file. let appBinaryId = args.appBinaryId; diff --git a/src/methods.ts b/src/methods.ts index c1183d5..4817908 100644 --- a/src/methods.ts +++ b/src/methods.ts @@ -281,8 +281,8 @@ async function encryptUploadSource( const kek = await resolveKekPublicKey(apiUrl); if (!kek) { throw new Error( - 'Encryption is on (from --encrypt, DCD_ENCRYPT or the default) but no KEK public key is configured for this API. ' + - 'Set DCD_BINARY_KEK_PUBLIC=:, or pass --no-encrypt to upload without encryption.', + 'Encryption is on but no KEK public key is configured for this API. ' + + 'Set DCD_BINARY_KEK_PUBLIC=:.', ); } diff --git a/src/services/test-submission.service.ts b/src/services/test-submission.service.ts index fa9ba6f..54aa4c6 100644 --- a/src/services/test-submission.service.ts +++ b/src/services/test-submission.service.ts @@ -241,8 +241,8 @@ export class TestSubmissionService { const kek = await resolveKekPublicKey(apiUrl); if (!kek) { throw new Error( - 'Encryption is on (from --encrypt, DCD_ENCRYPT or the default) but no KEK public key is configured for this API. ' + - 'Set DCD_BINARY_KEK_PUBLIC=:, or pass --no-encrypt to upload without encryption.', + 'Encryption is on but no KEK public key is configured for this API. ' + + 'Set DCD_BINARY_KEK_PUBLIC=:.', ); } const flow = encryptFlowBuffer(plaintextZip, kek); diff --git a/src/utils/app-upload.ts b/src/utils/app-upload.ts index e60ce46..1880be4 100644 --- a/src/utils/app-upload.ts +++ b/src/utils/app-upload.ts @@ -3,7 +3,6 @@ import { rm } from 'node:fs/promises'; import { uploadBinary, verifyAppZip } from '../methods.js'; import type { AuthContext } from '../types/domain/auth.types.js'; import { CliError } from './cli.js'; -import { fetchCompatibilityData } from './compatibility.js'; import { checkKekForEncryption, resolveEncryption } from './envelope.js'; import { extractTarGz, findAppBundle } from './expo.js'; @@ -47,8 +46,8 @@ export interface UploadLocalAppOptions { /** * Upload a local build the way `dcd upload` does, and return its binary id: - * an Expo `.tar.gz` is unpacked to its `.app`, a `.zip` is checked, and the - * org's encryption default applies. URLs are not handled here: `dcd device` + * an Expo `.tar.gz` is unpacked to its `.app`, a `.zip` is checked, and it + * is encrypted before upload. URLs are not handled here: `dcd device` * hands those to the API's install-from-URL instead. */ export async function uploadLocalApp(opts: UploadLocalAppOptions): Promise { @@ -60,17 +59,7 @@ export async function uploadLocalApp(opts: UploadLocalAppOptions): Promise 0) { + decision.notice = `Ignoring ${ignored.join(' and ')}: uploads are always encrypted.`; + } + return decision; } /** * Check that `apiUrl` has a KEK before a run encrypts against it: either * `DCD_BINARY_KEK_PUBLIC` is set or the API publishes one on `GET /cli-config` - * (see resolveKekPublicKey). Without one, an explicit `--encrypt` is an error, - * while encryption from DCD_ENCRYPT or the server default is skipped with a - * notice, so a run against an API that publishes no KEK uploads in plaintext - * as before. An API that cannot be asked at all is an error whatever decided - * encryption: silently falling back to plaintext would be the one outcome the - * org asked not to have. + * (see resolveKekPublicKey). An API that publishes no KEK gets plaintext + * uploads with a notice: that is the server's own switch, and the only way to + * turn encryption off. An API that cannot be asked at all is an error, since + * silently falling back to plaintext would hide the failure. */ export async function checkKekForEncryption( decision: EncryptionDecision, @@ -124,17 +111,10 @@ export async function checkKekForEncryption( ): Promise { if (!decision.enabled || (await resolveKekPublicKey(apiUrl))) return decision; - if (decision.source === 'flag') { - throw new Error( - `--encrypt needs the KEK public key of the API it uploads to, and ${apiUrl} does not publish one. ` + - 'Set DCD_BINARY_KEK_PUBLIC=: to encrypt against it, or drop --encrypt.', - ); - } - return { enabled: false, notice: `Not encrypting uploads: ${apiUrl} does not publish a KEK public key. Set DCD_BINARY_KEK_PUBLIC to encrypt against it.`, - source: decision.source, + source: 'no_kek', }; } diff --git a/test/unit/device-bundle.test.ts b/test/unit/device-bundle.test.ts index 9e037c4..6433b9a 100644 --- a/test/unit/device-bundle.test.ts +++ b/test/unit/device-bundle.test.ts @@ -5,7 +5,7 @@ import * as os from 'node:os'; import * as path from 'node:path'; import StreamZip from 'node-stream-zip'; -import { clearCompatibilityCache } from '../../src/utils/compatibility.js'; +import { resetCliConfigCache } from '../../src/config/cli-config.js'; import { bundleFiles, flowFileBundle, uploadFlowBundle } from '../../src/utils/device-bundle.js'; const API = 'https://api.devicecloud.dev'; @@ -22,7 +22,6 @@ describe('dcd device run : flow bundles', () => { const ORIGINAL_ENV = { ...process.env }; let dir: string; let calls: Call[]; - let encryptionDefault: boolean; beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dcd-bundle-test-')); @@ -35,8 +34,7 @@ describe('dcd device run : flow bundles', () => { fs.writeFileSync(path.join(dir, 'node_modules', 'x.js'), ''); delete process.env.DCD_ENCRYPT; delete process.env.DCD_ENCRYPT_BINARIES; - encryptionDefault = false; - clearCompatibilityCache(); + resetCliConfigCache(); calls = []; globalThis.fetch = (async (input: unknown, init?: RequestInit) => { const url = String(input); @@ -46,10 +44,10 @@ describe('dcd device run : flow bundles', () => { body: init?.body as Uint8Array | string | undefined, }; calls.push(call); - if (url.includes('/results/compatibility')) { - return new Response( - JSON.stringify({ statusCode: 200, data: { encryption: { defaultOn: encryptionDefault } } }), - ); + // The API publishes no KEK, so bundles go up in plaintext unless a + // test sets DCD_BINARY_KEK_PUBLIC. + if (url.endsWith('/cli-config')) { + return new Response(JSON.stringify({ supabase_publishable_key: 'sb_publishable_test' })); } if (url.endsWith('/device/bundles/upload-url')) { @@ -76,7 +74,7 @@ describe('dcd device run : flow bundles', () => { }); afterEach(() => { - clearCompatibilityCache(); + resetCliConfigCache(); globalThis.fetch = realFetch; process.env = { ...ORIGINAL_ENV }; fs.rmSync(dir, { force: true, recursive: true }); @@ -195,7 +193,7 @@ describe('dcd device run : flow bundles', () => { }); }); - it('uploads the zip to both copies and registers what landed', async () => { + it('uploads the zip to both copies and registers what landed (no KEK, so plaintext)', async () => { const bundle = await uploadFlowBundle({ apiUrl: API, auth, dir, log: () => {} }); expect(bundle.id).to.equal('b-1'); expect(bundle.files).to.equal(2); @@ -223,11 +221,11 @@ describe('dcd device run : flow bundles', () => { await zip.close(); }); - it("encrypts the zip when the org's default says so", async () => { + it('encrypts the zip whenever there is a KEK, even with DCD_ENCRYPT=0', async () => { const { publicKey } = generateKeyPairSync('x25519'); const raw = publicKey.export({ type: 'spki', format: 'der' }).subarray(12); process.env.DCD_BINARY_KEK_PUBLIC = `1:${raw.toString('base64')}`; - encryptionDefault = true; + process.env.DCD_ENCRYPT = '0'; const bundle = await uploadFlowBundle({ apiUrl: API, auth, dir, log: () => {} }); expect(bundle.encrypted).to.equal(true); diff --git a/test/unit/device-command.test.ts b/test/unit/device-command.test.ts index e14e60f..585864a 100644 --- a/test/unit/device-command.test.ts +++ b/test/unit/device-command.test.ts @@ -956,6 +956,8 @@ describe('dcd device', () => { return { body: { id: 'b-1', upload: { path: 'p', signedUrl: 'https://sb/put' }, mirror: null } }; } + // No KEK published, so the bundle goes up in plaintext. + if (call.url.endsWith('/cli-config')) return { body: { supabase_publishable_key: 'sb_publishable_test' } }; if (call.url === 'https://sb/put') return { body: {} }; if (call.url.endsWith('/device/bundles')) { return { body: { bundle: { id: 'b-1', sha: 's', bytes: 1, encrypted: false, created_at: 'now' } } }; @@ -1475,6 +1477,8 @@ describe('dcd device', () => { return { body: { id: 'b-1', upload: { path: 'p', signedUrl: 'https://sb/put' }, mirror: null } }; } + // No KEK published, so the bundle goes up in plaintext. + if (call.url.endsWith('/cli-config')) return { body: { supabase_publishable_key: 'sb_publishable_test' } }; if (call.url === 'https://sb/put') return { body: {} }; if (call.url.endsWith('/device/bundles')) { return { body: { bundle: { id: 'b-1', sha: 's', bytes: 1, encrypted: false, created_at: 'now' } } }; diff --git a/test/unit/encrypt-help.test.ts b/test/unit/encrypt-help.test.ts index 871ce2a..616f635 100644 --- a/test/unit/encrypt-help.test.ts +++ b/test/unit/encrypt-help.test.ts @@ -9,8 +9,9 @@ import { cloudCommand } from '../../src/commands/cloud.js'; import { uploadCommand } from '../../src/commands/upload.js'; /** - * Client-side encryption is on by default (dcd#1138), so --encrypt is listed - * in --help, and --no-encrypt, the per-run opt-out, must parse. + * Uploads are always encrypted (dcd#1138). --encrypt stays as a no-op so old + * scripts still run, and --no-encrypt must still parse so it can be ignored + * with a warning rather than fail. */ const commands: Array<[string, CommandDef]> = [ ['cloud', cloudCommand as CommandDef], @@ -24,10 +25,11 @@ async function argsOf(cmd: CommandDef): Promise { describe('--encrypt flag', () => { for (const [name, cmd] of commands) { describe(`dcd ${name}`, () => { - it('lists encrypt in the usage, with the opt-out', async () => { + it('lists encrypt in the usage, as no longer needed', async () => { const usage = await renderUsage(cmd); expect(usage).to.contain('--encrypt'); - expect(usage).to.contain('--no-encrypt or DCD_ENCRYPT=0 turns it off'); + expect(usage).to.contain('always encrypted'); + expect(usage).to.not.contain('turns it off'); }); it('parses --encrypt and --no-encrypt', async () => { diff --git a/test/unit/encryption-decision.test.ts b/test/unit/encryption-decision.test.ts index 2621498..75e26e1 100644 --- a/test/unit/encryption-decision.test.ts +++ b/test/unit/encryption-decision.test.ts @@ -1,167 +1,79 @@ import { expect } from 'chai'; -import { - type EncryptionDecision, - resolveEncryption, -} from '../../src/utils/envelope.js'; +import { resolveEncryption } from '../../src/utils/envelope.js'; /** - * The order that decides whether a run encrypts (dcd#1138): an explicit flag, - * then DCD_ENCRYPT (or its legacy alias), then the server default from the - * compatibility data, then off. Every case passes `env` explicitly so the - * developer's own shell can't leak in. + * Uploads are always encrypted (dcd#1138). The old opt-outs, --no-encrypt and + * DCD_ENCRYPT=0 (or its legacy alias), are ignored with a warning. Every case + * passes `env` explicitly so the developer's own shell can't leak in. */ describe('resolveEncryption', () => { - const on = (source: EncryptionDecision['source']) => ({ enabled: true, source }); - const off = (source: EncryptionDecision['source']) => ({ enabled: false, source }); + const ON = { enabled: true, source: 'always' }; - it('is off when nothing is set', () => { - expect(resolveEncryption({ env: {} })).to.deep.equal(off('off')); + it('is on when nothing is set', () => { + expect(resolveEncryption({ env: {} })).to.deep.equal(ON); }); - it('treats a missing server field (an older API) as off', () => { - expect(resolveEncryption({ env: {}, serverDefault: undefined })).to.deep.equal( - off('off'), - ); + it('is on with --encrypt, which is now a no-op', () => { + expect(resolveEncryption({ env: {}, flag: true })).to.deep.equal(ON); }); - it('is off when the server default is explicitly off', () => { - expect(resolveEncryption({ env: {}, serverDefault: false })).to.deep.equal( - off('off'), - ); + it('is on with DCD_ENCRYPT=1', () => { + expect(resolveEncryption({ env: { DCD_ENCRYPT: '1' } })).to.deep.equal(ON); }); - it('follows the server default when neither the flag nor the env decides', () => { - expect(resolveEncryption({ env: {}, serverDefault: true })).to.deep.equal( - on('server'), - ); - }); - - describe('an explicit flag', () => { - it('--encrypt turns it on with nothing else set', () => { - expect(resolveEncryption({ env: {}, flag: true })).to.deep.equal(on('flag')); - }); - - it('--no-encrypt beats DCD_ENCRYPT=1', () => { - expect( - resolveEncryption({ env: { DCD_ENCRYPT: '1' }, flag: false }), - ).to.deep.equal(off('flag')); - }); - - it('--no-encrypt beats the server default', () => { - expect( - resolveEncryption({ env: {}, flag: false, serverDefault: true }), - ).to.deep.equal(off('flag')); - }); - - it('--encrypt beats DCD_ENCRYPT=0', () => { - expect( - resolveEncryption({ env: { DCD_ENCRYPT: '0' }, flag: true }), - ).to.deep.equal(on('flag')); + it('ignores --no-encrypt, and says so', () => { + expect(resolveEncryption({ env: {}, flag: false })).to.deep.equal({ + ...ON, + notice: 'Ignoring --no-encrypt: uploads are always encrypted.', }); }); - describe('the env var', () => { - for (const value of ['1', 'true', 'TRUE', 'True', ' 1 ']) { - it(`reads DCD_ENCRYPT=${JSON.stringify(value)} as on`, () => { - expect(resolveEncryption({ env: { DCD_ENCRYPT: value } })).to.deep.equal( - on('env'), - ); - }); - } - - for (const value of ['0', 'false', 'FALSE', 'False']) { - it(`reads DCD_ENCRYPT=${JSON.stringify(value)} as off, beating the server default`, () => { - expect( - resolveEncryption({ env: { DCD_ENCRYPT: value }, serverDefault: true }), - ).to.deep.equal(off('env')); - }); - } - - for (const value of ['', 'yes', 'on', '2']) { - it(`ignores DCD_ENCRYPT=${JSON.stringify(value)}, leaving the server default to decide`, () => { - expect( - resolveEncryption({ env: { DCD_ENCRYPT: value }, serverDefault: true }), - ).to.deep.equal(on('server')); - expect(resolveEncryption({ env: { DCD_ENCRYPT: value } })).to.deep.equal( - off('off'), - ); + for (const value of ['0', 'false', 'FALSE', 'False', ' 0 ']) { + it(`ignores DCD_ENCRYPT=${JSON.stringify(value)}, and says so`, () => { + expect(resolveEncryption({ env: { DCD_ENCRYPT: value } })).to.deep.equal({ + ...ON, + notice: 'Ignoring DCD_ENCRYPT: uploads are always encrypted.', }); - } - - it('honours the legacy DCD_ENCRYPT_BINARIES alias both ways', () => { - expect( - resolveEncryption({ env: { DCD_ENCRYPT_BINARIES: '1' } }), - ).to.deep.equal(on('env')); - expect( - resolveEncryption({ - env: { DCD_ENCRYPT_BINARIES: 'false' }, - serverDefault: true, - }), - ).to.deep.equal(off('env')); }); + } - it('lets DCD_ENCRYPT win when both are set', () => { - expect( - resolveEncryption({ - env: { DCD_ENCRYPT: '0', DCD_ENCRYPT_BINARIES: '1' }, - }), - ).to.deep.equal(off('env')); - expect( - resolveEncryption({ - env: { DCD_ENCRYPT: 'true', DCD_ENCRYPT_BINARIES: '0' }, - }), - ).to.deep.equal(on('env')); + it('ignores the legacy DCD_ENCRYPT_BINARIES=0, and says so', () => { + expect(resolveEncryption({ env: { DCD_ENCRYPT_BINARIES: '0' } })).to.deep.equal({ + ...ON, + notice: 'Ignoring DCD_ENCRYPT_BINARIES: uploads are always encrypted.', }); + }); - it('falls back to the alias when DCD_ENCRYPT is set to something unreadable', () => { - expect( - resolveEncryption({ - env: { DCD_ENCRYPT: 'yes', DCD_ENCRYPT_BINARIES: '1' }, - }), - ).to.deep.equal(on('env')); + it('names every ignored opt-out in one line', () => { + expect( + resolveEncryption({ + env: { DCD_ENCRYPT: '0', DCD_ENCRYPT_BINARIES: 'false' }, + flag: false, + }), + ).to.deep.equal({ + ...ON, + notice: + 'Ignoring --no-encrypt and DCD_ENCRYPT and DCD_ENCRYPT_BINARIES: uploads are always encrypted.', }); }); + for (const value of ['', 'yes', 'on', '2', '1', 'true']) { + it(`gives no notice for DCD_ENCRYPT=${JSON.stringify(value)}`, () => { + expect(resolveEncryption({ env: { DCD_ENCRYPT: value } })).to.deep.equal(ON); + }); + } + it('reads process.env when no env is passed', () => { const original = process.env.DCD_ENCRYPT; try { - process.env.DCD_ENCRYPT = '1'; - expect(resolveEncryption({})).to.deep.equal(on('env')); + process.env.DCD_ENCRYPT = '0'; + expect(resolveEncryption().notice).to.equal( + 'Ignoring DCD_ENCRYPT: uploads are always encrypted.', + ); } finally { if (original === undefined) delete process.env.DCD_ENCRYPT; else process.env.DCD_ENCRYPT = original; } }); - - describe('full matrix', () => { - const flags = [undefined, true, false] as const; - const envs: Array<[string, Record, boolean | undefined]> = [ - ['unset', {}, undefined], - ['DCD_ENCRYPT=1', { DCD_ENCRYPT: '1' }, true], - ['DCD_ENCRYPT=0', { DCD_ENCRYPT: '0' }, false], - ]; - const serverDefaults = [undefined, true, false] as const; - - for (const flag of flags) { - for (const [envLabel, env, envValue] of envs) { - for (const serverDefault of serverDefaults) { - const expected: EncryptionDecision = - flag !== undefined - ? { enabled: flag, source: 'flag' } - : envValue !== undefined - ? { enabled: envValue, source: 'env' } - : serverDefault === true - ? on('server') - : off('off'); - - it(`flag=${String(flag)}, ${envLabel}, server=${String(serverDefault)} → ${expected.enabled ? 'on' : 'off'} (${expected.source})`, () => { - expect(resolveEncryption({ env, flag, serverDefault })).to.deep.equal( - expected, - ); - }); - } - } - } - }); }); diff --git a/test/unit/kek-from-api.test.ts b/test/unit/kek-from-api.test.ts index a57a5b1..720863f 100644 --- a/test/unit/kek-from-api.test.ts +++ b/test/unit/kek-from-api.test.ts @@ -128,11 +128,9 @@ describe('KEK public key from /cli-config', () => { it('passes a decision through when the API publishes a key', async () => { stubFetch(() => json({ binary_kek_public_key: { version: 1, key: rawPublicKey() } })); - for (const source of ['flag', 'env', 'server'] as const) { - expect( - await checkKekForEncryption({ enabled: true, source }, 'https://api.dev.devicecloud.dev'), - ).to.deep.equal({ enabled: true, source }); - } + expect( + await checkKekForEncryption({ enabled: true, source: 'always' }, 'https://api.dev.devicecloud.dev'), + ).to.deep.equal({ enabled: true, source: 'always' }); }); it('passes a decision to stay off through without asking the API', async () => { @@ -144,50 +142,36 @@ describe('KEK public key from /cli-config', () => { expect(requested).to.deep.equal([]); }); - for (const source of ['env', 'server'] as const) { - it(`skips ${source} encryption when the API publishes no key, with a notice`, async () => { - stubFetch(() => json({ supabase_publishable_key: 'sb_publishable_x' })); - const decision = await checkKekForEncryption({ enabled: true, source }, NO_KEK); - expect(decision).to.include({ enabled: false, source }); - expect(decision.notice).to.contain(NO_KEK); - expect(decision.notice).to.contain('DCD_BINARY_KEK_PUBLIC'); - expect(decision.notice).to.not.contain('\n'); - }); - } - - it('refuses an explicit --encrypt when the API publishes no key', async () => { + it('skips encryption when the API publishes no key, with a notice', async () => { stubFetch(() => json({ supabase_publishable_key: 'sb_publishable_x' })); - let message = ''; - try { - await checkKekForEncryption({ enabled: true, source: 'flag' }, NO_KEK); - } catch (error) { - message = (error as Error).message; - } - expect(message).to.match(/--encrypt needs the KEK public key.*DCD_BINARY_KEK_PUBLIC/); + const decision = await checkKekForEncryption( + { enabled: true, notice: 'Ignoring DCD_ENCRYPT: uploads are always encrypted.', source: 'always' }, + NO_KEK, + ); + expect(decision).to.include({ enabled: false, source: 'no_kek' }); + expect(decision.notice).to.contain(NO_KEK); + expect(decision.notice).to.contain('DCD_BINARY_KEK_PUBLIC'); + expect(decision.notice).to.not.contain('\n'); }); it('fails closed rather than uploading plaintext when the API cannot be asked', async () => { stubFetch(() => json({}, 503)); - for (const source of ['flag', 'env', 'server'] as const) { - let failed = false; - try { - await checkKekForEncryption({ enabled: true, source }, NO_KEK); - } catch { - failed = true; - } - expect(failed, source).to.equal(true); + let failed = false; + try { + await checkKekForEncryption({ enabled: true, source: 'always' }, NO_KEK); + } catch { + failed = true; } + expect(failed).to.equal(true); }); - it('lets DCD_BINARY_KEK_PUBLIC unlock any API, whatever the source', async () => { + it('lets DCD_BINARY_KEK_PUBLIC unlock any API', async () => { process.env.DCD_BINARY_KEK_PUBLIC = `7:${rawPublicKey()}`; stubFetch(() => json({}, 500)); - for (const source of ['flag', 'env', 'server'] as const) { - expect(await checkKekForEncryption({ enabled: true, source }, NO_KEK)).to.deep.equal({ - enabled: true, - source, - }); - } + expect(await checkKekForEncryption({ enabled: true, source: 'always' }, NO_KEK)).to.deep.equal({ + enabled: true, + source: 'always', + }); expect(requested).to.deep.equal([]); }); }); diff --git a/test/unit/mcp-bridge.test.ts b/test/unit/mcp-bridge.test.ts index 4de9808..e9e9be5 100644 --- a/test/unit/mcp-bridge.test.ts +++ b/test/unit/mcp-bridge.test.ts @@ -430,6 +430,8 @@ describe('dcd mcp bridge', () => { return new Response(JSON.stringify({ id: 'b-1', upload: { path: 'p', signedUrl: 'https://sb.example/put' }, mirror: null })); } + // No KEK published, so the bundle goes up in plaintext. + if (url.endsWith('/cli-config')) return new Response(JSON.stringify({ supabase_publishable_key: 'sb_publishable_test' })); if (url === 'https://sb.example/put') return new Response(null, { status: 200 }); if (url.endsWith('/device/bundles')) { return new Response(JSON.stringify({ bundle: { id: 'b-1', sha: 's', bytes: 1, encrypted: false, created_at: 'now' } })); diff --git a/test/unit/telemetry-encryption.test.ts b/test/unit/telemetry-encryption.test.ts index 430cd73..f1477a5 100644 --- a/test/unit/telemetry-encryption.test.ts +++ b/test/unit/telemetry-encryption.test.ts @@ -43,7 +43,7 @@ describe('encryption telemetry (#1138)', () => { events.filter((e) => e.message === message).pop(); it('puts what the command encrypted on `command completed`, merged as it happens', async () => { - telemetry.recordEncryption({ binary: false, env: false, flow: false, source: 'server' }); + telemetry.recordEncryption({ binary: false, env: false, flow: false, source: 'always' }); telemetry.recordEncryption({ binary: true }); telemetry.recordEncryption({ env: true, flow: true }); telemetry.recordCommandSuccess(); @@ -53,12 +53,12 @@ describe('encryption telemetry (#1138)', () => { binary: true, env: true, flow: true, - source: 'server', + source: 'always', }); }); it('puts it on `command failed` too', async () => { - telemetry.recordEncryption({ binary: false, env: false, flow: false, source: 'flag' }); + telemetry.recordEncryption({ binary: false, env: false, flow: false, source: 'no_kek' }); telemetry.recordCommandFailure({ error: new Error('boom'), exitCode: 1 }); await telemetry.flush(); @@ -66,7 +66,7 @@ describe('encryption telemetry (#1138)', () => { binary: false, env: false, flow: false, - source: 'flag', + source: 'no_kek', }); }); @@ -75,7 +75,7 @@ describe('encryption telemetry (#1138)', () => { binary: true, env: false, flow: true, - source: 'env', + source: 'always', }; it('sends the fields a tool adds on its `mcp tool completed` event', async () => { diff --git a/test/unit/upload-encrypt.test.ts b/test/unit/upload-encrypt.test.ts index 4443d8a..21ceb81 100644 --- a/test/unit/upload-encrypt.test.ts +++ b/test/unit/upload-encrypt.test.ts @@ -10,9 +10,9 @@ import { resetCliConfigCache } from '../../src/config/cli-config.js'; import { clearCompatibilityCache } from '../../src/utils/compatibility.js'; /** - * `dcd upload` decides encryption in the same order as `dcd cloud`: the flag - * (--encrypt / --no-encrypt), then DCD_ENCRYPT, then the server default from - * the compatibility data (dcd#1138), then off. + * `dcd upload` always encrypts, like `dcd cloud` (dcd#1138). --no-encrypt and + * DCD_ENCRYPT=0 are ignored with a warning. The only way to get a plaintext + * upload is an API that publishes no KEK. * * Observed through the dedup lookup, which is the first upload request either * way: an encrypted upload deduplicates on the plaintext hash and says so @@ -22,8 +22,6 @@ const API = 'http://localhost:9999'; /** An API that publishes no KEK on /cli-config (see the fetch stub). */ const UNKNOWN_API = 'https://api.example.com'; const APK = path.join(process.cwd(), 'test/fixtures/wikipedia.apk'); -/** Printed when the server default, not a flag or env var, turned encryption on. */ -const DEFAULT_ON_NOTICE = 'Encrypting uploads; opt out with --no-encrypt'; describe('dcd upload encryption', () => { const ORIGINAL_ENV = { ...process.env }; @@ -31,9 +29,6 @@ describe('dcd upload encryption', () => { const realExit = process.exit; const realFlushSync = telemetry.flushSync; let lookups: Array>; - /** The `encryption` field the mocked compatibility endpoint returns, if any. */ - let compatEncryption: { defaultOn: boolean } | undefined; - let compatFails: boolean; let tempDir: string; beforeEach(() => { @@ -44,16 +39,15 @@ describe('dcd upload encryption', () => { delete process.env.DCD_ENCRYPT_BINARIES; clearCompatibilityCache(); resetCliConfigCache(); - compatEncryption = undefined; - compatFails = false; // A throwaway KEK so encryption can run against an API that publishes none. const { publicKey } = generateKeyPairSync('x25519'); const raw = publicKey.export({ format: 'der', type: 'spki' }).subarray(12); process.env.DCD_BINARY_KEK_PUBLIC = `1:${raw.toString('base64')}`; - // Serve the compatibility data, and answer the dedup lookup with a hit of - // the matching kind, so the command finishes without uploading anything. + // Answer the dedup lookup with a hit of the matching kind, so the command + // finishes without uploading anything. `dcd upload` no longer reads the + // compatibility data, so any request for it fails the test. lookups = []; globalThis.fetch = (async (input: unknown, init?: RequestInit) => { const json = (body: unknown, status = 200) => @@ -69,17 +63,7 @@ describe('dcd upload encryption', () => { } if (String(input).includes('/results/compatibility/data')) { - if (compatFails) return json({ message: 'boom' }, 500); - return json({ - data: { - android: {}, - androidPlay: {}, - ios: {}, - maestro: { defaultVersion: '2.0.0', latestVersion: '2.0.0', supportedVersions: ['2.0.0'] }, - ...(compatEncryption ? { encryption: compatEncryption } : {}), - }, - statusCode: 200, - }); + throw new Error('dcd upload should not fetch the compatibility data'); } const body = JSON.parse(String(init?.body)) as Record; @@ -134,65 +118,33 @@ describe('dcd upload encryption', () => { return (JSON.parse(out) as { appBinaryId: string }).appBinaryId; } - it('encrypts when DCD_ENCRYPT=1 is exported and --encrypt is not passed', async () => { - process.env.DCD_ENCRYPT = '1'; - + it('encrypts with nothing set', async () => { expect(await upload()).to.equal('encrypted-binary'); expect(lookups[0]).to.include({ encrypted: true }); expect(lookups[0]).to.have.property('shaPlain'); }); - it('still encrypts with --encrypt alone', async () => { + it('encrypts with --encrypt, which is now a no-op', async () => { expect(await upload({ encrypt: true })).to.equal('encrypted-binary'); }); - it('uploads in plaintext when neither is set and the API sends no default', async () => { - expect(await upload()).to.equal('plain-binary'); - expect(lookups[0]).to.have.property('sha'); - expect(lookups[0]).to.not.have.property('encrypted'); - }); - - it('uploads in plaintext when the server default is off', async () => { - compatEncryption = { defaultOn: false }; - expect(await upload()).to.equal('plain-binary'); - }); - - it('encrypts when the server default is on', async () => { - compatEncryption = { defaultOn: true }; - expect(await upload()).to.equal('encrypted-binary'); - }); - - it('lets --no-encrypt beat the server default', async () => { - compatEncryption = { defaultOn: true }; - expect(await upload({ encrypt: false })).to.equal('plain-binary'); + it('ignores --no-encrypt, with a warning, and still encrypts', async () => { + expect(await upload({ encrypt: false })).to.equal('encrypted-binary'); + expect(await run({ encrypt: false })).to.contain( + 'Ignoring --no-encrypt: uploads are always encrypted.', + ); }); - it('lets --no-encrypt beat DCD_ENCRYPT=1', async () => { - process.env.DCD_ENCRYPT = '1'; - expect(await upload({ encrypt: false })).to.equal('plain-binary'); - }); - - it('lets DCD_ENCRYPT=0 beat the server default', async () => { - process.env.DCD_ENCRYPT = '0'; - compatEncryption = { defaultOn: true }; - expect(await upload()).to.equal('plain-binary'); - }); - - it('treats a failed compatibility fetch as the default being off', async () => { - compatFails = true; - expect(await upload()).to.equal('plain-binary'); - }); - - it('says it is encrypting, and how to opt out, when the server default encrypts', async () => { - compatEncryption = { defaultOn: true }; - expect(await run({})).to.contain(DEFAULT_ON_NOTICE); - }); + for (const name of ['DCD_ENCRYPT', 'DCD_ENCRYPT_BINARIES']) { + it(`ignores ${name}=0, with a warning, and still encrypts`, async () => { + process.env[name] = '0'; + expect(await upload()).to.equal('encrypted-binary'); + expect(await run({})).to.contain(`Ignoring ${name}: uploads are always encrypted.`); + }); + } - it('prints no notice when the flag or env var turned encryption on', async () => { - compatEncryption = { defaultOn: true }; - expect(await run({ encrypt: true })).to.not.contain(DEFAULT_ON_NOTICE); - process.env.DCD_ENCRYPT = '1'; - expect(await run({})).to.not.contain(DEFAULT_ON_NOTICE); + it('prints no notice in the normal case', async () => { + expect(await run({})).to.not.contain('Ignoring'); }); describe('against an API that publishes no KEK', () => { @@ -200,46 +152,23 @@ describe('dcd upload encryption', () => { delete process.env.DCD_BINARY_KEK_PUBLIC; }); - it('skips the server default, with a notice, and uploads in plaintext', async () => { - compatEncryption = { defaultOn: true }; + it('uploads in plaintext, with a notice', async () => { expect(await upload({ 'api-url': UNKNOWN_API })).to.equal('plain-binary'); + expect(lookups[0]).to.have.property('sha'); + expect(lookups[0]).to.not.have.property('encrypted'); const out = await run({ 'api-url': UNKNOWN_API }); expect(out).to.contain(`Not encrypting uploads: ${UNKNOWN_API}`); - expect(out).to.not.contain(DEFAULT_ON_NOTICE); }); - it('skips DCD_ENCRYPT=1 the same way', async () => { - process.env.DCD_ENCRYPT = '1'; - expect(await upload({ 'api-url': UNKNOWN_API })).to.equal('plain-binary'); - }); - - it('refuses --encrypt before uploading anything', async () => { - let stderr = ''; - const original = process.stderr.write.bind(process.stderr); - process.stderr.write = ((chunk: string) => { - stderr += chunk; - return true; - }) as typeof process.stderr.write; - try { - await run({ 'api-url': UNKNOWN_API, encrypt: true }); - expect.fail('dcd upload --encrypt should have failed'); - } catch (error) { - expect((error as Error).message).to.equal('process.exit(1)'); - } finally { - process.stderr.write = original; - } - - expect(stderr).to.contain('--encrypt needs the KEK public key'); - expect(lookups).to.have.lengthOf(0); + it('treats --encrypt the same way, since it is a no-op', async () => { + expect(await upload({ 'api-url': UNKNOWN_API, encrypt: true })).to.equal('plain-binary'); }); - it('encrypts with --encrypt once DCD_BINARY_KEK_PUBLIC names a key', async () => { + it('encrypts once DCD_BINARY_KEK_PUBLIC names a key', async () => { const { publicKey } = generateKeyPairSync('x25519'); const raw = publicKey.export({ format: 'der', type: 'spki' }).subarray(12); process.env.DCD_BINARY_KEK_PUBLIC = `1:${raw.toString('base64')}`; - expect(await upload({ 'api-url': UNKNOWN_API, encrypt: true })).to.equal( - 'encrypted-binary', - ); + expect(await upload({ 'api-url': UNKNOWN_API })).to.equal('encrypted-binary'); }); }); });