Skip to content

Commit d72a854

Browse files
github-actions[bot]romainkomorn-exdatadogmabdinur
authored
fix(ci_visibility): sanitize GITHUB_SERVER_URL env var when parsing GitHub CI env [backport 1.20] (#7600)
Backport a599002 from #7583 to 1.20. This fixes prevents potential credentials in GITHUB_SERVER_URL env vars from leaking into the `_dd.ci.env_vars` tag. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. Co-authored-by: Romain Komorn <[email protected]> Co-authored-by: Munir Abdinur <[email protected]>
1 parent 9f439cc commit d72a854

File tree

3 files changed

+192
-17
lines changed

3 files changed

+192
-17
lines changed

ddtrace/ext/ci.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -337,31 +337,33 @@ def extract_github_actions(env):
337337
# type: (MutableMapping[str, str]) -> Dict[str, Optional[str]]
338338
"""Extract CI tags from Github environ."""
339339

340+
github_server_url = _filter_sensitive_info(env.get("GITHUB_SERVER_URL"))
341+
github_repository = env.get("GITHUB_REPOSITORY")
342+
git_commit_sha = env.get("GITHUB_SHA")
343+
github_run_id = env.get("GITHUB_RUN_ID")
344+
run_attempt = env.get("GITHUB_RUN_ATTEMPT")
345+
340346
pipeline_url = "{0}/{1}/actions/runs/{2}".format(
341-
env.get("GITHUB_SERVER_URL"),
342-
env.get("GITHUB_REPOSITORY"),
343-
env.get("GITHUB_RUN_ID"),
347+
github_server_url,
348+
github_repository,
349+
github_run_id,
344350
)
345-
run_attempt = env.get("GITHUB_RUN_ATTEMPT")
346-
if run_attempt:
347-
pipeline_url = "{0}/attempts/{1}".format(pipeline_url, run_attempt)
348351

349352
env_vars = {
350-
"GITHUB_SERVER_URL": env.get("GITHUB_SERVER_URL"),
351-
"GITHUB_REPOSITORY": env.get("GITHUB_REPOSITORY"),
352-
"GITHUB_RUN_ID": env.get("GITHUB_RUN_ID"),
353+
"GITHUB_SERVER_URL": github_server_url,
354+
"GITHUB_REPOSITORY": github_repository,
355+
"GITHUB_RUN_ID": github_run_id,
353356
}
354-
if env.get("GITHUB_RUN_ATTEMPT") is not None:
355-
env_vars["GITHUB_RUN_ATTEMPT"] = env["GITHUB_RUN_ATTEMPT"]
357+
if run_attempt:
358+
env_vars["GITHUB_RUN_ATTEMPT"] = run_attempt
359+
pipeline_url = "{0}/attempts/{1}".format(pipeline_url, run_attempt)
356360

357361
return {
358362
git.BRANCH: env.get("GITHUB_HEAD_REF") or env.get("GITHUB_REF"),
359-
git.COMMIT_SHA: env.get("GITHUB_SHA"),
360-
git.REPOSITORY_URL: "{0}/{1}.git".format(env.get("GITHUB_SERVER_URL"), env.get("GITHUB_REPOSITORY")),
361-
JOB_URL: "{0}/{1}/commit/{2}/checks".format(
362-
env.get("GITHUB_SERVER_URL"), env.get("GITHUB_REPOSITORY"), env.get("GITHUB_SHA")
363-
),
364-
PIPELINE_ID: env.get("GITHUB_RUN_ID"),
363+
git.COMMIT_SHA: git_commit_sha,
364+
git.REPOSITORY_URL: "{0}/{1}.git".format(github_server_url, github_repository),
365+
JOB_URL: "{0}/{1}/commit/{2}/checks".format(github_server_url, github_repository, git_commit_sha),
366+
PIPELINE_ID: github_run_id,
365367
PIPELINE_NAME: env.get("GITHUB_WORKFLOW"),
366368
PIPELINE_NUMBER: env.get("GITHUB_RUN_NUMBER"),
367369
PIPELINE_URL: pipeline_url,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
CI Visibility: fixes the fact that the GITHUB_SERVER_URL environment variable was not being sanitized for
5+
credentials

tests/tracer/fixtures/ci/github.json

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,174 @@
352352
"git.tag": "0.1.0"
353353
}
354354
],
355+
[
356+
{
357+
"GITHUB_ACTION": "run",
358+
"GITHUB_JOB": "github-job-name",
359+
"GITHUB_REF": "origin/tags/0.1.0",
360+
"GITHUB_REPOSITORY": "ghactions-repo",
361+
"GITHUB_RUN_ID": "ghactions-pipeline-id",
362+
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
363+
"GITHUB_SERVER_URL": "https://github.com",
364+
"GITHUB_SHA": "ghactions-commit",
365+
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
366+
"GITHUB_WORKSPACE": "/foo/bar"
367+
},
368+
{
369+
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://github.com\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\"}",
370+
"ci.job.name": "github-job-name",
371+
"ci.job.url": "https://github.com/ghactions-repo/commit/ghactions-commit/checks",
372+
"ci.pipeline.id": "ghactions-pipeline-id",
373+
"ci.pipeline.name": "ghactions-pipeline-name",
374+
"ci.pipeline.number": "ghactions-pipeline-number",
375+
"ci.pipeline.url": "https://github.com/ghactions-repo/actions/runs/ghactions-pipeline-id",
376+
"ci.provider.name": "github",
377+
"ci.workspace_path": "/foo/bar",
378+
"git.commit.sha": "ghactions-commit",
379+
"git.repository_url": "https://github.com/ghactions-repo.git",
380+
"git.tag": "0.1.0"
381+
}
382+
],
383+
[
384+
{
385+
"GITHUB_ACTION": "run",
386+
"GITHUB_JOB": "github-job-name",
387+
"GITHUB_REF": "origin/tags/0.1.0",
388+
"GITHUB_REPOSITORY": "ghactions-repo",
389+
"GITHUB_RUN_ID": "ghactions-pipeline-id",
390+
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
391+
"GITHUB_SERVER_URL": "https://user:[email protected]",
392+
"GITHUB_SHA": "ghactions-commit",
393+
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
394+
"GITHUB_WORKSPACE": "/foo/bar"
395+
},
396+
{
397+
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://github.com\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\"}",
398+
"ci.job.name": "github-job-name",
399+
"ci.job.url": "https://github.com/ghactions-repo/commit/ghactions-commit/checks",
400+
"ci.pipeline.id": "ghactions-pipeline-id",
401+
"ci.pipeline.name": "ghactions-pipeline-name",
402+
"ci.pipeline.number": "ghactions-pipeline-number",
403+
"ci.pipeline.url": "https://github.com/ghactions-repo/actions/runs/ghactions-pipeline-id",
404+
"ci.provider.name": "github",
405+
"ci.workspace_path": "/foo/bar",
406+
"git.commit.sha": "ghactions-commit",
407+
"git.repository_url": "https://github.com/ghactions-repo.git",
408+
"git.tag": "0.1.0"
409+
}
410+
],
411+
[
412+
{
413+
"GITHUB_ACTION": "run",
414+
"GITHUB_JOB": "github-job-name",
415+
"GITHUB_REF": "origin/tags/0.1.0",
416+
"GITHUB_REPOSITORY": "ghactions-repo",
417+
"GITHUB_RUN_ID": "ghactions-pipeline-id",
418+
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
419+
"GITHUB_SERVER_URL": "https://[email protected]",
420+
"GITHUB_SHA": "ghactions-commit",
421+
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
422+
"GITHUB_WORKSPACE": "/foo/bar"
423+
},
424+
{
425+
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://github.com\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\"}",
426+
"ci.job.name": "github-job-name",
427+
"ci.job.url": "https://github.com/ghactions-repo/commit/ghactions-commit/checks",
428+
"ci.pipeline.id": "ghactions-pipeline-id",
429+
"ci.pipeline.name": "ghactions-pipeline-name",
430+
"ci.pipeline.number": "ghactions-pipeline-number",
431+
"ci.pipeline.url": "https://github.com/ghactions-repo/actions/runs/ghactions-pipeline-id",
432+
"ci.provider.name": "github",
433+
"ci.workspace_path": "/foo/bar",
434+
"git.commit.sha": "ghactions-commit",
435+
"git.repository_url": "https://github.com/ghactions-repo.git",
436+
"git.tag": "0.1.0"
437+
}
438+
],
439+
[
440+
{
441+
"GITHUB_ACTION": "run",
442+
"GITHUB_JOB": "github-job-name",
443+
"GITHUB_REF": "origin/tags/0.1.0",
444+
"GITHUB_REPOSITORY": "ghactions-repo",
445+
"GITHUB_RUN_ID": "ghactions-pipeline-id",
446+
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
447+
"GITHUB_SERVER_URL": "https://user:[email protected]:1234",
448+
"GITHUB_SHA": "ghactions-commit",
449+
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
450+
"GITHUB_WORKSPACE": "/foo/bar"
451+
},
452+
{
453+
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://github.com:1234\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\"}",
454+
"ci.job.name": "github-job-name",
455+
"ci.job.url": "https://github.com:1234/ghactions-repo/commit/ghactions-commit/checks",
456+
"ci.pipeline.id": "ghactions-pipeline-id",
457+
"ci.pipeline.name": "ghactions-pipeline-name",
458+
"ci.pipeline.number": "ghactions-pipeline-number",
459+
"ci.pipeline.url": "https://github.com:1234/ghactions-repo/actions/runs/ghactions-pipeline-id",
460+
"ci.provider.name": "github",
461+
"ci.workspace_path": "/foo/bar",
462+
"git.commit.sha": "ghactions-commit",
463+
"git.repository_url": "https://github.com:1234/ghactions-repo.git",
464+
"git.tag": "0.1.0"
465+
}
466+
],
467+
[
468+
{
469+
"GITHUB_ACTION": "run",
470+
"GITHUB_JOB": "github-job-name",
471+
"GITHUB_REF": "origin/tags/0.1.0",
472+
"GITHUB_REPOSITORY": "ghactions-repo",
473+
"GITHUB_RUN_ID": "ghactions-pipeline-id",
474+
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
475+
"GITHUB_SERVER_URL": "https://user:[email protected]",
476+
"GITHUB_SHA": "ghactions-commit",
477+
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
478+
"GITHUB_WORKSPACE": "/foo/bar"
479+
},
480+
{
481+
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://1.1.1.1\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\"}",
482+
"ci.job.name": "github-job-name",
483+
"ci.job.url": "https://1.1.1.1/ghactions-repo/commit/ghactions-commit/checks",
484+
"ci.pipeline.id": "ghactions-pipeline-id",
485+
"ci.pipeline.name": "ghactions-pipeline-name",
486+
"ci.pipeline.number": "ghactions-pipeline-number",
487+
"ci.pipeline.url": "https://1.1.1.1/ghactions-repo/actions/runs/ghactions-pipeline-id",
488+
"ci.provider.name": "github",
489+
"ci.workspace_path": "/foo/bar",
490+
"git.commit.sha": "ghactions-commit",
491+
"git.repository_url": "https://1.1.1.1/ghactions-repo.git",
492+
"git.tag": "0.1.0"
493+
}
494+
],
495+
[
496+
{
497+
"GITHUB_ACTION": "run",
498+
"GITHUB_JOB": "github-job-name",
499+
"GITHUB_REF": "origin/tags/0.1.0",
500+
"GITHUB_REPOSITORY": "ghactions-repo",
501+
"GITHUB_RUN_ID": "ghactions-pipeline-id",
502+
"GITHUB_RUN_NUMBER": "ghactions-pipeline-number",
503+
"GITHUB_SERVER_URL": "https://user:[email protected]:1234",
504+
"GITHUB_SHA": "ghactions-commit",
505+
"GITHUB_WORKFLOW": "ghactions-pipeline-name",
506+
"GITHUB_WORKSPACE": "/foo/bar"
507+
},
508+
{
509+
"_dd.ci.env_vars": "{\"GITHUB_SERVER_URL\":\"https://1.1.1.1:1234\",\"GITHUB_REPOSITORY\":\"ghactions-repo\",\"GITHUB_RUN_ID\":\"ghactions-pipeline-id\"}",
510+
"ci.job.name": "github-job-name",
511+
"ci.job.url": "https://1.1.1.1:1234/ghactions-repo/commit/ghactions-commit/checks",
512+
"ci.pipeline.id": "ghactions-pipeline-id",
513+
"ci.pipeline.name": "ghactions-pipeline-name",
514+
"ci.pipeline.number": "ghactions-pipeline-number",
515+
"ci.pipeline.url": "https://1.1.1.1:1234/ghactions-repo/actions/runs/ghactions-pipeline-id",
516+
"ci.provider.name": "github",
517+
"ci.workspace_path": "/foo/bar",
518+
"git.commit.sha": "ghactions-commit",
519+
"git.repository_url": "https://1.1.1.1:1234/ghactions-repo.git",
520+
"git.tag": "0.1.0"
521+
}
522+
],
355523
[
356524
{
357525
"GITHUB_ACTION": "run",

0 commit comments

Comments
 (0)