Skip to content

Commit

Permalink
Add static analysis rules for C++ and Rust security checks (#128)
Browse files Browse the repository at this point in the history
* removed missing-secure-java

* sizeof-this-cpp

* tokio-postgres-empty-password-rust

* tokio-postgres-hardcoded-password-rust

---------

Co-authored-by: Sakshis <[email protected]>
  • Loading branch information
ESS-ENN and Sakshis authored Jan 13, 2025
1 parent 36b59a3 commit 670f26f
Show file tree
Hide file tree
Showing 10 changed files with 761 additions and 1 deletion.
44 changes: 44 additions & 0 deletions rules/cpp/sizeof-this-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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
utils:
match_sizeof_this:
kind: sizeof_expression
has:
kind: parenthesized_expression
has:
kind: this
regex: "^this$"
inside:
stopBy: end
kind: return_statement
inside:
kind: compound_statement
follows:
kind: function_declarator
inside:
kind: function_definition

rule:
kind: sizeof_expression
all:
- has:
stopBy: end
kind: this
- not:
has:
stopBy: end
any:
- nthChild: 2
- kind: pointer_expression
- kind: ERROR
- kind: sizeof_expression


248 changes: 248 additions & 0 deletions rules/rust/security/tokio-postgres-empty-password-rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
id: tokio-postgres-empty-password-rust
language: rust
severity: warning
message: >-
The application uses an empty credential. This can lead to unauthorized
access by either an internal or external malicious actor. It is
recommended to rotate the secret and retrieve them from a secure secret
vault or Hardware Security Module (HSM), alternatively environment
variables can be used if allowed by your company policy.
note: >-
[CWE-287] Improper Authentication.
[REFERENCES]
- https://docs.rs/tokio-postgres/latest/tokio_postgres/
- https://owasp.org/Top10/A07_2021-Identification_and_Authentication_Failures
utils:
MATCH_FOLLOW_1:
follows:
stopBy: end
any:
- kind: let_declaration
all:
- has:
kind: identifier
pattern: $CONFIG
- has:
kind: call_expression
regex: ^tokio_postgres::Config::new\(\)$
- kind: let_declaration
all:
- has:
kind: identifier
pattern: $CONFIG
- has:
kind: call_expression
regex: ^Config::new\(\)$
any:
- follows:
stopBy: end
kind: use_declaration
has:
stopBy: end
kind: scoped_identifier
regex: ^tokio_postgres::Config$
- inside:
stopBy: end
follows:
stopBy: end
kind: use_declaration
has:
stopBy: end
kind: scoped_identifier
regex: ^tokio_postgres::Config$


rule:
kind: call_expression
not:
has:
stopBy: end
kind: ERROR
any:
# CONFIG IS DIRECT AND PWD IS DIRECT
- all:
- has:
stopBy: end
kind: scoped_identifier
regex: ^tokio_postgres::Config::new()$
- has:
kind: field_expression
regex: \.password$
nthChild: 1
- has:
kind: arguments
nthChild: 2
has:
stopBy: end
kind: string_literal
not:
has:
kind: string_content
nthChild: 1
all:
- not:
has:
stopBy: end
nthChild: 2
- not:
has:
stopBy: end
any:
- kind: block
- kind: array_expression
# CONFIG IS DIRECT AND PWD IS INSTANCE
- all:
- has:
stopBy: end
kind: scoped_identifier
regex: ^tokio_postgres::Config::new()$
- has:
kind: field_expression
regex: \.password$
nthChild: 1
- has:
kind: arguments
nthChild: 2
has:
stopBy: end
kind: identifier
pattern: $PASSWORD
inside:
stopBy: end
follows:
stopBy: end
any:
- kind: let_declaration
has:
kind: identifier
pattern: $PASSWORD
precedes:
stopBy: end
kind: string_literal
not:
has:
kind: string_content
- kind: expression_statement
has:
kind: assignment_expression
has:
kind: identifier
pattern: $PASSWORD
precedes:
stopBy: end
kind: string_literal
not:
has:
kind: string_content

nthChild: 1
all:
- not:
has:
stopBy: end
nthChild: 2
- not:
has:
stopBy: end
any:
- kind: block
- kind: array_expression
# CONFIG IS INSTANCE AND PWD IS DIRECT
- all:
- has:
stopBy: end
kind: identifier
pattern: $CONFIG
any:
- inside:
stopBy: end
matches: MATCH_FOLLOW_1
- has:
kind: field_expression
regex: \.password$
nthChild: 1
- has:
kind: arguments
nthChild: 2
has:
stopBy: end
kind: string_literal
not:
has:
kind: string_content
nthChild: 1
all:
- not:
has:
stopBy: end
nthChild: 2
- not:
has:
stopBy: end
any:
- kind: block
- kind: array_expression
# CONFIG IS INSTANCE AND PWD IS INSTANCE
- all:
- has:
stopBy: end
kind: identifier
pattern: $CONFIG
any:
- inside:
stopBy: end
matches: MATCH_FOLLOW_1
- has:
kind: field_expression
regex: \.password$
nthChild: 1
- has:
kind: arguments
nthChild: 2
has:
stopBy: end
kind: identifier
pattern: $PASSWORD
nthChild: 1
inside:
stopBy: end
follows:
stopBy: end
any:
- kind: let_declaration
all:
- has:
kind: identifier
pattern: $PASSWORD
- has:
kind: string_literal
not:
has:
kind: string_content
- kind: expression_statement
has:
kind: assignment_expression
all:
- has:
kind: identifier
pattern: $PASSWORD
- has:
kind: string_literal
not:
has:
kind: string_content

all:
- not:
has:
stopBy: end
nthChild: 2
- not:
has:
stopBy: end
any:
- kind: block
- kind: array_expression

Loading

0 comments on commit 670f26f

Please sign in to comment.