Skip to content

More Rules #8

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
merged 43 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
12e05e0
Merge branch 'ESS-ENN-ESS-ENN' into main
ESS-ENN Sep 17, 2024
bf998be
Merge remote-tracking branch 'upstream/main' into main
ESS-ENN Sep 17, 2024
640eba4
Made changes to scala rule
ESS-ENN Sep 17, 2024
57c1c83
python-cassandra-empty-password
ESS-ENN Sep 19, 2024
3c917d0
python-cassandra-empty-password-python rule id changed
ESS-ENN Sep 19, 2024
7ae95c5
changed rule of python-cassandra-empty-password-python
ESS-ENN Sep 19, 2024
5536878
hashids-with-django-secret-python
ESS-ENN Sep 19, 2024
1c372cd
documentbuilderfactory-external-parameter-entities-true-java
ESS-ENN Sep 19, 2024
6e2626e
Merge branch 'main' into main
ESS-ENN Sep 19, 2024
50919dc
python-elasticsearch-hardcoded-bearer-auth-python
ESS-ENN Sep 20, 2024
d3b8e8f
documentbuilderfactory-disallow-doctype-decl-false-java
ESS-ENN Sep 23, 2024
1b510d2
Valid test case added to python-elasticsearch-hardcoded-bearer-auth-p…
ESS-ENN Sep 23, 2024
88eb5a4
gcm-nonce-reuse-java
ESS-ENN Sep 23, 2024
c68bd1e
grpc-client-insecure-connection-go
ESS-ENN Sep 24, 2024
df25bc7
avoid-bind-to-all-interfaces-go
ESS-ENN Sep 24, 2024
3ed4514
Removed duplicates from rule pattern for avoid-bind-to-all-interfaces-go
ESS-ENN Sep 25, 2024
e4992b2
jwt-simple-noverify-js
ESS-ENN Sep 25, 2024
a8f9725
jwt-simple-noverify-ts
ESS-ENN Sep 25, 2024
6e1f903
detect-angular-sce-disabled-javascript
ESS-ENN Sep 25, 2024
c3aaeba
detect-angular-sce-disabled-typescript
ESS-ENN Sep 25, 2024
88bd8dc
jwt-go-none-algorithm-go
ESS-ENN Sep 25, 2024
fbed6dc
Fixed changes suggested by bot
ESS-ENN Sep 26, 2024
66f9735
more changes by bot
ESS-ENN Sep 26, 2024
d298eec
small-key-size-c
ESS-ENN Sep 26, 2024
6235236
simple-command-injection-direct-input-java
ESS-ENN Sep 26, 2024
e621b4e
jwt-none-alg-typescript
ESS-ENN Sep 26, 2024
a03697c
jwt-none-alg-javascript
ESS-ENN Sep 26, 2024
7587e49
small-key-size-c++
ESS-ENN Sep 27, 2024
a1d5bc5
jwt-python-hardcoded-secret-python
ESS-ENN Sep 27, 2024
d5609b3
Minor Corrections
ESS-ENN Sep 27, 2024
4db2725
use-of-weak-rsa-key-go
ESS-ENN Oct 1, 2024
a1c20ff
openssl-cbc-static-iv-php
ESS-ENN Oct 1, 2024
4666d0c
std-return-data-c
ESS-ENN Oct 3, 2024
f83aec6
node-sequelize-empty-password-argument-javascript
ESS-ENN Oct 3, 2024
6e710c5
node-sequelize-empty-password-argument-typescript
ESS-ENN Oct 3, 2024
52b258f
small-key-size-cpp
ESS-ENN Oct 4, 2024
e9a1690
sizeof-this-cpp
ESS-ENN Oct 4, 2024
b2c8b05
std-return-data-c changed
ESS-ENN Oct 4, 2024
78143e9
std-return-data-cpp
ESS-ENN Oct 4, 2024
381a23e
small-key-size-cpp id changed
ESS-ENN Oct 4, 2024
e00b5fa
node-sequelize-hardcoded-secret-argument-javascript
ESS-ENN Oct 4, 2024
93e4125
node-sequelize-hardcoded-secret-argument-typescript
ESS-ENN Oct 4, 2024
aeccebc
return-c-str-cpp
ESS-ENN Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions rules/c/security/small-key-size-c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
id: small-key-size-c
language: c
severity: warning
message: >-
$KEY_FUNCTION` is using a key size of only $KEY_BITS bits. This is
less than the recommended key size of 2048 bits.
note: >-
[CWE-326]: Inadequate Encryption Strength
[OWASP A02:2021]: Cryptographic Failures
[OWASP A03:2017]: Sensitive Data Exposure
[REFERENCES]
https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
https://owasp.org/Top10/A02_2021-Cryptographic_Failures
utils:
Match_pattern_with_prefix_statement:
kind: expression_statement
all:
- has:
stopBy: end
kind: call_expression
all:
- has:
stopBy: end
kind: identifier
pattern: $AST
- has:
stopBy: end
kind: argument_list
has:
stopby: end
kind: identifier
pattern: $Q
- follows:
stopBy: end
kind: declaration
has:
stopBy: end
kind: init_declarator
all:
- has:
stopBy: end
kind: identifier
pattern: $Q
- has:
stopBy: end
kind: number_literal
pattern: $AASS

rule:
kind: expression_statement
matches: Match_pattern_with_prefix_statement
constraints:
AST:
regex: (DH_generate_parameters_ex|DSA_generate_parameters_ex|EVP_PKEY_CTX_set_dh_paramgen_prime_len|EVP_PKEY_CTX_set_dsa_paramgen_bits|EVP_PKEY_CTX_set_rsa_keygen_bits|RSA_generate_key_ex|RSA_generate_key_fips)
AASS:
regex: '^(-?(0|[1-9][0-9]?|[1-9][0-9]{2}|1[0-9]{3}|20[0-3][0-9]|204[0-7])(\.[0-9]+)?|0|-[1-9][0-9]*|-[1-9][0-9]{2,}|-1[0-9]{3}|-20[0-3][0-9]|-204[0-7])$'
Comment on lines +52 to +56
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

AST constraint looks good, but AASS constraint needs improvement.

The AST constraint effectively covers various key generation functions, which is excellent for comprehensive checking. However, the AASS constraint, which is meant to match the key size, has some issues:

  1. The regex is overly complex, making it difficult to understand and maintain.
  2. It allows negative values, which are not appropriate for key sizes.
  3. The upper limit (2047) is hardcoded in the regex, making it inflexible if requirements change.

Consider simplifying and improving the AASS constraint:

constraints:
  AST:
    regex: (DH_generate_parameters_ex|DSA_generate_parameters_ex|EVP_PKEY_CTX_set_dh_paramgen_prime_len|EVP_PKEY_CTX_set_dsa_paramgen_bits|EVP_PKEY_CTX_set_rsa_keygen_bits|RSA_generate_key_ex|RSA_generate_key_fips)
  AASS:
    regex: '^([1-9][0-9]{0,3}|2047)$'
    transform: parseInt
    range: 
      min: 1
      max: 2047

This improved version:

  1. Simplifies the regex to match positive integers up to 2047.
  2. Uses transform: parseInt to convert the matched string to an integer.
  3. Adds a range check to ensure the value is between 1 and 2047.

This approach is more readable, maintainable, and flexible if the requirements change in the future.

109 changes: 109 additions & 0 deletions rules/c/security/std-return-data-c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
id: std-return-data-c
language: c
severity: warning
message: >-
$FUNC` returns a pointer to the memory owned by `$VAR`. This pointer
is invalid after `$VAR` 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
utils:
MATCH_RETURN_STATEMENT_WITH_STD:
kind: return_statement
all:
- has:
stopBy: end
kind: call_expression
has:
stopBy: end
kind: field_expression
has:
stopBy: end
kind: identifier
pattern: $R
- follows:
stopBy: end
kind: labeled_statement
all:
- has:
stopBy: end
kind: statement_identifier
regex: ^std
- has:
stopBy: end
kind: expression_statement
has:
stopBy: end
kind: binary_expression
all:
- has:
stopBy: end
kind: binary_expression
all:
- has:
stopBy: end
kind: identifier
regex: (vector|array|deque|forward_list|list|map|multimap|multiset|set|unordered_map|unordered_multimap|unordered_multiset|unordered_set)
- has:
stopBy: end
kind: identifier
- has:
stopBy: end
kind: identifier
pattern: $R
inside:
stopBy: end
kind: function_definition
has:
stopBy: end
kind: primitive_type

