From b11916dc517d2cb2cce1b49251db11db26045982 Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:47:56 -0300 Subject: [PATCH] fix(scripts): keep a self-hosted GitLab token on its own host runner-ctl.sh loads $MINIRUNNER_HOME/mini-runner.env with `.`, so a GITLAB_HOST set there without `export` (as mini-runner.env.example does) stayed in the control script's shell. start runs check-repo.sh as a child process, which then fell back to gitlab.com. With a self-hosted GitLab the project check asked the wrong server, and on the curl path it sent the self-hosted GITLAB_TOKEN to gitlab.com whenever that token was in the environment. runner-ctl.sh now exports GITLAB_HOST right after loading the env file when it is set, so the check asks the server named there. gh-runners-ctl.sh is unchanged: it only checks GitHub repositories. check-repo.sh no longer sends GITLAB_TOKEN to a default host. On the curl path (no glab), the token is sent only to a host named in GITLAB_HOST, gitlab.com included; with the token and no GITLAB_HOST, a GitLab target is reported UNVERIFIED (exit 3) and no request is made. The glab path is unchanged, since glab keeps its own login per --hostname. The script header, the env example and docs/public-repos.md state the rule. Tests: the host from the env file reaches a fake curl and a fake glab started by runner-ctl.sh, the token without GITLAB_HOST is never sent, and with GITLAB_HOST set the token goes only to that host. --- board/tests/test_check_repo.py | 57 +++++++++++++++++++++++++++++++++ docs/public-repos.md | 3 +- scripts/check-repo.sh | 10 ++++-- scripts/mini-runner.env.example | 3 +- scripts/runner-ctl.sh | 2 ++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/board/tests/test_check_repo.py b/board/tests/test_check_repo.py index 8a54f32..b64a2f3 100644 --- a/board/tests/test_check_repo.py +++ b/board/tests/test_check_repo.py @@ -21,6 +21,12 @@ [ -f "$f" ] && cat "$f" || exit 1 """ FAKE_PROOT = "#!/bin/sh\nsleep 300\n" +# fake curl: records its arguments, one call per line, and fails like curl -f on an HTTP error +FAKE_CURL = """#!/bin/sh +printf '%s\\n' "$*" >>"$CURL_LOG" +exit 22 +""" +FAKE_TOKEN = "fake-gitlab-token" class Base(unittest.TestCase): @@ -36,6 +42,7 @@ def setUp(self): (self.bin / name).chmod(0o755) self.env = {**os.environ, "FIXTURES": str(self.fix), "MINIRUNNER_GH": str(self.bin / "gh"), "MINIRUNNER_GLAB": str(self.bin / "glab"), "GH_TOKEN": "", "GITLAB_TOKEN": ""} + self.env.pop("GITLAB_HOST", None) def put(self, path, body): name = path @@ -57,6 +64,17 @@ def gitlab(self, project, visibility, parent=True, runners=()): def check(self, *targets, env=None): return subprocess.run(["sh", str(CHECK), *targets], capture_output=True, text=True, env=env or self.env) + def token_without_glab(self): + """GITLAB_TOKEN set, `glab` absent and a recording fake curl first on PATH: the curl path.""" + (self.bin / "curl").write_text(FAKE_CURL) + (self.bin / "curl").chmod(0o755) + self.curl_log = self.tmp / "curl.log" + self.env.update(GITLAB_TOKEN=FAKE_TOKEN, MINIRUNNER_GLAB=str(self.bin / "absent"), + CURL_LOG=str(self.curl_log), PATH=f"{self.bin}:{os.environ.get('PATH', '/usr/bin:/bin')}") + + def curl_calls(self): + return self.curl_log.read_text().splitlines() if self.curl_log.exists() else [] + class CheckRepo(Base): def test_private_github_repo_passes(self): @@ -116,6 +134,24 @@ def test_gitlab_public_passes_with_the_member_gate_and_prints_the_stricter_setti def test_gitlab_unreadable_is_unverified(self): self.assertEqual(self.check("gitlab:acme/none").returncode, 3) + def test_gitlab_token_without_a_named_host_is_never_sent(self): + self.token_without_glab() + r = self.check("gitlab:acme/api") + self.assertEqual(r.returncode, 3, r.stdout) + self.assertIn("UNVERIFIED gitlab:acme/api", r.stdout) + self.assertIn("GITLAB_HOST", r.stdout) + self.assertEqual(self.curl_calls(), []) + + def test_gitlab_token_goes_to_the_named_host_only(self): + self.token_without_glab() + for host in ("gitlab.example.com", "gitlab.com"): + self.curl_log.unlink(missing_ok=True) + self.check("gitlab:acme/api", env={**self.env, "GITLAB_HOST": host}) + calls = self.curl_calls() + self.assertTrue(calls, host) + for call in calls: + self.assertIn(f"https://{host}/api/v4/projects/acme%2Fapi", call) + @unittest.skipUnless(shutil.which("setsid"), "needs setsid") class StartRefusal(Base): @@ -175,6 +211,27 @@ def test_gitlab_runner_checks_its_projects(self): self.assertNotEqual(r.returncode, 0) self.assertIn("never verified", r.stdout) + def test_gitlab_host_from_the_env_file_reaches_the_check(self): + # set in mini-runner.env without export, as the example does + (self.tmp / "mini-runner.env").write_text('GITLAB_RUNNER_PROJECTS="acme/api"\nGITLAB_HOST=gitlab.example.com\n') + self.token_without_glab() + r = self.ctl("runner-ctl.sh", "start") + self.assertNotEqual(r.returncode, 0, r.stdout) # the fake curl reads nothing: never verified + calls = self.curl_calls() + self.assertTrue(calls, r.stdout) + for call in calls: + self.assertIn("https://gitlab.example.com/api/v4/", call) + self.assertNotIn("https://gitlab.com/", call) + + def test_glab_is_asked_about_the_host_from_the_env_file(self): + (self.tmp / "mini-runner.env").write_text('GITLAB_RUNNER_PROJECTS="acme/api"\nGITLAB_HOST=gitlab.example.com\n') + hosts = self.tmp / "glab-hosts" + (self.bin / "glab").write_text(f'#!/bin/sh\nprintf "%s\\n" "$3" >>"{hosts}"\n' + FAKE_API.split("\n", 1)[1]) + self.gitlab("acme/api", "private") + r = self.ctl("runner-ctl.sh", "start") + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + self.assertEqual(set(hosts.read_text().split()), {"gitlab.example.com"}) + if __name__ == "__main__": unittest.main() diff --git a/docs/public-repos.md b/docs/public-repos.md index d98d568..bf69eb3 100644 --- a/docs/public-repos.md +++ b/docs/public-repos.md @@ -65,7 +65,8 @@ GITLAB_HOST=gitlab.example.com sh scripts/check-repo.sh gitlab:GROUP/PROJECT | `UNVERIFIED` | Could not read the repository or the setting (login, access, network) | 3 | It only reads. It needs `gh` or `GH_TOKEN` with repository admin read for GitHub, and `glab` or -`GITLAB_TOKEN` with Maintainer read for GitLab. +`GITLAB_TOKEN` with Maintainer read for GitLab. `GITLAB_TOKEN` is sent only to the host named in +`GITLAB_HOST`, so set it for gitlab.com too (`GITLAB_HOST=gitlab.com`). Name the repositories in `mini-runner.env` (`GH_RUNNER_REPOS="id=owner/repo ..."`, `GITLAB_RUNNER_PROJECTS="group/project ..."`) and the control scripts run the check on every start. An diff --git a/scripts/check-repo.sh b/scripts/check-repo.sh index 908a7f8..106d7c5 100755 --- a/scripts/check-repo.sh +++ b/scripts/check-repo.sh @@ -15,7 +15,8 @@ # # Reads only: it never changes a setting. GitHub needs repository admin read, through `gh` (MINIRUNNER_GH # overrides the binary) or GH_TOKEN with curl. GitLab needs Maintainer read, through `glab` -# (MINIRUNNER_GLAB) or GITLAB_TOKEN with curl. +# (MINIRUNNER_GLAB) or GITLAB_TOKEN with curl. GITLAB_TOKEN is sent only to a host named in GITLAB_HOST, +# gitlab.com included: with the token and no GITLAB_HOST, a GitLab target is UNVERIFIED and nothing is sent. # Exit: 0 every repository is safe, 1 at least one is unsafe, 2 usage, 3 could not verify. set -u GH=${MINIRUNNER_GH:-gh} @@ -33,12 +34,13 @@ api() { } GLAB=${MINIRUNNER_GLAB:-glab} +GITLAB_HOST_GIVEN=${GITLAB_HOST:-} GITLAB_HOST=${GITLAB_HOST:-gitlab.com} gl_api() { if command -v "$GLAB" >/dev/null 2>&1; then "$GLAB" api --hostname "$GITLAB_HOST" "$1" 2>/dev/null - elif [ -n "${GITLAB_TOKEN:-}" ] && command -v curl >/dev/null 2>&1; then + elif [ -n "${GITLAB_TOKEN:-}" ] && [ -n "$GITLAB_HOST_GIVEN" ] && command -v curl >/dev/null 2>&1; then curl -fsS -H "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://$GITLAB_HOST/api/v4/$1" 2>/dev/null else return 3 @@ -55,6 +57,10 @@ except Exception: print("unknown")'; } check_gitlab() { project=$1 id=$(printf %s "$project" | sed 's|/|%2F|g') + if ! command -v "$GLAB" >/dev/null 2>&1 && [ -n "${GITLAB_TOKEN:-}" ] && [ -z "$GITLAB_HOST_GIVEN" ]; then + echo "UNVERIFIED gitlab:$project: GITLAB_TOKEN is sent only to the host named in GITLAB_HOST, and it is not set (for gitlab.com, set GITLAB_HOST=gitlab.com)" + worse 3; return + fi body=$(gl_api "projects/$id") visibility=$(printf %s "$body" | field visibility) if [ -z "$visibility" ]; then diff --git a/scripts/mini-runner.env.example b/scripts/mini-runner.env.example index 4af80b3..86a7848 100644 --- a/scripts/mini-runner.env.example +++ b/scripts/mini-runner.env.example @@ -12,7 +12,8 @@ GITLAB_RUNNER_CONFIG=/etc/gitlab-runner/config.toml GITLAB_RUNNER_WORKDIR=/root # The projects the GitLab runner serves, as "group/project" paths separated by spaces. With this set, # start checks each one first (scripts/check-repo.sh gitlab:group/project). GITLAB_HOST names the server -# (default gitlab.com). The check needs `glab` logged in, or GITLAB_TOKEN, with Maintainer read. +# (default gitlab.com). The check needs `glab` logged in, or GITLAB_TOKEN, with Maintainer read; the token +# is sent only to the host named here, so keep GITLAB_HOST set, gitlab.com included. GITLAB_RUNNER_PROJECTS="" GITLAB_HOST=gitlab.com diff --git a/scripts/runner-ctl.sh b/scripts/runner-ctl.sh index fcc0fc4..544bd8e 100755 --- a/scripts/runner-ctl.sh +++ b/scripts/runner-ctl.sh @@ -11,6 +11,8 @@ set -u MINIRUNNER_HOME=${MINIRUNNER_HOME:-$HOME/mini-runner} # shellcheck disable=SC1091 [ -f "$MINIRUNNER_HOME/mini-runner.env" ] && . "$MINIRUNNER_HOME/mini-runner.env" +# a GITLAB_HOST set there without export must still reach check-repo.sh, which otherwise asks gitlab.com +[ -n "${GITLAB_HOST:-}" ] && export GITLAB_HOST RUN=${MINIRUNNER_RUN:-$MINIRUNNER_HOME/run} PROOT_DISTRO=${PROOT_DISTRO:-ubuntu} GITLAB_RUNNER_BIN=${GITLAB_RUNNER_BIN:-/usr/local/bin/gitlab-runner}