Skip to content

Commit 79add1a

Browse files
Copilotabdurriq
andcommitted
Fix code review issues: validate _REMOTE_USER, fix common-utils none handling, remove dead code
Co-authored-by: abdurriq <[email protected]>
1 parent c5712f9 commit 79add1a

4 files changed

Lines changed: 41 additions & 15 deletions

File tree

src/_common/common-setup.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ determine_user_from_input() {
3737

3838
# First, check if _REMOTE_USER is set and is not root
3939
if [ -n "${_REMOTE_USER:-}" ] && [ "${_REMOTE_USER}" != "root" ]; then
40-
resolved_username="${_REMOTE_USER}"
41-
else
40+
# Verify the user exists before using it
41+
if id -u "${_REMOTE_USER}" > /dev/null 2>&1; then
42+
resolved_username="${_REMOTE_USER}"
43+
else
44+
# _REMOTE_USER doesn't exist, fall through to normal detection
45+
resolved_username=""
46+
fi
47+
fi
48+
49+
# If we didn't resolve via _REMOTE_USER, try to find a non-root user
50+
if [ -z "${resolved_username}" ]; then
4251
# Try to find a non-root user from a list of common usernames
4352
# The list includes: devcontainer, vscode, node, codespace, and the user with UID 1000
4453
local possible_users=("devcontainer" "vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd 2>/dev/null || echo '')")

src/common-utils/main.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,16 @@ esac
395395
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
396396
source "${SCRIPT_DIR}/../_common/common-setup.sh"
397397

398-
# If in automatic mode, determine if a user already exists, if not use vscode (which will be created)
399-
USERNAME=$(determine_user_from_input "${USERNAME}" "vscode")
400-
401-
# Handle the special "none" case for common-utils
402-
if [ "${USERNAME}" = "none" ]; then
398+
# Handle the special "none" case for common-utils before user determination
399+
# The "none" case sets USER_UID and USER_GID to 0
400+
ORIGINAL_USERNAME="${USERNAME}"
401+
if [ "${ORIGINAL_USERNAME}" = "none" ]; then
402+
USERNAME="root"
403403
USER_UID=0
404404
USER_GID=0
405+
else
406+
# If in automatic mode, determine if a user already exists, if not use vscode (which will be created)
407+
USERNAME=$(determine_user_from_input "${USERNAME}" "vscode")
405408
fi
406409
# Create or update a non-root user to match UID/GID.
407410
group_name="${USERNAME}"

src/java/wrapper.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ if [ "${is_jdk_8}" = "true" ]; then
2323
./install.sh "${ADDITIONAL_JAVA_VERSION}" "${SDKMAN_DIR}" "${USERNAME}" "${UPDATE_RC}"
2424
jdk_11_folder="$(ls --format=single-column ${SDKMAN_DIR}/candidates/java | grep -oE -m 1 '11\..+')"
2525
ln -s "${SDKMAN_DIR}/candidates/java/${jdk_11_folder}" /extension-java-home
26-
27-
# Source common helper functions to determine the appropriate non-root user
28-
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../_common/common-setup.sh"
29-
username=$(determine_user_from_input "${USERNAME}" "root")
3026
else
3127
ln -s ${SDKMAN_DIR}/candidates/java/current /extension-java-home
3228
fi

test/_common/test-common-setup.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,26 @@ test_remote_user_root() {
113113

114114
# Test 10: Finding vscode user if it exists
115115
test_find_vscode_user() {
116-
# Check if vscode user exists
117-
if id -u vscode > /dev/null 2>&1; then
116+
# Check if vscode user exists and no higher priority users exist
117+
if id -u vscode > /dev/null 2>&1 && \
118+
! id -u devcontainer > /dev/null 2>&1; then
119+
# Unset _REMOTE_USER to ensure it doesn't interfere
120+
unset _REMOTE_USER
118121
local result=$(determine_user_from_input "automatic")
119122
# Should find vscode (it's second in priority after devcontainer)
120123
run_test "Finds vscode user in automatic mode" "vscode" "${result}"
121124
else
122-
# Skip this test if vscode user doesn't exist
123-
run_test "Finds vscode user in automatic mode (SKIPPED - user doesn't exist)" "SKIP" "SKIP"
125+
# Skip this test if vscode user doesn't exist or higher priority user exists
126+
run_test "Finds vscode user in automatic mode (SKIPPED - conditions not met)" "SKIP" "SKIP"
124127
fi
125128
}
126129

127130
# Test 11: Finding devcontainer user (highest priority)
128131
test_find_devcontainer_user() {
129132
# Check if devcontainer user exists
130133
if id -u devcontainer > /dev/null 2>&1; then
134+
# Unset _REMOTE_USER to ensure it doesn't interfere
135+
unset _REMOTE_USER
131136
local result=$(determine_user_from_input "automatic")
132137
# Should find devcontainer (highest priority)
133138
run_test "Finds devcontainer user (highest priority)" "devcontainer" "${result}"
@@ -165,6 +170,18 @@ test_empty_input() {
165170
run_test "Empty input treated as automatic and finds UID 1000 or uses fallback" "${expected}" "${result}"
166171
}
167172

173+
# Test 14: _REMOTE_USER set to non-existent user should use fallback
174+
test_remote_user_nonexistent() {
175+
export _REMOTE_USER="nonexistentuser99999"
176+
local result=$(determine_user_from_input "automatic" "mydefault")
177+
unset _REMOTE_USER
178+
179+
# Should fall through to normal detection - find UID 1000 or use fallback
180+
local uid_1000_user=$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd 2>/dev/null || echo '')
181+
local expected="${uid_1000_user:-mydefault}"
182+
run_test "_REMOTE_USER set to non-existent user falls back to detection" "${expected}" "${result}"
183+
}
184+
168185
# Run all tests
169186
echo "Running tests for common-setup.sh..."
170187
echo "======================================"
@@ -183,6 +200,7 @@ test_find_vscode_user
183200
test_find_devcontainer_user
184201
test_find_uid_1000
185202
test_empty_input
203+
test_remote_user_nonexistent
186204

187205
# Print summary
188206
echo ""

0 commit comments

Comments
 (0)