Skip to content

feat: Enhanced secret scan no longer relies on env vars #6333

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

Conversation

aitchiss
Copy link
Contributor

@aitchiss aitchiss commented May 19, 2025

🎉 Thanks for submitting a pull request! 🎉

Summary

Fixes https://linear.app/netlify/issue/WRFL-2554/enhanced-scan-shouldnt-rely-on-value-being-an-env-var

The users most at risk of shipping secrets to production accidentally are folks who haven't configured env vars yet. This PR switches up the implementation of the enhanced secret scan so that we don't rely on env vars at all.

Previously: all env vars checked in case they are secrets, and just haven't been marked as 'secret' by the user
Now: we check file content directly for anything that looks like a secret (based on length and prefix), and if we find one we fail the build

This PR also:

  • improves logging to disambiguate "likely secrets" vs "secret env vars" detected
  • provides for env var ENHANCED_SECRETS_SCAN_OMIT_VALUES and ENHANCED_SECRETS_SCAN_ENABLED to allow users to opt out of the new functionality or safelist values without sacrificing the explicit secrets env var checks

For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures
    we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or
    something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures
    your code follows our style guide and passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as outdated.

@aitchiss aitchiss self-assigned this May 20, 2025
@netlify netlify deleted a comment from github-actions bot May 20, 2025
@netlify netlify deleted a comment from github-actions bot May 20, 2025
Copy link
Contributor

This pull request adds or modifies JavaScript (.js, .cjs, .mjs) files.
Consider converting them to TypeScript.

Copy link
Contributor

This pull request adds or modifies JavaScript (.js, .cjs, .mjs) files.
Consider converting them to TypeScript.

@aitchiss aitchiss marked this pull request as ready for review May 20, 2025 14:12
@aitchiss aitchiss requested a review from a team as a code owner May 20, 2025 14:12
Copy link
Contributor

This pull request adds or modifies JavaScript (.js, .cjs, .mjs) files.
Consider converting them to TypeScript.

@aitchiss aitchiss requested a review from pieh May 21, 2025 10:01
// Note: Using the global flag (g) means this regex object maintains state between executions.
// We would need to reset lastIndex to 0 if we wanted to reuse it on the same string multiple times.
const likelySecretRegex = new RegExp(
`(?:["'\`]|^|[=:,]) *(?:${prefixMatchingRegex})[^ "'\`=:,]{${MIN_CHARS_AFTER_PREFIX}}[^ "'\`=:,]*?(?:["'\`]|[ =:,]|$)`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use capturing group for token so that we don't have to do massaging after finding a match later in?

const token = match[0].replace(/^["'`=:, ]+|["'`=:, ]+$/g, '')
Suggested change
`(?:["'\`]|^|[=:,]) *(?:${prefixMatchingRegex})[^ "'\`=:,]{${MIN_CHARS_AFTER_PREFIX}}[^ "'\`=:,]*?(?:["'\`]|[ =:,]|$)`,
`(?:["'\`]|^|[=:,]) *(?<token>(?:${prefixMatchingRegex})[^ "'\`=:,]{${MIN_CHARS_AFTER_PREFIX}}[^ "'\`=:,]*?)(?:["'\`]|[ =:,]|$)`,

And later

-let match
+let match: RegExpExecArray | null
 while ((match = likelySecretRegex.exec(line)) !== null) {
-  const token = match[0].replace(/^["'`=:, ]+|["'`=:, ]+$/g, '')
+  const token = match.groups?.token

Copy link
Contributor

This pull request adds or modifies JavaScript (.js, .cjs, .mjs) files.
Consider converting them to TypeScript.

@aitchiss aitchiss enabled auto-merge (squash) May 23, 2025 08:58
Copy link
Contributor

This pull request adds or modifies JavaScript (.js, .cjs, .mjs) files.
Consider converting them to TypeScript.

@aitchiss aitchiss merged commit 86dead5 into main May 23, 2025
32 of 33 checks passed
@aitchiss aitchiss deleted the suzanne/wrfl-2554-enhanced-scan-shouldnt-rely-on-value-being-an-env-var branch May 23, 2025 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants