Repository navigation
fix(cli): keep the .env file private to its owner #1236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Priyanshubhartistm
wants to merge
2
commits into
cameri:main
Choose a base branch
from
Priyanshubhartistm:fix/cli-env-file-private-permissions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| 'nostream': patch | ||
| --- | ||
|
|
||
| fix(cli): keep the .env file private to its owner | ||
|
|
||
| `nostream setup` and `nostream config env set` wrote `.env` with the default file mode, which under the usual umask makes it readable by every local account that can reach the project directory. The file holds `SECRET`, which signs the admin session tokens, and other credentials. The CLI now creates `.env` readable by its owner only (mode 0600), including when it starts from `.env.example`, and restricts an existing `.env` to the owner whenever it writes to it. An existing `.env` that setup does not need to change is left as it is. Where the permissions cannot be changed, for example on a file system without Unix permissions, the write still succeeds. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { expect } from 'chai' | ||
| import fs from 'fs' | ||
| import os from 'os' | ||
| import path from 'path' | ||
|
|
||
| import { upsertEnvValue, writeEnvFile } from '../../../src/cli/utils/env-config' | ||
|
|
||
| describe('the permissions of the .env file written by the CLI', function () { | ||
| // File modes mean nothing on Windows. | ||
| before(function () { | ||
| if (process.platform === 'win32') { | ||
| this.skip() | ||
| } | ||
| }) | ||
|
|
||
| const originalCwd = process.cwd() | ||
| let tempDir: string | ||
| let originalUmask: number | ||
|
|
||
| const envPath = () => path.join(tempDir, '.env') | ||
| const modeOf = (file: string) => fs.statSync(file).mode & 0o777 | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-env-')) | ||
| process.chdir(tempDir) | ||
| // The usual umask, under which a file created with the default mode is readable by every local account. | ||
| originalUmask = process.umask(0o022) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| process.umask(originalUmask) | ||
| process.chdir(originalCwd) | ||
| fs.rmSync(tempDir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| describe('writeEnvFile', () => { | ||
| it('creates a new file readable by its owner only', () => { | ||
| writeEnvFile(envPath(), 'SECRET=abc\n') | ||
|
|
||
| expect(modeOf(envPath())).to.equal(0o600) | ||
| expect(fs.readFileSync(envPath(), 'utf-8')).to.equal('SECRET=abc\n') | ||
| }) | ||
|
|
||
| it('restricts a file that exists already and replaces its content', () => { | ||
| fs.writeFileSync(envPath(), 'OLD=1\n') | ||
| fs.chmodSync(envPath(), 0o644) | ||
|
|
||
| writeEnvFile(envPath(), 'SECRET=abc\n') | ||
|
|
||
| expect(modeOf(envPath())).to.equal(0o600) | ||
| expect(fs.readFileSync(envPath(), 'utf-8')).to.equal('SECRET=abc\n') | ||
| }) | ||
|
|
||
| it('restricts an existing file before the new content is written into it', () => { | ||
| fs.writeFileSync(envPath(), 'OLD=1\n') | ||
| fs.chmodSync(envPath(), 0o644) | ||
|
|
||
| // What another account could read at the moment the new content arrives. | ||
| let modeWhenWritten: number | undefined | ||
| const write = fs.writeFileSync | ||
| ;(fs as any).writeFileSync = (...args: Parameters<typeof fs.writeFileSync>) => { | ||
| modeWhenWritten = modeOf(envPath()) | ||
| return write(...args) | ||
| } | ||
|
|
||
| try { | ||
| writeEnvFile(envPath(), 'SECRET=abc\n') | ||
| } finally { | ||
| ;(fs as any).writeFileSync = write | ||
| } | ||
|
|
||
| expect(modeWhenWritten).to.equal(0o600) | ||
| expect(fs.readFileSync(envPath(), 'utf-8')).to.equal('SECRET=abc\n') | ||
| }) | ||
|
|
||
| it('does not fail when the permissions cannot be changed', () => { | ||
| const chmod = fs.chmodSync | ||
| ;(fs as any).chmodSync = () => { | ||
| throw Object.assign(new Error('EPERM'), { code: 'EPERM' }) | ||
| } | ||
|
|
||
| try { | ||
| expect(() => writeEnvFile(envPath(), 'SECRET=abc\n')).not.to.throw() | ||
| } finally { | ||
| ;(fs as any).chmodSync = chmod | ||
| } | ||
|
|
||
| expect(fs.readFileSync(envPath(), 'utf-8')).to.equal('SECRET=abc\n') | ||
| }) | ||
| }) | ||
|
|
||
| describe('upsertEnvValue (nostream config env set)', () => { | ||
| it('creates .env readable by its owner only when it did not exist', () => { | ||
| upsertEnvValue('SECRET', 'abc') | ||
|
|
||
| expect(modeOf(envPath())).to.equal(0o600) | ||
| expect(fs.readFileSync(envPath(), 'utf-8')).to.equal('SECRET=abc\n') | ||
| }) | ||
|
|
||
| it('restricts an existing .env and keeps its other values', () => { | ||
| fs.writeFileSync(envPath(), 'FOO=bar\n') | ||
| fs.chmodSync(envPath(), 0o644) | ||
|
|
||
| upsertEnvValue('SECRET', 'abc') | ||
|
|
||
| expect(modeOf(envPath())).to.equal(0o600) | ||
| expect(fs.readFileSync(envPath(), 'utf-8')).to.equal('FOO=bar\n\nSECRET=abc\n') | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.