Skip to content

Commit

Permalink
Handle missing entry in /etc/passwd gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Aug 25, 2023
1 parent 199a4f1 commit c960d97
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/spec-common/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,5 +588,5 @@ export function getEntPasswdShellCommand(userNameOrId: string) {
const escapedForRexExp = escapeRegExCharacters(userNameOrId)
.replaceAll('\'', '\\\'');
// Leading space makes sure we don't concatenate to arithmetic expansion (https://tldp.org/LDP/abs/html/dblparens.html).
return ` (command -v getent >/dev/null 2>&1 && getent passwd '${escapedForShell}' || grep -E '^${escapedForRexExp}|^[^:]*:[^:]*:${escapedForRexExp}:' /etc/passwd)`;
return ` (command -v getent >/dev/null 2>&1 && getent passwd '${escapedForShell}' || grep -E '^${escapedForRexExp}|^[^:]*:[^:]*:${escapedForRexExp}:' /etc/passwd || true)`;
}
3 changes: 3 additions & 0 deletions src/spec-common/injectHeadless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ async function getUserShell(containerEnv: NodeJS.ProcessEnv, passwdUser: PasswdU

export async function getUserFromPasswdDB(shellServer: ShellServer, userNameOrId: string) {
const { stdout } = await shellServer.exec(getEntPasswdShellCommand(userNameOrId), { logOutput: false });
if (!stdout.trim()) {
return undefined;
}
return parseUserInPasswdDB(stdout);
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/getEntPasswd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ describe('getEntPasswdShellCommand', function () {
assert.strictEqual(userById.name, userName);
assert.strictEqual(userById.home, '/home/foo');

const nonexistentUser = await getUserFromPasswdDB(shellServer, '123456');
assert.strictEqual(undefined, nonexistentUser);

await shellExec(`docker rm -f ${containerId}`);
});
});
Expand Down

0 comments on commit c960d97

Please sign in to comment.