-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(golang): add hardcoded secret for pg
- Loading branch information
1 parent
53727fc
commit 2832c27
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
patterns: | ||
- pattern: | | ||
$<SQL>.Options{$<!>Password: $<STRING_LITERAL>} | ||
filters: | ||
- variable: SQL | ||
detection: go_lang_hardcoded_pg_database_password_init | ||
scope: cursor | ||
- variable: STRING_LITERAL | ||
detection: string_literal | ||
scope: cursor | ||
- pattern: | | ||
$<INIT>.Open($<_>, $<STRING>) | ||
filters: | ||
- variable: INIT | ||
detection: go_lang_hardcoded_pg_database_password_sql_init | ||
scope: cursor | ||
- variable: STRING | ||
string_regex: \Apostgres://\w+:.+@.*\z | ||
scope: cursor | ||
auxiliary: | ||
- id: go_lang_hardcoded_pg_database_password_init | ||
patterns: | ||
- import $<!>"github.com/lib/pg" | ||
- | | ||
import ( | ||
$<!>"github.com/lib/pg" | ||
) | ||
- id: go_lang_hardcoded_pg_database_password_sql_init | ||
patterns: | ||
- import $<!>"database/sql" | ||
- | | ||
import ( | ||
$<!>"database/sql" | ||
) | ||
languages: | ||
- go | ||
metadata: | ||
description: "Usage of hard-coded PostgreSQL database password" | ||
remediation_message: | | ||
## Description | ||
Hardcoded password used in database connection string detected. Code is not a safe place to store passwords like this; use environment variables or a key-management system instead. | ||
## Resources | ||
- [OWASP hardcoded passwords](https://owasp.org/www-community/vulnerabilities/Use_of_hard-coded_password) | ||
cwe_id: | ||
- 259 | ||
id: go_lang_hardcoded_pg_database_password | ||
documentation_url: https://docs.bearer.com/reference/rules/go_lang_hardcoded_pg_database_password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("hardcoded_database_password", () => { | ||
const testCase = "main.go" | ||
|
||
const results = invoke(testCase) | ||
|
||
expect(results.Missing).toEqual([]) | ||
expect(results.Extra).toEqual([]) | ||
}) | ||
}) |
54 changes: 54 additions & 0 deletions
54
tests/go/lang/hardcoded_pg_database_password/testdata/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Use of bearer:expected go_lang_hardcoded_mysql_database_password to flag expected findings | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/lib/pg" | ||
) | ||
|
||
func bad1() { | ||
options := &pg.Options{ | ||
Addr: ":5432", | ||
User: "admin123", | ||
// bearer:expected go_lang_hardcoded_pg_database_password | ||
Password: "password", | ||
Database: "db_name", | ||
} | ||
db := pg.Connect(options) | ||
} | ||
|
||
func bad2() { | ||
dsn := "postgres://admin123:password@localhost/db_name?sslmode=enable" | ||
// bearer:expected go_lang_hardcoded_pg_database_password | ||
db, err = sql.Open("postgres", dsn) | ||
} | ||
|
||
func good1() { | ||
dbUser := os.Getenv("DB_USER") | ||
dbPassword := os.Getenv("DB_PASSWORD") | ||
dbName := "myDatabase" | ||
|
||
dsn := fmt.Sprintf("postgres://%s:%s@localhost/%s", dbUser, dbPassword, dbName) | ||
|
||
db, err := sql.Open("postgres", dsn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
|
||
// Database operations go here | ||
// ... | ||
} | ||
|
||
func good2() { | ||
options := &pg.Options{ | ||
Addr: ":5432", | ||
User: "admin123", | ||
Password: os.Getenv("DB_PASSWORD"), | ||
Database: "db_name", | ||
} | ||
db := pg.Connect(options) | ||
} |