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:
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.shcontainingAKIAIOSFODNN7EXAMPLEon line 3 returns one finding withPath: "deploy.sh",Line: 3, andRule: "aws-access-key". - A line containing
-----BEGIN RSA PRIVATE KEY-----inkeys/server.pemproduces a finding withRule: "private-key"andLine: 1. - A
ghp_token followed by 36 alphanumeric characters inci.ymlproduces a finding withRule: "github-token"andLine: 2. - The assignments
password: "hunter2hunter2",JWT_SECRET=abcdefgh1234,api_key = 'k9d8s7f6g5h4', andDB_TOKEN: zz99yy88xx77produce four findings inconfig.txt. Each hasRule: "generic-secret"and its corresponding line number from 1 to 4. - The values
${JWT_SECRET},${DB_PASSWORD}, an emptyPASSWORD=,short,$SECRET_FROM_ENV, and<your-key-here>produce no findings. - Files ending in
.example, including.env.exampleandconfig.yml.example, produce no findings. - A file containing a NUL byte, such as
logo.pngwith binary content and an AWS-looking key, produces no findings. - Directories named
.gitandnode_modulesare excluded. In the supplied tree, onlyapp/src/index.tsproduces a finding; files under.gitandapp/node_modulesdo not. - When a line contains both an AWS access key and a generic secret assignment, only the
aws-access-keyfinding is returned for that line. Findings are ordered by path and then line:a.txtline 1 (generic-secret),a.txtline 3 (aws-access-key), andb.txtline 2 (aws-access-key). - Formatted findings never contain the credential values
AKIAIOSFODNN7EXAMPLEorhunter2hunter2. - A clean filesystem containing
main.goandREADME.mdreturns no findings.
Constraints
- Use the supplied
fs.FSas 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:
cd backend
make test W=7O1Read the first failing assertion's actual and expected Finding values. Check the path, one-based line number, and rule before investigating later failures.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
backend/internal/secretscan/scan.goexposesFindingandScan.- AWS, GitHub, private-key, and generic-secret patterns are detected.
- Placeholders, example files, binary files, and excluded directories are skipped.
- Findings are sorted and contain metadata only.
make test W=7O1passes all 11 tests.
Commands
cd backend && make test W=7O1Tests
TestW7O1_FindsAnAwsAccessKeyOnTheRightLineinbackend/internal/secretscan/scan_test.goTestW7O1_FindsAPrivateKeyHeaderinbackend/internal/secretscan/scan_test.goTestW7O1_FindsAGithubTokeninbackend/internal/secretscan/scan_test.goTestW7O1_FindsGenericAssignmentsOfSecretLookingNamesinbackend/internal/secretscan/scan_test.goTestW7O1_IgnoresPlaceholdersAndShortValuesinbackend/internal/secretscan/scan_test.goTestW7O1_SkipsExampleFilesinbackend/internal/secretscan/scan_test.goTestW7O1_SkipsBinaryFilesinbackend/internal/secretscan/scan_test.goTestW7O1_SkipsGitAndNodeModulesinbackend/internal/secretscan/scan_test.goTestW7O1_ReportsOneFindingPerLineSortedByPathThenLineinbackend/internal/secretscan/scan_test.goTestW7O1_FindingsNeverContainTheSecretValueinbackend/internal/secretscan/scan_test.goTestW7O1_CleanTreeHasNoFindingsinbackend/internal/secretscan/scan_test.go