Skip to content

Commit

Permalink
feat(java): empty database password rule (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet authored Jan 31, 2024
1 parent c061aa1 commit fb80d38
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
32 changes: 32 additions & 0 deletions rules/java/lang/empty_database_password.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
patterns:
- pattern: |
$<SQL_DRIVER_MANAGER>.getConnection($<_>, $<_>, $<EMPTY_STRING>)
filters:
- variable: SQL_DRIVER_MANAGER
regex: \A(java\.sql\.)?DriverManager\z
- variable: EMPTY_STRING
string_regex: \A\z
languages:
- java
severity: warning
metadata:
description: "Empty database password detected."
remediation_message: |
## Description
A database with an empty password is a security risk as its data is unprotected.
Database servers should be configured with appropriate authentication and restrictions, and their passwords should be stored and accessed securely - for example, through a Key Management Service (KMS).
## Remediations
❌ Do not configure database servers with empty passwords
✅ Always ensure secure password management practices
## Resources
- [OWASP hardcoded passwords](https://owasp.org/www-community/vulnerabilities/Use_of_hard-coded_password)
cwe_id:
- 306
id: java_lang_empty_database_password
documentation_url: https://docs.bearer.com/reference/rules/java_lang_empty_database_password
18 changes: 18 additions & 0 deletions tests/java/lang/empty_database_password/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const {
createNewInvoker,
getEnvironment,
} = require("../../../helper.js")
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname)

describe(ruleId, () => {
const invoke = createNewInvoker(ruleId, ruleFile, testBase)

test("empty_database_password", () => {
const testCase = "main.java"

const results = invoke(testCase)

expect(results.Missing).toEqual([])
expect(results.Extra).toEqual([])
})
})
17 changes: 17 additions & 0 deletions tests/java/lang/empty_database_password/testdata/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Use bearer:expected java_lang_empty_database_password to flag expected findings
import java.sql.Connection;
import java.sql.DriverManager;

public class Foo
{
public static void bad() {
String url = "jdbc:mysql://localhost:3306/foo";
// bearer:expected java_lang_empty_database_password
Connection conn = DriverManager.getConnection(url, "root", "");
}

public static void ok() {
String url = "jdbc:mysql://localhost:3306/bar";
Connection conn = DriverManager.getConnection(url, "admin", "admin");
}
}

0 comments on commit fb80d38

Please sign in to comment.