Skip to content

Conversation

rvesse
Copy link
Contributor

@rvesse rvesse commented Mar 24, 2025

As discussed in #422 the current way that the Trivy action injects the GitHub Actions inputs to trivy is by adding these to the GITHUB_ENV file which is then used to populate the environment for all subsequent job steps. This leads to a problem where a subsequent call to aquasecurity/trivy-action may incorrectly pick up inputs from a previous call because those previous inputs are already set in the environment file, and the action only sets those environment variables to new values if the new inputs differ from the defaults.

This PR takes an approach that @DmitriyLewen was already developing in his fork and finishes it up ready for review. Basically instead of writing the inputs to the GITHUB_ENV file it writes them, in the form of export <var>=<value> statements to a temporary trivy_envs.txt file, and has the entrypoint.sh script source that file. The file is cleaned up as part of the action running. This ensures that inputs from one call to the action cannot be leaked into a subsequent call to the action.

Note that supersedes another approach proposed in #437 since as discussed there that approach still potentially leaks the Trivy action inputs across job steps. As noted in that PR the alternative approach in this PR also likely fixes other related issues #438 and #435

Testing

  • Have added a BATS test in this repository that verifies that the entrypoint.sh script does load the trivy_envs.txt file correctly
  • Have been testing my fork against my $dayjob workflows where we were encountering this issue with input leakage leading to Trivy scans not generating output in our desired formats. With the fork those workflows are now generating the correct output, and GitHub Actions debug logging has been used to confirm that inputs are no longer being set in the environment for subsequent trivy-action invocations as we had previously noted. Now the only env variables we are seeing passed into trivy-action are the ones we explicitly add ourselves.
  • Tests repository, again based on @DmitriyLewen's earlier work, that compares behaviour across master, the previous proposed fix and my proposed fix - https://github.com/rvesse/test-trivy-action/actions/runs/14216797998

E.g. With current master, environment leakage occurs:

Current Master with Env Leakage

With the changes from this branch, environment leakage no longer occurs and all inputs are honoured as expected:

Example Action Invocation with No Env Leakage

@rvesse rvesse changed the title Fix overwrite envs (#422) Fix Trivy action inputs leaking between invocations (#422) Mar 24, 2025
@rvesse rvesse force-pushed the fix/overwrite-envs branch from d167969 to a4fc06b Compare March 24, 2025 15:37
@rvesse rvesse marked this pull request as ready for review March 24, 2025 15:41
Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rvesse Thanks for your work!
LGTM
left small comment.

Can you also add examples with multiple few test runs (as example - #397)?
I do that in test repository (https://github.com/DmitriyLewen/test-trivy-action/actions)
you can do same.

@simar7 Can you also take a look?

@rvesse
Copy link
Contributor Author

rvesse commented Mar 25, 2025

Can you also add examples with multiple few test runs (as example - #397)? I do that in test repository (https://github.com/DmitriyLewen/test-trivy-action/actions) you can do same.

I have a repo where we're wrapping our usage of Trivy up in a composite action which was what led me to discovering this bug and the various proposed fixes. I can likely get that made public tomorrow and add a link here when that's available

Dump the generated environment variables file only when tests are run
with actions debug logging
@rvesse rvesse force-pushed the fix/overwrite-envs branch from cf8c1ca to c0352d3 Compare March 28, 2025 10:24
This is done as long as they have a non-empty input value, or a
non-empty default value.
@rvesse rvesse force-pushed the fix/overwrite-envs branch from c0352d3 to 732bcb3 Compare March 28, 2025 10:34
@rvesse
Copy link
Contributor Author

rvesse commented Mar 28, 2025

@DmitriyLewen Forked your tests and modified the test workflow so it tests current master, the previous fix proposal from #437 and this proposed fix - https://github.com/rvesse/test-trivy-action/actions/runs/14127078253 - all side by side in a single workflow for comparison

Did spot an issue with my fix around environment variables not being set when they were the defaults, since the defaults for the action inputs don't necessarily match the defaults for the trivy CLI, I've changed that to just always set the environment variables if a non-empty input value is received, or a default value should be set. As this PR isolates environment variables to each trivy-action invocation this seemed fine to do and gives the correct behaviour AFAICT from my testing.

You can also see my fix in action over on my $dayjob wrapper action around our usage of Trivy - https://github.com/telicent-oss/trivy-action/actions/runs/14127012407 - where we're using a second invocation with template output to populate GitHub Actions summaries. Prior to the fix we were getting the JSON output despite having requested a different output format - https://github.com/telicent-oss/trivy-action/actions/runs/14034107613

Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good for me.
@rvesse Thanks for your work, help and good testsing!

Left small comments.

Can you also add https://github.com/rvesse/test-trivy-action/actions/runs/14127078253 into PR description?

@simar7 Can you also take a look?

@simar7
Copy link
Member

simar7 commented Apr 1, 2025

@rvesse thanks for the PR! Only one comment related to documentation: Old GitHub action runs are pruned for logs over a period of time. Therefore, we should document this in the readme of this project. Screenshots or other associated materials are fine.

Copy link
Member

@simar7 simar7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just left one comment with regards to the docs.

@rvesse
Copy link
Contributor Author

rvesse commented Apr 1, 2025

Thanks for the reviews, didn't get chance to work on this today, will tidy everything up as requested tomorrow

- Remove unnecessary debug statements in the action used during testing
- Additional explanatory comments
- Fix to address case where caller explicitly injects environment
  variables, either via env: block on the action call or via GITHUB_ENV
@rvesse rvesse force-pushed the fix/overwrite-envs branch 2 times, most recently from 93f8287 to 2895385 Compare April 2, 2025 09:46
Noted in documenting this fix that what had been implemented deviated
from the existing configuration priority documentation.  Amended the
implementation of the Action to try and restore that consistency.
@rvesse rvesse force-pushed the fix/overwrite-envs branch from 2895385 to 830ec6f Compare April 2, 2025 09:54
is the case by looking at the GitHub Actions step information, if the `env` section shown in your Actions output
contains `TRIVY_*` environment variables you did not explicitly set then you may be affected by this bug and should
upgrade to the latest Action version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simar7 Wasn't really sure how/what you wanted to document in the README for this PR, let me know if this is appropriate or if you had something else in mind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a screenshot showing Actions output with env leakage happening to help users spot if they're affected by this, similar to what I showed in the PR description?

Screenshot 2025-04-02 at 11 03 29

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is pretty good! Thanks.

echo "$var_name=$input_value" >> $GITHUB_ENV
if [ ! -z "$input_value" && "$input_value" != "$default_value" ]; then
# If action was provided with explicit input by the caller set that
echo "export $var_name=$input_value" >> trivy_envs.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ending up removing the extra logic for defaults I previously added as realised this wasn't needed, and would cause behaviour to deviate from configuration priority that is documented/intended for the action

The weird behaviour I'd seen in tests of defaults apparently not being honoured was actually due to the tests repository having a trivy.yaml file present which was automatically getting picked up and throwing off behaviour for the default output test

@simar7 simar7 merged commit 7aca5ac into aquasecurity:master Apr 5, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants