From c960d97bd79d717f39c00d794cc3b86395d41266 Mon Sep 17 00:00:00 2001 From: Christof Marti Date: Thu, 24 Aug 2023 10:43:52 +0200 Subject: [PATCH] Handle missing entry in /etc/passwd gracefully --- src/spec-common/commonUtils.ts | 2 +- src/spec-common/injectHeadless.ts | 3 +++ src/test/getEntPasswd.test.ts | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/spec-common/commonUtils.ts b/src/spec-common/commonUtils.ts index 940f2d72b..5d5f1c0c7 100644 --- a/src/spec-common/commonUtils.ts +++ b/src/spec-common/commonUtils.ts @@ -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)`; } diff --git a/src/spec-common/injectHeadless.ts b/src/spec-common/injectHeadless.ts index 2f36662f9..ba2606356 100644 --- a/src/spec-common/injectHeadless.ts +++ b/src/spec-common/injectHeadless.ts @@ -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); } diff --git a/src/test/getEntPasswd.test.ts b/src/test/getEntPasswd.test.ts index 1dfae5d0a..d3c5945aa 100644 --- a/src/test/getEntPasswd.test.ts +++ b/src/test/getEntPasswd.test.ts @@ -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}`); }); });