Skip to content
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

Improved and reliable secret masking #569

Merged
merged 3 commits into from
Jun 29, 2023
Merged
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
17 changes: 12 additions & 5 deletions src/spec-utils/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,27 +299,34 @@ export function toWarningText(str: string) {

export function replaceAllLog(origin: LogHandler, values: string[], replacement: string): LogHandler {
values = values
.map(v => v.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
.filter(v => v.length);
.filter(v => v.length)
.sort((a, b) => b.length - a.length);
if (!values.length) {
return origin;
}
const r = new RegExp(values.join('|'), 'g');
return {
event: e => {
if ('text' in e) {
origin.event({
...e,
text: e.text.replace(r, replacement),
text: replaceValues(e.text, replacement, values),
});
} else if (e.type === 'progress' && e.stepDetail) {
origin.event({
...e,
stepDetail: e.stepDetail.replace(r, replacement),
stepDetail: replaceValues(e.stepDetail, replacement, values),
});
} else {
origin.event(e);
}
}
};
}

function replaceValues(str: string, replacement: string, values: string[]) {
values.forEach(x => {
str = str.replaceAll(x, replacement);
});

return str;
}
9 changes: 2 additions & 7 deletions src/test/cli.up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ describe('Dev Containers CLI', function () {
const testFolder = `${__dirname}/configs`;
let containerId: string | null = null;
await shellExec(`rm -f ${testFolder}/*.testMarker`, undefined, undefined, true);
const secrets = {
'SECRET1': 'SecretValue1',
'MASK_IT': 'container',
};
await shellExec(`printf '${JSON.stringify(secrets)}' > ${testFolder}/test-secrets-temp.json`, undefined, undefined, true);

const res = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/image-with-git-feature --dotfiles-repository https://github.com/codspace/test-dotfiles --secrets-file ${testFolder}/test-secrets-temp.json --log-level trace --log-format json`);
const res = await shellExec(`${cli} up --workspace-folder ${__dirname}/configs/image-with-git-feature --dotfiles-repository https://github.com/codspace/test-dotfiles --secrets-file ${testFolder}/test-secrets.json --log-level trace --log-format json`);
const response = JSON.parse(res.stdout);
assert.equal(response.outcome, 'success');
containerId = response.containerId;
Expand All @@ -71,7 +66,7 @@ describe('Dev Containers CLI', function () {
assert.match(stdout, /TEST_REMOTE_ENV=Value 1/);

// assert secret masking
// We log the message `Starting container` from CLI. Since the word `container` is specified as a secret here, that should get masked
// We log the message `Starting container` from CLI. Since the word `container` is specified as a secret (in test-secrets.json), that should get masked
const logs = res.stderr;
assert.match(logs, /Starting \*\*\*\*\*\*\*\*/);
assert.doesNotMatch(logs, /Starting container/);
Expand Down
Loading