MATCH_RETURN_STATEMENT_WITHOUT_STD:
kind: return_statement
all:
- has:
stopBy: end
kind: call_expression
has:
stopBy: end
kind: field_expression
has:
stopBy: end
kind: identifier
pattern: $R
- follows:
stopBy: end
kind: expression_statement
has:
stopBy: end
kind: binary_expression
all:
- has:
stopBy: end
kind: binary_expression
all:
- has:
stopBy: end
kind: identifier
regex: (vector|array|deque|forward_list|list|map|multimap|multiset|set|unordered_map|unordered_multimap|unordered_multiset|unordered_set)
- has:
stopBy: end
kind: identifier
- has:
stopBy: end
kind: identifier
pattern: $R
inside:
stopBy: end
kind: function_definition
has:
stopBy: end
kind: primitive_type

rule:
kind: return_statement
any:
- matches: MATCH_RETURN_STATEMENT_WITH_STD
- matches: MATCH_RETURN_STATEMENT_WITHOUT_STD
109 changes: 109 additions & 0 deletions rules/cpp/security/return-c-str-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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

utils:
util_for_declaration_inside_function:
kind: return_statement
pattern: return $STR.$METHOD();
follows:
kind: declaration
stopBy: end
any:
- pattern: string $STR;
- pattern: wstring $STR;
- pattern: basic_string $STR;
- pattern: std::string $STR;
- pattern: std::wstring $STR;
- pattern: std::basic_string<$TYPE> $STR;

