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

add bitbucket app password scanner #1498

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

Conversation

brandonjyan
Copy link
Contributor

No description provided.

@brandonjyan brandonjyan requested a review from a team as a code owner July 14, 2023 22:51
Copy link
Collaborator

@ahrav ahrav left a comment

Choose a reason for hiding this comment

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

Thanks again for the contribution. Left a handful of tiny comments, they are completely optional.


// Make sure that your group is surrounded in boundary characters such as below to reduce false positives.
/*
The following patterns cover the methods of authentication found here: https://support.atlassian.com/bitbucket-cloud/docs/using-app-passwords/, as well as for other general cases.
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: We can just use regular comments here. /* */ are generally used for package comments given how lengthy they are.

ex:

// The following patterns cover the methods of authentication found here:
// https://support.atlassian.com/bitbucket-cloud/docs/using-app-passwords/, as well as for other general cases.

comment tips

*/

// Covers 'username:appPassword' pattern
usernamePat1 = regexp.MustCompile(`\b([A-Za-z0-9-_]{1,30}):(?:ATBB[A-Za-z0-9_=\.-]+[A-Z0-9]{8})\b`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: We can remove the extra capture group around ?:ATBB... and we can also remove the redundant \ within [A-Za-z0-9_=\.-].
ex:
\b([A-Za-z0-9-_]{1,30}):ATBB[A-Za-z0-9_=.-]+[A-Z0-9]{8}\b

// Covers 'username:appPassword' pattern
usernamePat1 = regexp.MustCompile(`\b([A-Za-z0-9-_]{1,30}):(?:ATBB[A-Za-z0-9_=\.-]+[A-Z0-9]{8})\b`)
// Covers assignment of username to variable
usernamePat2 = regexp.MustCompile(`(?im)(?:user|usr)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([a-zA-Z0-9-_]{1,30})\b`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: {0,1} -> ?
ex:
(?im)(?:user|usr)\S{0,40}?[:=\s]{1,3}[ '"=]?([a-zA-Z0-9-_]{1,30})\b

// Covers 'https://username@bitbucket.org' pattern
usernamePat3 = regexp.MustCompile(`https:\/\/([a-zA-Z0-9-_]{1,30})@bitbucket.org`)
// Covers '("username", "password")' pattern, used for HTTP Basic Auth
usernamePat4 = regexp.MustCompile(`"([a-zA-Z0-9-_]{1,30})",(?: )?"(?:ATBB[A-Za-z0-9_=\.-]+[A-Z0-9]{8})"`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Similar to above, we can remove the redundant capture groups and the \.
ex:
"([a-zA-Z0-9-_]{1,30})", ?"ATBB[A-Za-z0-9_=.-]+[A-Z0-9]{8}"

// Covers assignment of username to variable
usernamePat2 = regexp.MustCompile(`(?im)(?:user|usr)\S{0,40}?[:=\s]{1,3}[ '"=]{0,1}([a-zA-Z0-9-_]{1,30})\b`)
// Covers 'https://username@bitbucket.org' pattern
usernamePat3 = regexp.MustCompile(`https:\/\/([a-zA-Z0-9-_]{1,30})@bitbucket.org`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: We don't need to escape /
ex:
https://([a-zA-Z0-9-_]{1,30})@bitbucket.org

// Covers '("username", "password")' pattern, used for HTTP Basic Auth
usernamePat4 = regexp.MustCompile(`"([a-zA-Z0-9-_]{1,30})",(?: )?"(?:ATBB[A-Za-z0-9_=\.-]+[A-Z0-9]{8})"`)

appPasswordPat = regexp.MustCompile(`\bATBB[A-Za-z0-9_=\.-]+[A-Z0-9]{8}\b`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: ditto regarding the redundant \.
ex:
\bATBB[A-Za-z0-9_=.-]+[A-Z0-9]{8}\b

func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) {
dataStr := string(data)

usernameMatches := append(usernamePat1.FindAllStringSubmatch(dataStr, -1), usernamePat2.FindAllStringSubmatch(dataStr, -1)...)
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: If we combine all the username patterns into a single slice and declare the var above during the package init we can simplify some of the logic here.

ex:

var (
...
usernamePat1 = ..
...
usernamePatterns = []*regexp.Regexp{usernamePat1, usernamePat2, usernamePat3, usernamePat4}
|
...

func (s Scanner) FromData(...) {
...
var usernameMatches [][]string
for _, pattern := range usernamePatterns {
	usernameMatches = append(usernameMatches, pattern.FindAllStringSubmatch(dataStr, -1)...)
}
...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, much cleaner!

@brandonjyan brandonjyan requested a review from ahrav July 21, 2023 07:12
@nyanshak
Copy link
Contributor

Anyone in Truffle Security group who could review / fix conflicts? I'd love to get this added to Trufflehog.

@zricethezav
Copy link
Collaborator

@brandonjyan happy to get this merged in once conflicts have been resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants