Skip to content
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

pkg/assessor/manifest: Add sensitive variable names checks #189

Merged
merged 10 commits into from
Jul 23, 2022

Conversation

qequ
Copy link
Contributor

@qequ qequ commented Jul 4, 2022

Add check for sensitive variable names in commands history.
Add unit tests.
Add cli flag for adding sensitive keys to look for.

pkg/assessor/manifest/manifest.go Outdated Show resolved Hide resolved
pkg/assessor/manifest/manifest.go Outdated Show resolved Hide resolved
pkg/assessor/manifest/manifest.go Outdated Show resolved Hide resolved
sensitiveWords = append(sensitiveWords, strings.ToUpper(s))
}
pat := strings.ReplaceAll(`.*(REP).*`, "REP", strings.Join(sensitiveWords, "|"))
r, _ := regexp.Compile(pat)

Choose a reason for hiding this comment

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

Don't ignore regexp.Compiler error, it could failed from a malformed regex originated from user input.

pkg/assessor/manifest/manifest.go Outdated Show resolved Hide resolved
if !strings.Contains(word, "=") {
continue
}
varName := strings.Split(word, "=")[0]

Choose a reason for hiding this comment

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

You may catch a few false-positives with this approach, I can think of cases with command line arguments such as:

/bin/sh -c cmd --pass=$SECURE_PASSWORD
/bin/sh -c cmd --api_config=api.yaml

pkg/assessor/manifest/manifest.go Outdated Show resolved Hide resolved
pkg/assessor/manifest/manifest.go Outdated Show resolved Hide resolved
pkg/assessor/manifest/manifest_test.go Show resolved Hide resolved
qequ added 3 commits July 5, 2022 14:41
Use suspiciousEnvKey and remove sensitiveWords slice.
Change senstiveVars function signature. Returns boolean and sensitive word string if found.
Update regex to ignore case sensitivity & handle regex error.
Remove useless for.
Use Sprintf instead of ReplaceAll.
Update assessment message; now prints the suspicious env key found.

Signed-off-by: Alvaro Frias Garay <[email protected]>
@arieltorti
Copy link

@tomoyamachi Would you care to take a look at this one ? I'm probably missing a few stuff.

if err != nil {
return fmt.Errorf("compile suspicious key: %w", err)
}
suspiciousCompiler = r
Copy link
Collaborator

@tomoyamachi tomoyamachi Jul 16, 2022

Choose a reason for hiding this comment

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

regexp.Compile is very slow, so I changed to compiling and set it globally.
golang/go#26623 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should it be compiled before hand in GetAssessments and passed every time results, err := assessor.Assess(files) is called? or what's your idea @tomoyamachi ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed this function sets the regex pattern globally so this solves the problem of compiling every time the regex

Copy link
Contributor Author

@qequ qequ Jul 19, 2022

Choose a reason for hiding this comment

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

tests for manifest checking sensitive vars fail with this change;

=== RUN   TestSensitiveVars
--- FAIL: TestSensitiveVars (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x90 pc=0x5093d0]

goroutine 6 [running]:
testing.tRunner.func1.2({0x5e2fc0, 0x789c30})
        /usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
        /usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0x5e2fc0, 0x789c30})
        /usr/local/go/src/runtime/panic.go:838 +0x207
regexp.(*Regexp).doExecute(0x0?, {0x0?, 0x0?}, {0x0?, 0xffffffffffffffff?, 0x0?}, {0xc000166db0?, 0xc0000506b0?}, 0x5e20c0?, 0x0, ...)
        /usr/local/go/src/regexp/exec.go:527 +0x90
regexp.(*Regexp).doMatch(...)
        /usr/local/go/src/regexp/exec.go:514
regexp.(*Regexp).MatchString(...)
        /usr/local/go/src/regexp/regexp.go:531
github.com/goodwithtech/dockle/pkg/assessor/manifest.sensitiveVars({0x61f612, 0x20})
        /home/alvaro/myDockle/pkg/assessor/manifest/manifest.go:245 +0x205
github.com/goodwithtech/dockle/pkg/assessor/manifest.TestSensitiveVars(0xc000121040)
        /home/alvaro/myDockle/pkg/assessor/manifest/manifest_test.go:417 +0x2fa
testing.tRunner(0xc000121040, 0x62b890)
        /usr/local/go/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
        /usr/local/go/src/testing/testing.go:1486 +0x35f
FAIL    github.com/goodwithtech/dockle/pkg/assessor/manifest    0.005s

but testing it manually works fine

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for your review. I fixed it. (1fd632b)

@tomoyamachi
Copy link
Collaborator

@arieltorti Thanks!
@qequ I committed some changes to your branch. Please have a look through it.

@tomoyamachi tomoyamachi merged commit b7b64e3 into goodwithtech:master Jul 23, 2022
@tomoyamachi
Copy link
Collaborator

@qequ Thank you for your contribution!

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