Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/cli-env-mask-connection-uris.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'nostream': patch
---

fix(cli): mask DB_URI and REDIS_URI in `nostream config env` output

`nostream config env list` and `config env get` hide the value of secret keys unless `--show-secrets` is given, but they only recognised keys with SECRET, PASSWORD, API_KEY or PRIVATE_KEY in the name. `DB_URI` and `REDIS_URI` can carry a password (`postgresql://user:password@host/db`), so it was printed as it was. These two keys are now masked like the other secrets, and shown in full with `--show-secrets`.
3 changes: 2 additions & 1 deletion src/cli/utils/env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const BOOLEAN_KEYS = new Set(['READ_REPLICA_ENABLED'])

const RR_INTEGER_KEY_REGEX = /^RR\d+_DB_(PORT|MIN_POOL_SIZE|MAX_POOL_SIZE|ACQUIRE_CONNECTION_TIMEOUT)$/

const SECRET_KEY_REGEX = /(SECRET|PASSWORD|API_KEY|PRIVATE_KEY)/i
// A connection URI (DB_URI, REDIS_URI) can carry a password, so it is masked like the other secrets.
const SECRET_KEY_REGEX = /(SECRET|PASSWORD|API_KEY|PRIVATE_KEY|_URI$)/i

const parseEnvFile = (): { lines: string[]; parsed: ParsedEnvLine[] } => {
const envPath = getEnvFilePath()
Expand Down
64 changes: 64 additions & 0 deletions test/unit/cli/env-secrets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect } from 'chai'
import fs from 'fs'
import os from 'os'
import path from 'path'
import sinon from 'sinon'

import { runConfigEnvGet, runConfigEnvList } from '../../../src/cli/commands/config'
import { isSecretEnvKey } from '../../../src/cli/utils/env-config'

describe('secrets in the .env output of the CLI', () => {
const originalCwd = process.cwd()
const dbUri = 'postgresql://relay:hunter2-db-password@db.internal:5432/nostr'
const redisUri = 'redis://default:hunter2-redis-password@cache.internal:6379'

let tempDir: string
let stdout: string

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-env-'))
process.chdir(tempDir)
const lines = [`DB_URI=${dbUri}`, `REDIS_URI=${redisUri}`, 'DB_HOST=db.internal', 'DB_PASSWORD=hunter2-password']
fs.writeFileSync(path.join(tempDir, '.env'), `${lines.join('\n')}\n`)
stdout = ''
sinon.stub(process.stdout, 'write').callsFake(((chunk: string | Uint8Array) => {
stdout += String(chunk)
return true
}) as typeof process.stdout.write)
})

afterEach(() => {
sinon.restore()
process.chdir(originalCwd)
fs.rmSync(tempDir, { recursive: true, force: true })
})

it('treats a connection URI as a secret, like a password', () => {
expect(isSecretEnvKey('DB_URI')).to.equal(true)
expect(isSecretEnvKey('REDIS_URI')).to.equal(true)
expect(isSecretEnvKey('DB_PASSWORD')).to.equal(true)
expect(isSecretEnvKey('DB_HOST')).to.equal(false)
})

it('does not list the password inside a connection URI by default', async () => {
await runConfigEnvList()

expect(stdout).to.not.include('hunter2')
expect(stdout).to.include('DB_HOST=db.internal')
})

it('does not print the password inside a connection URI with get by default', async () => {
await runConfigEnvGet('DB_URI')
await runConfigEnvGet('REDIS_URI')

expect(stdout).to.not.include('hunter2')
})

it('shows the whole URI when the operator asks for secrets', async () => {
await runConfigEnvGet('DB_URI', { showSecrets: true })
await runConfigEnvList({ showSecrets: true })

expect(stdout).to.include(dbUri)
expect(stdout).to.include(redisUri)
})
})
Loading