Skip to content

Two Java rules 10Oct2024 #16

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 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 53 additions & 0 deletions rules/java/security/cookie-missing-secure-flag-java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
id: cookie-missing-secure-flag-java
language: java
severity: warning
message: >-
A cookie was detected without setting the 'secure' flag. The 'secure'
flag for cookies prevents the client from transmitting the cookie over
insecure channels such as HTTP. Set the 'secure' flag by calling
'$COOKIE.setSecure(true);'.
note: >-
[CWE-614] Sensitive Cookie in HTTPS Session Without 'Secure' Attribute.
[REFERENCES]
- https://owasp.org/www-community/controls/SecureCookieAttribute
utils:
MATCH_RESPONSE_COOKIE_STATEMENT:
kind: expression_statement
all:
- has:
stopBy: neighbor
kind: method_invocation
all:
- has:
stopBy: neighbor
kind: identifier
regex: "response"
- has:
stopBy: neighbor
kind: identifier
regex: "addCookie"
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: identifier
- not:
follows:
stopBy: end
kind: expression_statement
all:
- has:
stopBy: end
kind: identifier
- has:
stopBy: end
kind: identifier
regex: "setSecure|setValue"
- has:
stopBy: end
kind: argument_list

rule:
kind: expression_statement
matches: MATCH_RESPONSE_COOKIE_STATEMENT
248 changes: 248 additions & 0 deletions rules/java/security/jedis-jedisfactory-hardcoded-password-java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
id: jedis-jedisfactory-hardcoded-password-java
language: java
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
utils:
MATCH_PATTERN_JEDISFACTORY:
kind: expression_statement
all:
- has:
stopBy: neighbor
kind: method_invocation
all:
- has:
stopBy: neighbor
kind: identifier
pattern: $R
- has:
stopBy: neighbor
kind: identifier
regex: "^setPassword$"
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: string_literal
- follows:
stopBy: end
kind: local_variable_declaration
all:
- has:
stopBy: neighbor
kind: type_identifier
regex: "^JedisFactory$|^jedis.ConnectionFactory$"
- has:
stopBy: neighbor
kind: variable_declarator
all:
- has:
stopBy: neighbor
kind: identifier
pattern: $R
- has:
stopBy: neighbor
kind: object_creation_expression
- inside:
stopBy: end
kind: class_declaration
follows:
stopBy: end
kind: import_declaration
has:
stopBy: neighbor
kind: scoped_identifier
all:
- has:
stopBy: end
kind: identifier
regex: "^redis$"
- has:
stopBy: end
kind: identifier
regex: "^clients$"

MATCH_PATTERN_CLIENT_JEDIS.JEDISFACTORY:
kind: expression_statement
all:
- has:
stopBy: neighbor
kind: method_invocation
all:
- has:
stopBy: neighbor
kind: identifier
pattern: $R
- has:
stopBy: neighbor
kind: identifier
regex: "^setPassword$"
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: string_literal
- follows:
stopBy: end
kind: local_variable_declaration
all:
- has:
stopBy: neighbor
kind: scoped_type_identifier
all:
- has:
stopBy: neighbor
kind: scoped_type_identifier
all:
- has:
stopBy: neighbor
kind: type_identifier
regex: "^clients$"
- has:
stopBy: neighbor
kind: type_identifier
regex: "^jedis$"
- has:
stopBy: neighbor
kind: type_identifier
regex: "^JedisFactory$|^ConnectionFactory$"
- has:
stopBy: end
kind: variable_declarator
has:
stopBy: neighbor
kind: identifier
pattern: $R
- inside:
stopBy: end
kind: class_declaration
follows:
stopBy: end
kind: import_declaration
all:
- has:
stopBy: end
kind: identifier
regex: "^redis$"
- has:
stopBy: end
kind: asterisk

MATCH_PATTERN_JEDIS.JEDISFACTORY:
kind: expression_statement
all:
- has:
stopBy: neighbor
kind: method_invocation
all:
- has:
stopBy: neighbor
kind: identifier
pattern: $R
- has:
stopBy: neighbor
kind: identifier
regex: "^setPassword$"
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: string_literal
- follows:
stopBy: end
kind: local_variable_declaration
all:
- has:
stopBy: neighbor
kind: scoped_type_identifier
all:
- has:
stopBy: neighbor
kind: type_identifier
regex: "^jedis$"
- has:
stopBy: neighbor
kind: type_identifier
regex: "^JedisFactory$|^ConnectionFactory$"
- has:
stopBy: neighbor
kind: variable_declarator
has:
stopBy: neighbor
kind: identifier
pattern: $R

MATCH_PATTERN_REDIS_CLIENT_JEDIS.JEDISFACTORY:
kind: expression_statement
all:
- has:
stopBy: neighbor
kind: method_invocation
all:
- has:
stopBy: neighbor
kind: identifier
pattern: $R
- has:
stopBy: neighbor
kind: identifier
regex: "^setPassword$"
- has:
stopBy: neighbor
kind: argument_list
has:
stopBy: neighbor
kind: string_literal
- follows:
stopBy: end
kind: local_variable_declaration
all:
- has:
stopBy: neighbor
kind: scoped_type_identifier
all:
- has:
stopBy: neighbor
kind: scoped_type_identifier
all:
- has:
stopBy: end
kind: type_identifier
regex: "^redis$"
- has:
stopBy: end
kind: type_identifier
regex: "^clients$"
- has:
stopBy: end
kind: type_identifier
regex: "^jedis$"
- has:
stopBy: end
kind: type_identifier
regex: "^ConnectionFactory$|^JedisFactory$"
- has:
stopBy: neighbor
kind: variable_declarator
has:
stopBy: end
kind: identifier
pattern: $R
rule:
kind: expression_statement
any:
- matches: MATCH_PATTERN_JEDISFACTORY
- matches: MATCH_PATTERN_CLIENT_JEDIS.JEDISFACTORY
- matches: MATCH_PATTERN_JEDIS.JEDISFACTORY
- matches: MATCH_PATTERN_REDIS_CLIENT_JEDIS.JEDISFACTORY
35 changes: 35 additions & 0 deletions tests/__snapshots__/cookie-missing-secure-flag-java-snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
id: cookie-missing-secure-flag-java
snapshots:
? |
public class CookieController {

@RequestMapping(value = "/cookie1", method = "GET")
public void setCookie(@RequestParam String value, HttpServletResponse response) {
Cookie cookie = new Cookie("cookie", value);
response.addCookie(cookie);
}
: labels:
- source: response.addCookie(cookie);
style: primary
start: 220
end: 247
- source: response
style: secondary
start: 220
end: 228
- source: addCookie
style: secondary
start: 229
end: 238
- source: cookie
style: secondary
start: 239
end: 245
- source: (cookie)
style: secondary
start: 238
end: 246
- source: response.addCookie(cookie)
style: secondary
start: 220
end: 246
Loading