-
Notifications
You must be signed in to change notification settings - Fork 7
Add security rules for C++ and Go to prevent use-after-free and interface binding risks #130
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 9 commits into
coderabbitai:main
from
ESS-ENN:returncstr_and_avoid_bind_go
Jan 16, 2025
Merged
Changes from all commits
Commits
Show all changes
9 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 ae5dc84
return-c-str-cpp
ESS-ENN 45e72ac
avoid-bind-to-all-interfaces-go
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
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,27 @@ | ||
id: return-c-str-cpp | ||
language: cpp | ||
severity: warning | ||
message: >- | ||
"`$FUNC` returns a pointer to the memory owned by `$STR`. This pointer | ||
is invalid after `$STR` goes out of scope, which can trigger a use after | ||
free." | ||
note: >- | ||
[CWE-416] Use After Free | ||
[REFERENCES] | ||
- https://wiki.sei.cmu.edu/confluence/display/c/DCL30-C.+Declare+objects+with+appropriate+storage+durations | ||
- https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP54-CPP.+Do+not+access+an+object+outside+of+its+lifetime | ||
|
||
rule: | ||
kind: return_statement | ||
any: | ||
- pattern: return basic_string<$TYPE>($$$).$METHOD(); | ||
- pattern: return std::basic_string<$TYPE>($$$).$METHOD(); | ||
- pattern: return string($$$).$METHOD(); | ||
- pattern: return std::string($$$).$METHOD(); | ||
- pattern: return wstring($$$).$METHOD(); | ||
- pattern: return std::wstring($$$).$METHOD(); | ||
|
||
constraints: | ||
METHOD: | ||
regex: ^(c_str|data)$ | ||
|
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,30 @@ | ||
id: avoid-bind-to-all-interfaces-go | ||
language: go | ||
severity: warning | ||
message: >- | ||
"Detected a network listener listening on 0.0.0.0 or an empty string. | ||
This could unexpectedly expose the server publicly as it binds to all | ||
available interfaces. Instead, specify another IP address that is not | ||
0.0.0.0 nor the empty string." | ||
note: >- | ||
[CWE-200] Exposure of Sensitive Information to an Unauthorized Actor | ||
[REFERENCES] | ||
- https://owasp.org/Top10/A01_2021-Broken_Access_Control | ||
|
||
rule: | ||
not: | ||
has: | ||
stopBy: end | ||
kind: ERROR | ||
any: | ||
- pattern: tls.Listen($NETWORK, $IP $$$) | ||
- pattern: net.Listen($NETWORK, $IP $$$) | ||
|
||
constraints: | ||
IP: | ||
any: | ||
- kind: interpreted_string_literal | ||
regex: ^"0.0.0.0:.*"$|^":.*"$|^'0.0.0.0:.*'$|^':.*'$ | ||
- kind: raw_string_literal | ||
regex: ^`0.0.0.0:.*`$|^`:.*`$ | ||
|
16 changes: 16 additions & 0 deletions
16
tests/__snapshots__/avoid-bind-to-all-interfaces-go-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,16 @@ | ||
id: avoid-bind-to-all-interfaces-go | ||
snapshots: | ||
? | | ||
l, err := net.Listen("tcp", "0.0.0.0:2000") | ||
: labels: | ||
- source: net.Listen("tcp", "0.0.0.0:2000") | ||
style: primary | ||
start: 10 | ||
end: 43 | ||
? | | ||
l, err := net.Listen("tcp", ":2000") | ||
: labels: | ||
- source: net.Listen("tcp", ":2000") | ||
style: primary | ||
start: 10 | ||
end: 36 |
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: return-c-str-cpp | ||
snapshots: | ||
? | | ||
char *return_basic_string_directly() { | ||
return std::basic_string<char>("foo").c_str(); | ||
} | ||
: labels: | ||
- source: return std::basic_string<char>("foo").c_str(); | ||
style: primary | ||
start: 41 | ||
end: 87 | ||
? | | ||
char *return_data_directly() { | ||
return std::string("foo").data(); | ||
} | ||
: labels: | ||
- source: return std::string("foo").data(); | ||
style: primary | ||
start: 33 | ||
end: 66 | ||
? | | ||
char *return_directly() { | ||
return string("foo").c_str(); | ||
} | ||
: labels: | ||
- source: return string("foo").c_str(); | ||
style: primary | ||
start: 28 | ||
end: 57 |
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 |
---|---|---|
@@ -1,2 +1,13 @@ | ||
id: sizeof-this-cpp | ||
snapshots: {} | ||
snapshots: | ||
? | | ||
return sizeof(this); | ||
: labels: | ||
- source: sizeof(this) | ||
style: primary | ||
start: 7 | ||
end: 19 | ||
- source: this | ||
style: secondary | ||
start: 14 | ||
end: 18 |
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,24 @@ | ||
id: return-c-str-cpp | ||
valid: | ||
- | | ||
std::string return_directly() { | ||
// ok: return-c-str | ||
return std::string("foo"); | ||
} | ||
invalid: | ||
- | | ||
char *return_namespace_directly() { | ||
return std::string("foo").c_str(); | ||
} | ||
- | | ||
char *return_directly() { | ||
return string("foo").c_str(); | ||
} | ||
- | | ||
char *return_basic_string_directly() { | ||
return std::basic_string<char>("foo").c_str(); | ||
} | ||
- | | ||
char *return_data_directly() { | ||
return std::string("foo").data(); | ||
} |
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,9 @@ | ||
id: avoid-bind-to-all-interfaces-go | ||
valid: | ||
- | | ||
l, err := net.Listen("tcp", "192.168.1.101:2000") | ||
invalid: | ||
- | | ||
l, err := net.Listen("tcp", "0.0.0.0:2000") | ||
- | | ||
l, err := net.Listen("tcp", ":2000") |
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.