Skip to content

Commit d47c547

Browse files
authored
Merge pull request #27 from advanced-security/testing
Testing, plus major changes to the patterns as a result of that testing
2 parents 184bc20 + 08b7260 commit d47c547

26 files changed

+1229
-75
lines changed

.github/scripts/validate.py

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Pattern:
6363

6464
regex: Regex = field(default_factory=Regex)
6565

66+
expected: Optional[List[Dict[str, str]]] = None
67+
6668
type: Optional[str] = None
6769
comments: List[str] = field(default_factory=list)
6870

Pipfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ jinja2 = "*"
1111

1212
[dev-packages]
1313

14+
1415
[requires]
15-
python_version = "3.8"
16+
python_version = "3.9"
1617

1718
[scripts]
1819
main = "python ./.github/scripts/validate.py"

configs/README.md

+27-10
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Hardcoded JDBC / Spring datasource passwords which typically are in property fil
6161
<p>
6262

6363
```regex
64-
[a-zA-Z0-9!$%&*+?^_`{|}~-]+
64+
[^\r\n'"]{1,40}
6565
```
6666

6767
</p>
@@ -72,7 +72,7 @@ Hardcoded JDBC / Spring datasource passwords which typically are in property fil
7272
<p>
7373

7474
```regex
75-
[^0-9A-Za-z](spring.datasource.password|jdbc.password)(\s+|)=(\s+|)
75+
\b(spring\.datasource\.password|jdbc\.password)[ \t]{0,15}=[ \t]{0,15}['"]?
7676
```
7777

7878
</p>
@@ -81,7 +81,7 @@ Hardcoded JDBC / Spring datasource passwords which typically are in property fil
8181
<p>
8282

8383
```regex
84-
\z|[^0-9A-Za-z]|'
84+
\z|['"\r\n]
8585
```
8686

8787
</p>
@@ -103,7 +103,7 @@ Hardcoded JDBC / Spring datasource passwords which typically are in property fil
103103
<p>
104104

105105
```regex
106-
[^\s"'(${{)][a-zA-Z0-9!.,$%&*+?^_`{|}()~-]*
106+
[^\r\n"']+
107107
```
108108

109109
</p>
@@ -114,7 +114,7 @@ Hardcoded JDBC / Spring datasource passwords which typically are in property fil
114114
<p>
115115

116116
```regex
117-
[^0-9A-Za-z](SECRET_KEY)(\s+|)=(\s+|)("|')
117+
\bSECRET_KEY[ \t]*=[ \t]*["']
118118
```
119119

120120
</p>
@@ -123,7 +123,7 @@ Hardcoded JDBC / Spring datasource passwords which typically are in property fil
123123
<p>
124124

125125
```regex
126-
\z|[^a-zA-Z0-9\s!.,$%&*+?^_`{|}()~-]|'|"
126+
['"]
127127
```
128128

129129
</p>
@@ -140,15 +140,15 @@ Pattern to find Static passwords in YAML configuration files
140140

141141
- The hardcoded password is between 12 and 32 chars long
142142
- Some false positives in Code might appear
143-
- The pattern only checks for cerain key words to begin the pattern (`secret:`, `password:`, etc.)
143+
- The pattern only checks for certain key words to begin the pattern (`secret`, `password`, etc.)
144144

145145

146146
<details>
147147
<summary>Pattern Format</summary>
148148
<p>
149149

150150
```regex
151-
[a-zA-Z0-9%!#$%&*+=?^_-{|}~\.,]{12,32}
151+
[^\r\n'"]{12,32}
152152
```
153153

154154
</p>
@@ -159,7 +159,7 @@ Pattern to find Static passwords in YAML configuration files
159159
<p>
160160

161161
```regex
162-
[^0-9A-Za-z](\s+|)(secret|service_pass(wd|word|code|phrase)|pass(wd|word|code|phrase)|key)(\s+|):(\s+|)
162+
(?:\n|\A)[ \t]{0,10}(?:secret|service_pass(wd|word|code|phrase)|pass(?:wd|word|code|phrase)?|key)[ \t]{0,30}:[ \t]{0,30}['"]?
163163
```
164164

165165
</p>
@@ -168,8 +168,25 @@ Pattern to find Static passwords in YAML configuration files
168168
<p>
169169

170170
```regex
171-
[^0-9A-Za-z'"\(\)]|\z
171+
['"\r\n]|\z
172172
```
173173

174+
</p>
175+
</details>
176+
<details>
177+
<summary>Additional Matches</summary>
178+
<p>
179+
Add these additional matches to the [Secret Scanning Custom Pattern](https://docs.github.com/en/enterprise-cloud@latest/code-security/secret-scanning/defining-custom-patterns-for-secret-scanning#example-of-a-custom-pattern-specified-using-additional-requirements).
180+
181+
182+
- Not Match: `^keyPassphrase$`
183+
- Not Match: `^.* = (?:None|True|False),?$`
184+
- Not Match: `^.* = \.\.\.,?$`
185+
- Not Match: `^(?:this\.)?[A-Za-z_]+\,$`
186+
- Not Match: `^(?:[a-zA-Z_]+(?:\(\))?\.)*[a-zA-Z_]+\(\)$`
187+
- Not Match: `^(?:str|int|bool)( +#.*)?$`
188+
- Not Match: `^[ \t]+$`
189+
- Not Match: `^\s*(?:typing\.)?(?:[Tt]uple|[Ll]ist|[Dd]ict|Callable|Iterable|Sequence|Optional|Union)\[.*$`
190+
174191
</p>
175192
</details>

configs/application.properties

+10-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ spring.datasource.password = root
1111
# no spaces
1212
spring.datasource.password=SQLSpringPassword
1313
# quoted password
14-
spring.datasource.password='SQLSpringPassword'
14+
spring.datasource.password='QuotedSpringPassword'
1515

16+
# Encrypted
17+
datasource.driver=com.mysql.jdbc.Driver
18+
datasource.url=jdbc:mysql://localhost/reportsdb
19+
datasource.username=reportsUser
20+
datasource.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
1621

1722

1823
# Sources:
@@ -21,27 +26,27 @@ spring.datasource.password='SQLSpringPassword'
2126
# H2 DB
2227
spring.datasource.url=jdbc:h2:file:C:/temp/test
2328
spring.datasource.username=sa
24-
spring.datasource.password=
29+
spring.datasource.password=dbpass1
2530
spring.datasource.driverClassName=org.h2.Driver
2631
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
2732

2833
# MySQL
2934
spring.datasource.url=jdbc:mysql://localhost:3306/test
3035
spring.datasource.username=dbuser
31-
spring.datasource.password=dbpass
36+
spring.datasource.password=dbpass2
3237
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3338
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
3439

3540
# Oracle
3641
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
3742
spring.datasource.username=dbuser
38-
spring.datasource.password=dbpass
43+
spring.datasource.password=dbpass3
3944
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
4045
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
4146

4247
# SQL Server
4348
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=springbootdb
4449
spring.datasource.username=dbuser
45-
spring.datasource.password=dbpass
50+
spring.datasource.password=dbpass4
4651
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
4752
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect

configs/global_settings.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# this is a fake/sample Django setting file
2+
3+
SECRET_KEY = '!r7!(xjadix=(m5t9$0y%+bdxs#$^4u+7(s+kg&m67o0jsj&b$' # sample
4+

configs/patterns.yml

+56-12
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,51 @@ patterns:
2525
regex:
2626
version: 0.1
2727
pattern: |
28-
[a-zA-Z0-9!$%&*+?^_`{|}~-]+
28+
[^\r\n'"\p{Cc}]+
2929
start: |
30-
[^0-9A-Za-z](spring.datasource.password|jdbc.password)(\s+|)=(\s+|)
30+
(?:spring\.datasource|jdbc)\.password[ \t]*=[ \t]*['"]?
3131
end: |
32-
\z|[^0-9A-Za-z]|'
32+
\z|['"\r\n]
33+
expected:
34+
- name: application.properties
35+
start_offset: 314
36+
end_offset: 318
37+
- name: application.properties
38+
start_offset: 358
39+
end_offset: 375
40+
- name: application.properties
41+
start_offset: 422
42+
end_offset: 442
43+
- name: application.properties
44+
start_offset: 836
45+
end_offset: 843
46+
- name: application.properties
47+
start_offset: 1078
48+
end_offset: 1085
49+
- name: application.properties
50+
start_offset: 1346
51+
end_offset: 1353
52+
- name: application.properties
53+
start_offset: 1633
54+
end_offset: 1640
55+
3356

3457
- name: Django Secret Key
3558
type: django_secret_key
3659
regex:
3760
version: 0.1
3861
pattern: |
39-
[^\s"'(${{)][a-zA-Z0-9!.,$%&*+?^_`{|}()~-]*
62+
[^\r\n"']+
4063
start: |
41-
[^0-9A-Za-z](SECRET_KEY)(\s+|)=(\s+|)("|')
64+
\bSECRET_KEY[ \t]*=[ \t]*["']
4265
end: |
43-
\z|[^a-zA-Z0-9\s!.,$%&*+?^_`{|}()~-]|'|"
44-
66+
['"]
4567
comments:
4668
- "_If the secret is at the start of the file, its not picked up_"
69+
expected:
70+
- name: global_settings.py
71+
start_offset: 59
72+
end_offset: 109
4773

4874

4975
# Experimental
@@ -56,13 +82,31 @@ patterns:
5682
regex:
5783
version: 0.1
5884
pattern: |
59-
[a-zA-Z0-9%!#$%&*+=?^_-{|}~\.,]{12,32}
85+
[^\r\n'"]*
6086
start: |
61-
[^0-9A-Za-z](\s+|)(secret|service_pass(wd|word|code|phrase)|pass(wd|word|code|phrase)|key)(\s+|):(\s+|)
87+
(?:\n|\A)[ \t]*(?:secret|service_pass(wd|word|code|phrase)|pass(?:wd|word|code|phrase)?|key)[ \t]*:[ \t]*['"]?
6288
end: |
63-
[^0-9A-Za-z'"\(\)]|\z
64-
89+
['"\r\n]|\z
90+
additional_not_match:
91+
- ^keyPassphrase$
92+
- ^.* = (?:None|True|False),?$
93+
- ^.* = \.\.\.,?$
94+
- ^(?:(?:this|self|obj)\.)?[A-Za-z_]+\,$
95+
- ^(?:(?:this|self|obj)\.)[A-Za-z_].*$
96+
- ^(?:[a-zA-Z_]+(?:\(\))?\.)*[a-zA-Z_]+\(\)$
97+
- ^(?:str|int|bool)( +#.*)?$
98+
- ^[ \t]+$
99+
- ^\s*(?:typing\.)?(?:[Tt]uple|[Ll]ist|[Dd]ict|Callable|Iterable|Sequence|Optional|Union)\[.*$
100+
- ^\$\{[A-Za-z0-9_-]+\}$
65101
comments:
66102
- "The hardcoded password is between 12 and 32 chars long"
67103
- "Some false positives in Code might appear"
68-
- "The pattern only checks for cerain key words to begin the pattern (`secret:`, `password:`, etc.)"
104+
- "The pattern only checks for certain key words to begin the pattern (`secret`, `password`, etc.)"
105+
expected:
106+
- name: example.yml
107+
start_offset: 57
108+
end_offset: 80
109+
- name: example.yml
110+
start_offset: 57
111+
end_offset: 80
112+

generic/README.md

+73-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<p>
2222

2323
```regex
24-
[^\t "'(${{)][a-zA-Z0-9\t !.,$%&*+?^_`{|}()~-]+
24+
[a-zA-Z0-9!.,$%&*+?^_`{|}()[\]\\/~-][a-zA-Z0-9\t !.,$%&*+?^_`{|}()[\]\\/~-]*
2525
```
2626

2727
</p>
@@ -32,7 +32,7 @@
3232
<p>
3333

3434
```regex
35-
(?i)((api|jwt|mysql|)?(_|-|.)?((pass|pas)(wd|wrd|word|code|phrase)|pass|pwd|secret|token))([\t ]+|)(=|:)([\t ]+|)("|'|[\t ]|)
35+
(?i)(?:api|jwt|mysql)?[_.-]?(?:pass?(?:wo?r?d|code|phrase)|pwd|secret)[\t ]*(={1,3}|:)[\t ]*(?:["']|b["'])?
3636
```
3737

3838
</p>
@@ -41,7 +41,7 @@
4141
<p>
4242

4343
```regex
44-
\z|[^a-zA-Z0-9\t !.,$%&*+?^_`{|}()~-]|'|"
44+
(\z|[\r\n'"])
4545
```
4646

4747
</p>
@@ -52,7 +52,43 @@
5252
Add these additional matches to the [Secret Scanning Custom Pattern](https://docs.github.com/en/enterprise-cloud@latest/code-security/secret-scanning/defining-custom-patterns-for-secret-scanning#example-of-a-custom-pattern-specified-using-additional-requirements).
5353

5454

55-
- Not Match: `\b((?i)(pass|pas)(wd|wrd|word|code|phrase)|pass|pwd|secret|token|write|read|on|off|true|false|placeholder|dummy)\b`
55+
- Not Match: `^(?i)(?:[A-Za-z0-9_.]*,\s*)?(?:str\()?[[<(]?(?:(?:user|key)_?)?(?:pass?(wo?r?d|code|phrase)|pass|pwd|secret|token|tok|redacted|placeholder|dummy|pw|thephrase),?[\]>)]?\\?$`
56+
- Not Match: `^.*token.*$`
57+
- Not Match: `^[a-zA-Z0-9._]+[_.](?:password|passphrase|secret|key).*$`
58+
- Not Match: `^.* passphrase .*$`
59+
- Not Match: `^(?i)(?:[a-zA-Z0-9_.]*,\s*)?[[<(]?(?:write|read|on|off|true|false|none|null|nil|undefined|eof|ignore|eol|git|yes|no|y|n),?[\]>)]?(?:\)\s*\{)?\\?$`
60+
- Not Match: `^\s*%[sr]\s*$`
61+
- Not Match: `^\s*$`
62+
- Not Match: `^\s*(?:int|str|(?:typing\.)?Any|None|bytes|bool|ReadableBuffer)\s*([,|].*)?\s*$`
63+
- Not Match: `^\s*(?:typing\.)?(?:[Tt]uple|[Ll]ist|[Dd]ict|Callable|Iterable|Sequence|Optional|Union)\[.*$`
64+
- Not Match: `^\s*\.\.\.,?\s*$`
65+
- Not Match: `^\s*\\\s*$`
66+
- Not Match: `^\\n$`
67+
- Not Match: `^\s*,s*$`
68+
- Not Match: `^\\0$`
69+
- Not Match: `^function\s*\([^)]*\)\s*{\s*`
70+
- Not Match: `^\([^)]*\)\s*=>\s*(?:{\s*|[^;)]+[;)])$`
71+
- Not Match: `^\s*[0-9]{1,4}(?:\s*(?:/\*|#|//).*)?$`
72+
- Not Match: `^(?:new )?[a-zA-Z0-9_.]+\(.*$`
73+
- Not Match: `^\s*(?:self|this)\.[a-zA-Z_][a-zA-Z0-9_]+[,[]?\s*$`
74+
- Not Match: `^\s*[a-zA-Z0-9_.]+\[(?:[a-zA-Z0-9_.]+)?\]?\s*$`
75+
- Not Match: `^\s*(?:~|/tmp|\.\.|\.)\s*$`
76+
- Not Match: `^\\{1,2}w\+/g,( \\?)?$`
77+
- Not Match: `^\s*\$\{[^}]+\}\s*$`
78+
- Not Match: `^\s*\$\([^)]+\)\s*$`
79+
- Not Match: `^\s*\{[^}]*\}\s*$`
80+
- Not Match: `^\s*\[[^\]]*\]\s*$`
81+
- Not Match: `^[,()[\]{}`.]\\?$`
82+
- Not Match: `^geheim\$parole$`
83+
- Not Match: `^\s*\([Oo]ptional\).*$`
84+
- Not Match: `^-[)(]$`
85+
- Not Match: `^0x[A-Fa-f0-9]+,?$`
86+
- Not Match: `^\$[1-9]$`
87+
- Not Match: `^\$[A-Za-z0-9_]+$`
88+
- Not Match: `^[0-9],?$`
89+
- Not Match: `^\s*ALL(?:\\n)?\s*$`
90+
- Not Match: `^(?:public|private) [A-Za-z0-9_]+ \{$`
91+
- Not Match: `^\$[a-zA-Z0-9_]+\{$`
5692

5793
</p>
5894
</details>
@@ -70,9 +106,41 @@ Add these additional matches to the [Secret Scanning Custom Pattern](https://doc
70106
<p>
71107

72108
```regex
73-
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
109+
(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
74110
```
75111

76112
</p>
77113
</details>
78114

115+
<details>
116+
<summary>Start Pattern</summary>
117+
<p>
118+
119+
```regex
120+
\A|[^0-9A-Fa-f-]
121+
```
122+
123+
</p>
124+
</details><details>
125+
<summary>End Pattern</summary>
126+
<p>
127+
128+
```regex
129+
\z|[^0-9A-Fa-f-]
130+
```
131+
132+
</p>
133+
</details>
134+
<details>
135+
<summary>Additional Matches</summary>
136+
<p>
137+
Add these additional matches to the [Secret Scanning Custom Pattern](https://docs.github.com/en/enterprise-cloud@latest/code-security/secret-scanning/defining-custom-patterns-for-secret-scanning#example-of-a-custom-pattern-specified-using-additional-requirements).
138+
139+
140+
- Not Match: `^12345678-1234-5678-1234-567812345678$`
141+
- Not Match: `^00000000-0000-0000-0000-000000000000$`
142+
- Not Match: `^(?i)00010203-0405-0607-0809-0a0b0c0d0e0f$`
143+
- Not Match: `^(?i)12345678-1234-1234-1234-123456789abc$`
144+
145+
</p>
146+
</details>

0 commit comments

Comments
 (0)