-
Notifications
You must be signed in to change notification settings - Fork 6
Add security rules for detecting hard-coded secrets in Java and Python #141
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
Merged
ganeshpatro321
merged 15 commits into
coderabbitai:main
from
ESS-ENN:python-urllib3_drivermanager-java_hardcoded
Jan 29, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bb7b8e1
removed missing-secure-java
308e947
Merge branch 'coderabbitai:main' into main
ESS-ENN 46efd55
Merge branch 'coderabbitai:main' into main
ESS-ENN 3ac0204
Merge branch 'coderabbitai:main' into main
ESS-ENN 7caba92
Merge branch 'coderabbitai:main' into main
ESS-ENN d3a2776
Merge branch 'coderabbitai:main' into main
ESS-ENN 2e86380
Merge branch 'coderabbitai:main' into main
ESS-ENN b072db7
Merge branch 'coderabbitai:main' into main
ESS-ENN 65dce29
Merge branch 'coderabbitai:main' into main
ESS-ENN 12bb3aa
httponly-false-csharp
ESS-ENN 2c5ea88
use-of-md5-digest-utils-java
ESS-ENN d3067f1
removing use-of-md5-digest-utils and httponly-false-csharp
ESS-ENN 56d4011
Merge branch 'coderabbitai:main' into main
ESS-ENN 1aebdd8
python-urllib3-hardcoded-secret-python
ESS-ENN f948d87
drivermanager-hardcoded-secret-java
ESS-ENN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
rules/java/security/drivermanager-hardcoded-secret-java.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
id: drivermanager-hardcoded-secret-java | ||
severity: warning | ||
language: java | ||
message: >- | ||
A secret is hard-coded in the application. Secrets stored in source | ||
code, such as credentials, identifiers, and other types of sensitive data, | ||
can be leaked and used by internal or external malicious actors. Use | ||
environment variables to securely provide credentials and other secrets or | ||
retrieve them from a secure vault or Hardware Security Module (HSM). | ||
note: >- | ||
[CWE-798] Use of Hard-coded Credentials. | ||
[REFERENCES] | ||
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html | ||
|
||
ast-grep-essentials: true | ||
|
||
utils: | ||
MATCH_PATTERN_DriverManager.getConnection: | ||
kind: method_invocation | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: identifier | ||
regex: '^DriverManager$' | ||
- has: | ||
stopBy: neighbor | ||
kind: identifier | ||
regex: '^getConnection$' | ||
- has: | ||
kind: argument_list | ||
# nthChild: 3 | ||
all: | ||
- any: | ||
- has: | ||
stopBy: end | ||
kind: string_literal | ||
nthChild: 3 | ||
pattern: $I | ||
has: | ||
stopBy: neighbor | ||
kind: string_fragment | ||
- has: | ||
stopBy: end | ||
kind: parenthesized_expression | ||
has: | ||
stopBy: end | ||
kind: string_fragment | ||
pattern: $I | ||
- has: | ||
nthChild: 3 | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: string_fragment | ||
inside: | ||
stopBy: neighbor | ||
kind: string_literal | ||
- not: | ||
has: | ||
stopBy: end | ||
kind: string_literal | ||
not: | ||
has: | ||
stopBy: neighbor | ||
kind: string_fragment | ||
- not: | ||
has: | ||
stopBy: end | ||
regex: ^-$ | ||
- not: | ||
has: | ||
nthChild: 4 | ||
- not: | ||
has: | ||
stopBy: end | ||
kind: ERROR | ||
- not: | ||
has: | ||
stopBy: end | ||
kind: binary_expression | ||
|
||
MATCH_PATTERN_DriverManagerDataSource: | ||
kind: object_creation_expression | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: type_identifier | ||
regex: '^DriverManagerDataSource$' | ||
- has: | ||
kind: argument_list | ||
# nthChild: 3 | ||
all: | ||
- any: | ||
- has: | ||
stopBy: neighbor | ||
kind: string_literal | ||
nthChild: 3 | ||
pattern: $I | ||
has: | ||
stopBy: neighbor | ||
kind: string_fragment | ||
- has: | ||
stopBy: end | ||
kind: parenthesized_expression | ||
has: | ||
stopBy: end | ||
kind: string_fragment | ||
pattern: $I | ||
- has: | ||
nthChild: 3 | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: string_fragment | ||
inside: | ||
stopBy: neighbor | ||
kind: string_literal | ||
- not: | ||
has: | ||
stopBy: end | ||
kind: string_literal | ||
not: | ||
has: | ||
stopBy: neighbor | ||
kind: string_fragment | ||
- not: | ||
has: | ||
stopBy: end | ||
regex: ^-$ | ||
- not: | ||
has: | ||
nthChild: 4 | ||
- not: | ||
has: | ||
stopBy: end | ||
kind: binary_expression | ||
- not: | ||
has: | ||
stopBy: end | ||
kind: ERROR | ||
|
||
rule: | ||
any: | ||
- kind: method_invocation | ||
matches: MATCH_PATTERN_DriverManager.getConnection | ||
- kind: object_creation_expression | ||
matches: MATCH_PATTERN_DriverManagerDataSource | ||
|
||
constraints: | ||
I: | ||
not: | ||
regex: ^""$ | ||
|
59 changes: 59 additions & 0 deletions
59
rules/python/security/python-urllib3-hardcoded-secret-python.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
id: python-urllib3-hardcoded-secret-python | ||
severity: warning | ||
language: python | ||
message: >- | ||
A secret is hard-coded in the application. Secrets stored in source | ||
code, such as credentials, identifiers, and other types of sensitive data, | ||
can be leaked and used by internal or external malicious actors. Use | ||
environment variables to securely provide credentials and other secrets or | ||
retrieve them from a secure vault or Hardware Security Module (HSM). | ||
note: >- | ||
[CWE-798] Use of Hard-coded Credentials. | ||
[REFERENCES] | ||
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html | ||
|
||
ast-grep-essentials: true | ||
|
||
utils: | ||
urllib3.util.make_headers(...,basic_auth="...",...): | ||
# urllib3.util.make_headers(...,basic_auth="...",...) | ||
kind: call | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: attribute | ||
regex: '^urllib3.util.make_headers$|^urllib3.make_headers$|^requests.packages.urllib3.make_headers$|^requests.packages.urllib3.util.make_headers$' | ||
- has: | ||
stopBy: neighbor | ||
kind: argument_list | ||
has: | ||
stopBy: neighbor | ||
kind: keyword_argument | ||
all: | ||
- has: | ||
stopBy: neighbor | ||
kind: identifier | ||
regex: '^basic_auth$|^proxy_basic_auth$' | ||
- has: | ||
stopBy: neighbor | ||
kind: string | ||
any: | ||
- has: | ||
stopBy: neighbor | ||
kind: string_content | ||
- has: | ||
stopBy: neighbor | ||
regex: '.*' | ||
ESS-ENN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
rule: | ||
kind: call | ||
matches: urllib3.util.make_headers(...,basic_auth="...",...) | ||
not: | ||
all: | ||
- has: | ||
stopBy: end | ||
kind: ERROR | ||
- inside: | ||
stopBy: end | ||
kind: ERROR | ||
|
29 changes: 29 additions & 0 deletions
29
tests/__snapshots__/drivermanager-hardcoded-secret-java-snapshot.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
id: drivermanager-hardcoded-secret-java | ||
snapshots: | ||
? | | ||
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:o92", "a", "password"); | ||
: labels: | ||
- source: DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:o92", "a", "password") | ||
style: primary | ||
start: 17 | ||
end: 101 | ||
- source: DriverManager | ||
style: secondary | ||
start: 17 | ||
end: 30 | ||
- source: getConnection | ||
style: secondary | ||
start: 31 | ||
end: 44 | ||
- source: password | ||
style: secondary | ||
start: 91 | ||
end: 99 | ||
- source: '"password"' | ||
style: secondary | ||
start: 90 | ||
end: 100 | ||
- source: ("jdbc:oracle:thin:@localhost:1521:o92", "a", "password") | ||
style: secondary | ||
start: 44 | ||
end: 101 |
32 changes: 32 additions & 0 deletions
32
tests/__snapshots__/python-urllib3-hardcoded-secret-python-snapshot.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
id: python-urllib3-hardcoded-secret-python | ||
snapshots: | ||
urllib3.util.make_headers(basic_auth="user:123"): | ||
labels: | ||
- source: urllib3.util.make_headers(basic_auth="user:123") | ||
style: primary | ||
start: 0 | ||
end: 48 | ||
- source: urllib3.util.make_headers | ||
style: secondary | ||
start: 0 | ||
end: 25 | ||
- source: basic_auth | ||
style: secondary | ||
start: 26 | ||
end: 36 | ||
- source: user:123 | ||
style: secondary | ||
start: 38 | ||
end: 46 | ||
- source: '"user:123"' | ||
style: secondary | ||
start: 37 | ||
end: 47 | ||
- source: basic_auth="user:123" | ||
style: secondary | ||
start: 26 | ||
end: 47 | ||
- source: (basic_auth="user:123") | ||
style: secondary | ||
start: 25 | ||
end: 48 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
id: drivermanager-hardcoded-secret-java | ||
valid: | ||
- | | ||
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:o92","a"); | ||
ESS-ENN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
invalid: | ||
- | | ||
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:o92", "a", "password"); | ||
ESS-ENN marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
id: python-urllib3-hardcoded-secret-python | ||
valid: | ||
- | | ||
urllib3.util.make_headers(basic_auth=os.env['auth']) | ||
invalid: | ||
- | | ||
urllib3.util.make_headers(basic_auth="user:123") | ||
ESS-ENN marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.