Skip to content

Add Ruby security rules for RSA key size, AWS SDK and Faraday secrets #159

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 21 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
91 changes: 91 additions & 0 deletions rules/ruby/security/insufficient-rsa-key-size-ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
id: insufficient-rsa-key-size-ruby
language: ruby
severity: warning
message: >-
The RSA key size $SIZE is insufficent by NIST standards. It is
recommended to use a key length of 2048 or higher.
note: >-
[CWE-326] Inadequate Encryption Strength.
[REFERENCES]
- https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57Pt3r1.pdf

ast-grep-essentials: true

utils:
OpenSSL::PKey::RSA.generate($SIZE,...):
# OpenSSL::PKey::RSA.generate($SIZE,...)
kind: call
all:
- has:
stopBy: neighbor
kind: scope_resolution
regex: ^OpenSSL::PKey::RSA$
- has:
stopBy: neighbor
regex: ^.$
- has:
stopBy: neighbor
kind: identifier
regex: ^(new|generate)$
- has:
stopBy: neighbor
kind: argument_list
has:
pattern: $KEYS
any:
- 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])$'
- regex: ^-\d+(\.\d+)?(/(\d+(\.\d+)?))?$
nthChild:
position: 1
ofRule:
not:
kind: comment

OpenSSL::PKey::RSA.new($ASSIGN, ...):
# $ASSIGN = $SIZE
# OpenSSL::PKey::RSA.new($ASSIGN, ...)
kind: call
all:
- has:
stopBy: neighbor
kind: scope_resolution
regex: ^OpenSSL::PKey::RSA$
- has:
stopBy: neighbor
regex: ^.$
- has:
stopBy: neighbor
kind: identifier
regex: ^(new|generate)$
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
pattern: $BIT
nthChild:
position: 1
ofRule:
not:
kind: comment
- any:
- inside:
stopBy: end
follows:
stopBy: end
kind: assignment
pattern: $BIT = $KEY
- follows:
stopBy: end
kind: assignment
pattern: $BIT = $KEY
rule:
kind: call
any:
- matches: OpenSSL::PKey::RSA.generate($SIZE,...)
- matches: OpenSSL::PKey::RSA.new($ASSIGN, ...)
constraints:
KEY:
any:
- 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])$'
- regex: ^-\d+(\.\d+)?(/(\d+(\.\d+)?))?$
149 changes: 149 additions & 0 deletions rules/ruby/security/ruby-aws-sdk-hardcoded-secret-ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
id: ruby-aws-sdk-hardcoded-secret-ruby
language: ruby
severity: warning
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:
Aws::Credentials.new($X, "...", ...):
# Aws::Credentials.new($X, "...", ...)
kind: call
all:
- has:
stopBy: neighbor
kind: scope_resolution
regex: ^Aws::Credentials$
- has:
stopBy: neighbor
regex: ^.$
- has:
stopBy: neighbor
kind: identifier
regex: ^new$
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: string
nthChild:
position: 2
ofRule:
not:
kind: comment
any:
- has:
nthChild: 1
not:
kind: pair
has:
nthChild: 1
kind: hash_key_symbol

- any:
- inside:
stopBy: end
follows:
stopBy: end
kind: call
pattern: require 'aws-sdk-core'
- follows:
stopBy: end
kind: call
pattern: require 'aws-sdk-core'
Aws::Credentials.new($X, "...", ...)_instance:
# Aws::Credentials.new($X, $VAR, ...)
kind: call
all:
- has:
stopBy: neighbor
kind: scope_resolution
regex: ^Aws::Credentials$
- has:
stopBy: neighbor
regex: ^.$
- has:
stopBy: neighbor
kind: identifier
regex: ^new$
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: identifier
pattern: $VAR
nthChild:
position: 2
ofRule:
not:
kind: comment
any:
- has:
nthChild: 1
not:
kind: pair
has:
nthChild: 1
kind: hash_key_symbol
- any:
- follows:
stopBy: end
kind: assignment
all:
- has:
kind: identifier
pattern: $VAR
- has:
kind: string
has:
kind: string_content
- inside:
stopBy: end
follows:
stopBy: end
kind: assignment
all:
- has:
kind: identifier
pattern: $VAR
- has:
kind: string
has:
kind: string_content
- any:
- inside:
stopBy: end
follows:
stopBy: end
kind: call
pattern: require 'aws-sdk-core'
- follows:
stopBy: end
kind: call
pattern: require 'aws-sdk-core'

rule:
kind: call
any:
- matches: Aws::Credentials.new($X, "...", ...)
- matches: Aws::Credentials.new($X, "...", ...)_instance
all:
- not:
has:
stopBy: end
kind: ERROR
- not:
inside:
stopBy: end
kind: ERROR
Loading