Filesystem secret scanner

You build a Go filesystem scanner that walks an fs.FS and returns sorted findings containing only file paths, line numbers, and rule names.

Ticket

You build internal/secretscan, a Go package that scans a supplied fs.FS and returns findings for lines that match credential patterns. Each finding identifies the path, line, and rule without carrying the credential itself.

This protects repository and deployment code from credentials committed by mistake. The optional release-safety lesson shows how a scan can gate image publication in GitHub Actions. The required Week 7 deployment work does not depend on this extension.

The tests call these exported declarations:

go
package secretscan

import "io/fs"

type Finding struct {
    Path string
    Line int
    Rule string
}

func Scan(fsys fs.FS) ([]Finding, error)

Acceptance criteria

  • A scan of deploy.sh containing AKIAIOSFODNN7EXAMPLE on line 3 returns one finding with Path: "deploy.sh", Line: 3, and Rule: "aws-access-key".
  • A line containing -----BEGIN RSA PRIVATE KEY----- in keys/server.pem produces a finding with Rule: "private-key" and Line: 1.
  • A ghp_ token followed by 36 alphanumeric characters in ci.yml produces a finding with Rule: "github-token" and Line: 2.
  • The assignments password: "hunter2hunter2", JWT_SECRET=abcdefgh1234, api_key = 'k9d8s7f6g5h4', and DB_TOKEN: zz99yy88xx77 produce four findings in config.txt. Each has Rule: "generic-secret" and its corresponding line number from 1 to 4.
  • The values ${JWT_SECRET}, ${DB_PASSWORD}, an empty PASSWORD=, short, $SECRET_FROM_ENV, and <your-key-here> produce no findings.
  • Files ending in .example, including .env.example and config.yml.example, produce no findings.
  • A file containing a NUL byte, such as logo.png with binary content and an AWS-looking key, produces no findings.
  • Directories named .git and node_modules are excluded. In the supplied tree, only app/src/index.ts produces a finding; files under .git and app/node_modules do not.
  • When a line contains both an AWS access key and a generic secret assignment, only the aws-access-key finding is returned for that line. Findings are ordered by path and then line: a.txt line 1 (generic-secret), a.txt line 3 (aws-access-key), and b.txt line 2 (aws-access-key).
  • Formatted findings never contain the credential values AKIAIOSFODNN7EXAMPLE or hunter2hunter2.
  • A clean filesystem containing main.go and README.md returns no findings.

Constraints

  • Use the supplied fs.FS as the filesystem boundary. Do not convert its paths into operating-system paths.
  • Do not modify the provided tests.

Design direction

backend/internal/secretscan/scan.go is a natural home for the package. Keep filesystem traversal, file eligibility, line matching, and result ordering separate. Any layout that meets the contract passes.

Consider these design choices:

  • Make line matching return a rule name rather than the matched text. This keeps findings safe to display and log.
  • Evaluate specific credential patterns before generic assignments. Explicit priority prevents a broad assignment pattern from hiding a more specific match.
  • Treat unreadable files differently from traversal failures. A file that cannot be read can be skipped, but a traversal failure should not be mistaken for a clean scan. Add context through error wrapping without including file contents.
  • Keep the full pattern set and placeholder handling in mind. A Compose environment file should use environment references rather than committed credentials.

How to check your work

Run the optional exercise tests from the backend directory:

bash
cd backend
make test W=7O1

Read the first failing assertion's actual and expected Finding values. Check the path, one-based line number, and rule before investigating later failures.

Finished Exercise 7.16?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. backend/internal/secretscan/scan.go exposes Finding and Scan.
  2. AWS, GitHub, private-key, and generic-secret patterns are detected.
  3. Placeholders, example files, binary files, and excluded directories are skipped.
  4. Findings are sorted and contain metadata only.
  5. make test W=7O1 passes all 11 tests.

Commands

terminal
cd backend && make test W=7O1

Tests

  • TestW7O1_FindsAnAwsAccessKeyOnTheRightLine in backend/internal/secretscan/scan_test.go
  • TestW7O1_FindsAPrivateKeyHeader in backend/internal/secretscan/scan_test.go
  • TestW7O1_FindsAGithubToken in backend/internal/secretscan/scan_test.go
  • TestW7O1_FindsGenericAssignmentsOfSecretLookingNames in backend/internal/secretscan/scan_test.go
  • TestW7O1_IgnoresPlaceholdersAndShortValues in backend/internal/secretscan/scan_test.go
  • TestW7O1_SkipsExampleFiles in backend/internal/secretscan/scan_test.go
  • TestW7O1_SkipsBinaryFiles in backend/internal/secretscan/scan_test.go
  • TestW7O1_SkipsGitAndNodeModules in backend/internal/secretscan/scan_test.go
  • TestW7O1_ReportsOneFindingPerLineSortedByPathThenLine in backend/internal/secretscan/scan_test.go
  • TestW7O1_FindingsNeverContainTheSecretValue in backend/internal/secretscan/scan_test.go
  • TestW7O1_CleanTreeHasNoFindings in backend/internal/secretscan/scan_test.go