+
Skip to content

feat: Audit secrets outside an environment #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

aldur
Copy link

@aldur aldur commented Mar 31, 2025

Would close #184.

Early draft, mainly to gather feedback (as this scratch an itch I have :)). Specifically, doing string matching in the with/env section might not be robust enough and/or might lead to false positives.

Sorry about the noise in the imports, that is what rustfmt has suggested for me. That can be reverted if it doesn't match the project style.

@woodruffw
Copy link
Member

Hey @aldur, thanks for taking a stab at this! I'll do a high-level review later today or tomorrow.

@aldur
Copy link
Author

aldur commented Apr 10, 2025

Thanks for the comments! I have addressed them, added a snapshot and had to modify use-trusted-publishing.yml in 6b963b6 for cargo insta test to pass.

I have also some .snap.new files in my cwd, should I add them to this PR?

@aldur aldur marked this pull request as ready for review April 10, 2025 07:13
@woodruffw
Copy link
Member

I have also some .snap.new files in my cwd, should I add them to this PR?

You should run cargo insta test --review and then accept them, and then check those files in 🙂

@aldur
Copy link
Author

aldur commented Apr 22, 2025

I have also some .snap.new files in my cwd, should I add them to this PR?

You should run cargo insta test --review and then accept them, and then check those files in 🙂

Done, thank you!

@woodruffw
Copy link
Member

Thanks @aldur, this is looking really good!

One thing I'm realizing, based on the changes to the other tests: this might be slightly too conservative as a default audit, at least for now, since a lot of people have relatively simple CI/CD setups with 1-2 workflows where an environment wouldn't provide much isolation.

Given that, I'm thinking that it might make sense to mark this as a "pedantic" audit for now, i.e. by calling .persona(Persona::Pedantic) on each finding produced. How would you feel about that? IMO that would allow people to opt into the behavior here while not being too disruptive, and we could make it a "regular" audit in the future (or make it enableable in the regular persona once #617 is fleshed out more).

@woodruffw woodruffw added enhancement New feature or request new-audit New audits labels Apr 23, 2025
@aldur aldur force-pushed the feat/secrets_outside_env branch 2 times, most recently from 1048e11 to 0132617 Compare May 5, 2025 06:08
@aldur
Copy link
Author

aldur commented May 5, 2025

Thanks @aldur, this is looking really good!

One thing I'm realizing, based on the changes to the other tests: this might be slightly too conservative as a default audit, at least for now, since a lot of people have relatively simple CI/CD setups with 1-2 workflows where an environment wouldn't provide much isolation.

Given that, I'm thinking that it might make sense to mark this as a "pedantic" audit for now, i.e. by calling .persona(Persona::Pedantic) on each finding produced. How would you feel about that? IMO that would allow people to opt into the behavior here while not being too disruptive, and we could make it a "regular" audit in the future (or make it enableable in the regular persona once #617 is fleshed out more).

Done! Sorry it took me some time to get back to this :)

@woodruffw
Copy link
Member

Done! Sorry it took me some time to get back to this :)

No problem at all! I'll do another review tonight.


audit_meta!(
SecretsOutsideEnvironment,
"secrets-outside-environment",
Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to make new audit names a bit shorter, so some ideas here:

  1. secret-outside-env
  2. secret-without-env
  3. secret-missing-env
  4. secret-no-env (shortest, but maybe not easy to understand)

Curious if you have any other naming thoughts as well 🙂

Copy link
Author

Choose a reason for hiding this comment

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

Picked number 2 and fixed in 96c90ac.

step: &crate::models::Step<'w>,
findings: &mut Vec<crate::finding::Finding<'w>>,
) -> anyhow::Result<()> {
if s.contains("secrets") {
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 we need to exclude secrets.github_token here, since that's a "latent" secret that's present regardless of any environment.

Copy link
Author

@aldur aldur May 6, 2025

Choose a reason for hiding this comment

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

Yep that makes sense! Fixed it in b5dab62.

working_directory: _,
} => match env {
LoE::Expr(e) => {
Self::check_secrets_access(e.as_bare(), step, &mut findings)?;
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 check needs to be a little more discerning: e needs to be parsed into an Expr, which then needs to be walked recursively to visit each Expr::Context and match that on the secrets namespace.

Without that, we'll end up with (admittedly goofy) false positives like:

with:
  yolo: ${{ 'secrets' }}

Writing the AST walker is currently a bit annoying/manual; if you want to wait until I add a Visitor API that might make your life a bit easier (I plan on doing that pretty soon) 🙂

Copy link
Author

Choose a reason for hiding this comment

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

I like that! It will also increase the audit confidence. I am ok with waiting for the Visitor API, just give me a ping once ready and I'll put it to work :)

eenv = with;
}
StepBody::Run {
run: _,
Copy link
Member

Choose a reason for hiding this comment

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

Note: I'm okay with skipping run: blocks for an MVP here, but we will need to extract expressions from those and scan for secrets as well at some point.

Copy link
Author

@aldur aldur May 6, 2025

Choose a reason for hiding this comment

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

Makes sense! I naively thought that wouldn't be required, would this be an example?

- name: Clean install dependencies and build
  run: |
    npm ci
    PASSWORD=${{ secrets.password }} npm run build

Copy link
Member

Choose a reason for hiding this comment

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

Yep, exactly 🙂

@aldur
Copy link
Author

aldur commented Jun 17, 2025

Hi there! Any news on the Visitor API? I can give this another shot and get it merged.

@woodruffw
Copy link
Member

Hey @aldur, sorry for the delay!

The Visitor API is high on my stack -- it got deferred a bit because of some larger expression API refactoring that needed to be done to enable #240, but now that that's done I'll be working on it. So you should be unblocked here shortly.

(P.S. Sorry for the huge conflict set here -- with any luck most of it can probably be discarded and the snapshot tests can be re-done on top of just the audit changes.)

@woodruffw
Copy link
Member

I'm done with the expression API refactoring, but I wasn't able to reach a Visitor API design that made a lot of sense -- all of the patterns I tried didn't map super well to our existing use patterns, or could be expressed more easily/cleanly with the existing "match and recurse" style.

Given that, I think we can move forward without blocking on any larger visitor API changes here; the existing APIs should work well enough. Sorry for such a delay on my end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new-audit New audits
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Audit idea: Secrets outside of environments
2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载