util_for_assignment_inside_function:
kind: return_statement
pattern: return $STR.$METHOD();
follows:
kind: declaration
stopBy: end
any:
- pattern: string $STR = string($STRING);
- pattern: wstring $STR = wstring($STRING);
- pattern: basic_string<$TYPE> $STR = basic_string<$TYPE>($STRING);
- pattern: std::string $STR = std::string($STRING);
- pattern: std::wstring $STR = std::wstring($STRING);
- pattern: std::basic_string<$TYPE> $STR = std::basic_string<$TYPE>($STRING);

util_for_func_params:
kind: return_statement
pattern: return $STR.$METHOD();
inside:
stopBy: end
kind: function_definition
has:
stopBy: end
kind: parameter_list
has:
stopBy: end
kind: parameter_declaration
has:
stopBy: end
kind: identifier
field: declarator
pattern: $STR
any:
- has:
any:
- kind: type_identifier
pattern: $IDENTIFIFER
- kind: qualified_identifier
any:
- all:
- has:
kind: namespace_identifier
pattern: $NAMESPACE_IDEN
- has:
kind: template_type
all:
- has:
kind: type_identifier
field: name
pattern: $BASIC_STR
precedes:
kind: template_argument_list
- pattern: $IDENTIFIFER
- kind: template_type
has:
kind: type_identifier
field: name
pattern: $BASIC_STR
precedes:
kind: template_argument_list

rule:
any:
- matches: util_for_declaration_inside_function
- matches: util_for_assignment_inside_function
- matches: util_for_func_params
- 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)$
IDENTIFIFER:
regex: ^(string|wstring|std::string|std::wstring)$
BASIC_STR:
regex: ^(basic_string)$
NAMESPACE_IDEN:
regex: ^(std)$
Comment on lines +101 to +109
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo and consider regex optimization

