| 1 |
FROM alpine:3.22 |
| 2 |
|
| 3 |
# add "nobody" to ALL groups (makes testing edge cases more interesting) |
| 4 |
RUN cut -d: -f1 /etc/group | xargs -rtn1 addgroup nobody |
| 5 |
|
| 6 |
RUN { \ |
| 7 |
echo '#!/bin/sh'; \ |
| 8 |
echo 'set -ex'; \ |
| 9 |
echo; \ |
| 10 |
echo 'spec="$1"; shift'; \ |
| 11 |
echo; \ |
| 12 |
echo 'expec="$1"; shift'; \ |
| 13 |
echo 'real="$(gosu "$spec" id -u):$(gosu "$spec" id -g):$(gosu "$spec" id -G)"'; \ |
| 14 |
echo '[ "$expec" = "$real" ]'; \ |
| 15 |
echo; \ |
| 16 |
echo 'expec="$1"; shift'; \ |
| 17 |
# have to "|| true" this one because of "id: unknown ID 1000" (rightfully) having a nonzero exit code |
| 18 |
echo 'real="$(gosu "$spec" id -un):$(gosu "$spec" id -gn):$(gosu "$spec" id -Gn)" || true'; \ |
| 19 |
echo '[ "$expec" = "$real" ]'; \ |
| 20 |
} > /usr/local/bin/gosu-t \ |
| 21 |
&& chmod +x /usr/local/bin/gosu-t |
| 22 |
|
| 23 |
COPY gosu /usr/local/bin/ |
| 24 |
|
| 25 |
# adjust users so we can make sure the tests are interesting |
| 26 |
RUN chgrp nobody /usr/local/bin/gosu \ |
| 27 |
&& chmod +s /usr/local/bin/gosu |
| 28 |
ENV GOSU_PLEASE_LET_ME_BE_COMPLETELY_INSECURE_I_GET_TO_KEEP_ALL_THE_PIECES="I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears in rain. Time to die." |
| 29 |
USER nobody |
| 30 |
ENV HOME /omg/really/gosu/nowhere |
| 31 |
# now we should be nobody, ALL groups, and have a bogus useless HOME value |
| 32 |
|
| 33 |
RUN id |
| 34 |
|
| 35 |
RUN gosu-t 0 "0:0:$(id -G root)" "root:root:$(id -Gn root)" |
| 36 |
RUN gosu-t 0:0 '0:0:0' 'root:root:root' |
| 37 |
RUN gosu-t root "0:0:$(id -G root)" "root:root:$(id -Gn root)" |
| 38 |
RUN gosu-t 0:root '0:0:0' 'root:root:root' |
| 39 |
RUN gosu-t root:0 '0:0:0' 'root:root:root' |
| 40 |
RUN gosu-t root:root '0:0:0' 'root:root:root' |
| 41 |
RUN gosu-t 1000 "1000:$(id -g):$(id -g)" "1000:$(id -gn):$(id -gn)" |
| 42 |
RUN gosu-t 0:1000 '0:1000:1000' 'root:1000:1000' |
| 43 |
RUN gosu-t 1000:1000 '1000:1000:1000' '1000:1000:1000' |
| 44 |
RUN gosu-t root:1000 '0:1000:1000' 'root:1000:1000' |
| 45 |
RUN gosu-t 1000:root '1000:0:0' '1000:root:root' |
| 46 |
RUN gosu-t 1000:daemon "1000:$(id -g daemon):$(id -g daemon)" '1000:daemon:daemon' |
| 47 |
RUN gosu-t games "$(id -u games):$(id -g games):$(id -G games)" 'games:games:games users' |
| 48 |
RUN gosu-t games:daemon "$(id -u games):$(id -g daemon):$(id -g daemon)" 'games:daemon:daemon' |
| 49 |
|
| 50 |
RUN gosu-t 0: "0:0:$(id -G root)" "root:root:$(id -Gn root)" |
| 51 |
RUN gosu-t '' "$(id -u):$(id -g):$(id -G)" "$(id -un):$(id -gn):$(id -Gn)" |
| 52 |
RUN gosu-t ':0' "$(id -u):0:0" "$(id -un):root:root" |
| 53 |
|
| 54 |
RUN [ "$(gosu 0 env | grep '^HOME=')" = 'HOME=/root' ] |
| 55 |
RUN [ "$(gosu 0:0 env | grep '^HOME=')" = 'HOME=/root' ] |
| 56 |
RUN [ "$(gosu root env | grep '^HOME=')" = 'HOME=/root' ] |
| 57 |
RUN [ "$(gosu 0:root env | grep '^HOME=')" = 'HOME=/root' ] |
| 58 |
RUN [ "$(gosu root:0 env | grep '^HOME=')" = 'HOME=/root' ] |
| 59 |
RUN [ "$(gosu root:root env | grep '^HOME=')" = 'HOME=/root' ] |
| 60 |
RUN [ "$(gosu 0:1000 env | grep '^HOME=')" = 'HOME=/root' ] |
| 61 |
RUN [ "$(gosu root:1000 env | grep '^HOME=')" = 'HOME=/root' ] |
| 62 |
RUN [ "$(gosu 1000 env | grep '^HOME=')" = 'HOME=/' ] |
| 63 |
RUN [ "$(gosu 1000:0 env | grep '^HOME=')" = 'HOME=/' ] |
| 64 |
RUN [ "$(gosu 1000:root env | grep '^HOME=')" = 'HOME=/' ] |
| 65 |
RUN [ "$(gosu games env | grep '^HOME=')" = 'HOME=/usr/games' ] |
| 66 |
RUN [ "$(gosu games:daemon env | grep '^HOME=')" = 'HOME=/usr/games' ] |
| 67 |
|
| 68 |
# make sure we error out properly in unexpected cases like an invalid username |
| 69 |
RUN ! gosu bogus true |
| 70 |
RUN ! gosu 0day true |
| 71 |
RUN ! gosu 0:bogus true |
| 72 |
RUN ! gosu 0:0day true |
| 73 |
|
| 74 |
# something missing? some other functionality we could test easily? PR! :D |