Skip to content

Commit 950b60a

Browse files
author
mujing
committed
ci: enhance su command blocking test and policy enforcement
1 parent bc5a43e commit 950b60a

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

.github/workflows/build-and-push.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,18 @@ jobs:
274274
timeout-minutes: 1
275275
run: |
276276
echo "Testing su command is blocked..."
277-
if docker exec test-container bash -c "sudo su - root" 2>&1 | grep -q "sudo: su: command not found\|Sorry, user coder is not allowed to execute"; then
277+
SU_OUTPUT=$(docker exec test-container bash -c "sudo -n /usr/bin/su - root -c 'whoami'" 2>&1 || true)
278+
echo "sudo su output: $SU_OUTPUT"
279+
280+
if echo "$SU_OUTPUT" | grep -q "^root$"; then
281+
echo "::error::su command escalated to root; policy is broken"
282+
exit 1
283+
fi
284+
285+
if echo "$SU_OUTPUT" | grep -Eq "not allowed to execute|command not found|a password is required|password is required|authentication is required"; then
278286
echo "✓ su command is properly blocked"
279287
else
280-
echo "::error::su command is not blocked as expected"
288+
echo "::error::Unexpected sudo/su behavior: $SU_OUTPUT"
281289
exit 1
282290
fi
283291

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ RUN mkdir -p /etc/apt/keyrings \
3232

3333
# Layer 2: User permissions - sudo with su blocked
3434
RUN apt-get update && apt-get install -y --no-install-recommends sudo \
35-
&& echo "coder ALL=(ALL) NOPASSWD: ALL, !/usr/bin/su, !/bin/su" > /etc/sudoers.d/coder-nopasswd \
36-
&& chmod 440 /etc/sudoers.d/coder-nopasswd \
37-
&& visudo -c -f /etc/sudoers.d/coder-nopasswd \
35+
&& find /etc/sudoers.d -maxdepth 1 -type f -name '*coder*' -delete \
36+
&& echo "coder ALL=(ALL) NOPASSWD: ALL, !/usr/bin/su, !/bin/su" > /etc/sudoers.d/zz-coder-policy \
37+
&& chmod 440 /etc/sudoers.d/zz-coder-policy \
38+
&& visudo -c -f /etc/sudoers.d/zz-coder-policy \
39+
&& sudo -l -U coder | grep -Eq '!(/usr/bin/su|/bin/su)' \
3840
&& rm -rf /var/lib/apt/lists/*
3941

4042
# Layer 3: Go (pinned stable) with China mirror and tools

0 commit comments

Comments
 (0)