The constraints effectively limit the rule to specific methods and types. However, there are two issues to address:

  1. There's a typo in the constraint name "IDENTIFIFER". It should be "IDENTIFIER".
  2. The regex patterns for IDENTIFIFER and BASIC_STR could be combined for efficiency.

Please apply the following changes:

-  IDENTIFIFER:
+  IDENTIFIER:
-    regex: ^(string|wstring|std::string|std::wstring)$
+    regex: ^(((std::)?(string|wstring))|(basic_string))$
-  BASIC_STR:
-    regex: ^(basic_string)$

This change corrects the typo and combines the IDENTIFIER and BASIC_STR patterns into a single, more efficient regex.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constraints:
METHOD:
regex: ^(c_str|data)$
IDENTIFIFER:
regex: ^(string|wstring|std::string|std::wstring)$
BASIC_STR:
regex: ^(basic_string)$
NAMESPACE_IDEN:
regex: ^(std)$
constraints:
METHOD:
regex: ^(c_str|data)$
IDENTIFIER:
regex: ^(((std::)?(string|wstring))|(basic_string))$
NAMESPACE_IDEN:
regex: ^(std)$

Comment on lines +101 to +109
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

LGTM with a minor typo: Effective constraints definition

The constraints section effectively limits the rule to relevant methods (c_str and data) and string types, ensuring that the rule targets the intended scenarios.

There's a typo in the "IDENTIFIFER" constraint name. It should be "IDENTIFIER". Please apply the following fix:

-  IDENTIFIFER:
+  IDENTIFIER:
    regex: ^(string|wstring|std::string|std::wstring)$

Also, update any references to this constraint in the utils section accordingly.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constraints:
METHOD:
regex: ^(c_str|data)$
IDENTIFIFER:
regex: ^(string|wstring|std::string|std::wstring)$
BASIC_STR:
regex: ^(basic_string)$
NAMESPACE_IDEN:
regex: ^(std)$
constraints:
METHOD:
regex: ^(c_str|data)$
IDENTIFIER:
regex: ^(string|wstring|std::string|std::wstring)$
BASIC_STR:
regex: ^(basic_string)$
NAMESPACE_IDEN:
regex: ^(std)$

Comment on lines +101 to +109
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

LGTM with minor correction: Effective constraints with a typo

The constraints effectively limit the rule to specific methods and types, which should help prevent false positives. The regular expressions used are precise and well-defined.

There's a minor typo in the constraint name "IDENTIFIFER". Please correct it to "IDENTIFIER":

-  IDENTIFIFER:
+  IDENTIFIER:
    regex: ^(string|wstring|std::string|std::wstring)$

Also, ensure that all references to this constraint throughout the file are updated accordingly.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constraints:
METHOD:
regex: ^(c_str|data)$
IDENTIFIFER:
regex: ^(string|wstring|std::string|std::wstring)$
BASIC_STR:
regex: ^(basic_string)$
NAMESPACE_IDEN:
regex: ^(std)$
constraints:
METHOD:
regex: ^(c_str|data)$
IDENTIFIER:
regex: ^(string|wstring|std::string|std::wstring)$
BASIC_STR:
regex: ^(basic_string)$
NAMESPACE_IDEN:
regex: ^(std)$

13 changes: 13 additions & 0 deletions rules/cpp/security/sizeof-this-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
id: sizeof-this-cpp
language: cpp
severity: warning
message: >-
Do not use `sizeof(this)` to get the number of bytes of the object in
memory. It returns the size of the pointer, not the size of the object.
note: >-
[CWE-467]: Use of sizeof() on a Pointer Type
[REFERENCES]
- https://wiki.sei.cmu.edu/confluence/display/c/ARR01-C.+Do+not+apply+the+sizeof+operator+to+a+pointer+when+taking+the+size+of+an+array
rule:
any:
- pattern: "sizeof(this)"
Comment on lines +11 to +13
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance pattern matching for better coverage and accuracy.

While the current pattern will catch the basic case, it may miss variations and could potentially flag false positives. Consider enhancing the pattern for better coverage and accuracy.

Here's a suggested improvement:

 rule:
   any:
-    - pattern: "sizeof(this)"
+    - pattern: 'sizeof\s*\(\s*this\s*\)'
+      inside:
+        - function
+        - method
+      not-inside:
+        - comment
+        - string

This pattern:

  1. Allows for spaces between sizeof, parentheses, and this.
  2. Ensures the pattern is inside a function or method.
  3. Excludes matches inside comments and strings to avoid false positives.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
rule:
any:
- pattern: "sizeof(this)"
rule:
any:
- pattern: 'sizeof\s*\(\s*this\s*\)'
inside:
- function
- method
not-inside:
- comment
- string

Comment on lines +11 to +13
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider making the pattern more robust.

The current pattern will correctly identify the problematic sizeof(this) usage. However, it might miss variations with different spacing.

Consider updating the pattern to account for potential spaces:

  rule:
    any:
-     - pattern: "sizeof(this)"
+     - pattern: 'sizeof\s*\(\s*this\s*\)'

This pattern will match sizeof(this), sizeof (this), sizeof( this ), and other variations with different spacing.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
rule:
any:
- pattern: "sizeof(this)"
rule:
any:
- pattern: 'sizeof\s*\(\s*this\s*\)'

Comment on lines +11 to +13
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider expanding the pattern matching.

While the current pattern "sizeof(this)" will catch the basic problematic usage, it might be too simplistic. Consider the following suggestions:

  1. Expand the pattern to catch variations, such as spaces between 'sizeof' and the parentheses.
  2. Consider potential false positives, such as comments or string literals containing "sizeof(this)".

Here's a suggested improvement to make the rule more robust:

rule:
  pattern-either:
    - pattern: "sizeof(this)"
    - pattern: "sizeof (this)"
  inside:
    pattern-not: 
      - pattern-inside: "// ..."
      - pattern-inside: "/* ... */"
      - pattern-inside: "\"...\""

This modification will catch variations with spaces and exclude matches within comments and string literals.

56 changes: 56 additions & 0 deletions rules/cpp/security/small-key-size-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
id: small-key-size-cpp
language: cpp
severity: warning
message: >-
$KEY_FUNCTION` is using a key size of only $KEY_BITS bits. This is
less than the recommended key size of 2048 bits.
note: >-
[CWE-326]: Inadequate Encryption Strength
[OWASP A02:2021]: Cryptographic Failures
[OWASP A03:2017]: Sensitive Data Exposure
[REFERENCES]
https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf
https://owasp.org/Top10/A02_2021-Cryptographic_Failures
utils:
Match_pattern_with_prefix_statement:
kind: expression_statement
all:
- has:
stopBy: end
kind: call_expression
all:
- has:
stopBy: end
kind: identifier
pattern: $AST
- has:
stopBy: end
kind: argument_list
has:
stopby: end
kind: identifier
pattern: $Q
- follows:
stopBy: end
kind: declaration
has:
stopBy: end
kind: init_declarator
all:
- has:
stopBy: end
kind: identifier
pattern: $Q
- has:
stopBy: end
kind: number_literal
pattern: $AASS

rule:
kind: expression_statement
matches: Match_pattern_with_prefix_statement
constraints:
AST:
regex: (DH_generate_parameters_ex|DSA_generate_parameters_ex|EVP_PKEY_CTX_set_dh_paramgen_prime_len|EVP_PKEY_CTX_set_dsa_paramgen_bits|EVP_PKEY_CTX_set_rsa_keygen_bits|RSA_generate_key_ex|RSA_generate_key_fips)
AASS:
regex: '^(-?(0|[1-9][0-9]?|[1-9][0-9]{2}|1[0-9]{3}|20[0-3][0-9]|204[0-7])(\.[0-9]+)?|0|-[1-9][0-9]*|-[1-9][0-9]{2,}|-1[0-9]{3}|-20[0-3][0-9]|-204[0-7])$'
Loading