codewatch-scan 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. codewatch/__init__.py +0 -0
  2. codewatch/__main__.py +14 -0
  3. codewatch/asgi.py +16 -0
  4. codewatch/settings.py +175 -0
  5. codewatch/urls.py +40 -0
  6. codewatch/wsgi.py +16 -0
  7. codewatch_scan-0.1.0.dist-info/METADATA +81 -0
  8. codewatch_scan-0.1.0.dist-info/RECORD +136 -0
  9. codewatch_scan-0.1.0.dist-info/WHEEL +5 -0
  10. codewatch_scan-0.1.0.dist-info/entry_points.txt +4 -0
  11. codewatch_scan-0.1.0.dist-info/licenses/LICENSE +21 -0
  12. codewatch_scan-0.1.0.dist-info/top_level.txt +2 -0
  13. core/__init__.py +0 -0
  14. core/analyzers/__init__.py +19 -0
  15. core/analyzers/accessibility.py +199 -0
  16. core/analyzers/base.py +192 -0
  17. core/analyzers/body_models.py +154 -0
  18. core/analyzers/codewatch_checks.py +394 -0
  19. core/analyzers/constant_branches.py +251 -0
  20. core/analyzers/containers.py +316 -0
  21. core/analyzers/coverage.py +131 -0
  22. core/analyzers/dependencies.py +347 -0
  23. core/analyzers/drf_serializers.py +65 -0
  24. core/analyzers/else_branches.py +106 -0
  25. core/analyzers/extra.py +400 -0
  26. core/analyzers/flask_resources.py +65 -0
  27. core/analyzers/inline_scripts.py +50 -0
  28. core/analyzers/jinja_sandboxes.py +63 -0
  29. core/analyzers/match_statements.py +148 -0
  30. core/analyzers/multi_tools.py +888 -0
  31. core/analyzers/php_template_sinks.py +444 -0
  32. core/analyzers/project_checks.py +150 -0
  33. core/analyzers/python_tools.py +333 -0
  34. core/analyzers/route_converters.py +237 -0
  35. core/analyzers/slices.py +94 -0
  36. core/analyzers/starred_targets.py +42 -0
  37. core/analyzers/stored_fields.py +386 -0
  38. core/analyzers/summaries.py +1387 -0
  39. core/analyzers/template_sinks.py +162 -0
  40. core/api.py +360 -0
  41. core/apps.py +6 -0
  42. core/benchmark.py +955 -0
  43. core/bundled/catalog/codewatch/rules/javascript-crypto.yml +224 -0
  44. core/bundled/catalog/codewatch/rules/javascript-injection.yml +116 -0
  45. core/bundled/catalog/codewatch/rules/javascript-taint.yml +1539 -0
  46. core/bundled/catalog/codewatch/rules/javascript-web.yml +562 -0
  47. core/bundled/catalog/codewatch/rules/php-web.yml +719 -0
  48. core/bundled/catalog/codewatch/rules/python-crypto.yml +203 -0
  49. core/bundled/catalog/codewatch/rules/python-django.yml +267 -0
  50. core/bundled/catalog/codewatch/rules/python-injection.yml +115 -0
  51. core/bundled/catalog/codewatch/rules/python-web.yml +3382 -0
  52. core/bundled/catalog/custom/mappings.json +360 -0
  53. core/bundled/catalog/custom/overrides.json +4 -0
  54. core/bundled/catalog/custom/rules/django-settings.yml +19 -0
  55. core/bundled/catalog/imported/changes.json +5 -0
  56. core/bundled/catalog/imported/docs.json +1143 -0
  57. core/bundled/catalog/imported/meta.json +54 -0
  58. core/bundled/catalog/imported/rules.json +85421 -0
  59. core/bundled/catalog/imported/standards.json +11191 -0
  60. core/bundled/tools/analyzers.lock +786 -0
  61. core/bundled/tools/axe-scan.mjs +45 -0
  62. core/bundled/tools/checkov.lock +2463 -0
  63. core/bundled/tools/codewatch-checks.mjs +99 -0
  64. core/bundled/tools/eslint-a11y.config.js +16 -0
  65. core/bundled/tools/eslint-quality.config.js +63 -0
  66. core/bundled/tools/eslint-typed.config.js +18 -0
  67. core/bundled/tools/package-lock.json +6102 -0
  68. core/bundled/tools/package.json +32 -0
  69. core/bundled/tools/rule-metadata.mjs +56 -0
  70. core/bundled/tools/semgrep.lock +1232 -0
  71. core/bundled/tools/stylelint.config.json +11 -0
  72. core/catalog/__init__.py +190 -0
  73. core/catalog/build.py +84 -0
  74. core/catalog/merge.py +122 -0
  75. core/catalog/sources.py +418 -0
  76. core/catalog/standards.py +119 -0
  77. core/catalog/update.py +99 -0
  78. core/catalog_views.py +115 -0
  79. core/client.py +645 -0
  80. core/collect.py +518 -0
  81. core/duplicates.py +60 -0
  82. core/fingerprint.py +25 -0
  83. core/folders.py +73 -0
  84. core/gate.py +77 -0
  85. core/git.py +149 -0
  86. core/jobs.py +86 -0
  87. core/management/__init__.py +0 -0
  88. core/management/commands/__init__.py +0 -0
  89. core/management/commands/addproject.py +40 -0
  90. core/management/commands/buildcatalog.py +15 -0
  91. core/management/commands/scan.py +74 -0
  92. core/management/commands/updatecatalog.py +14 -0
  93. core/mcp_server.py +140 -0
  94. core/middleware.py +17 -0
  95. core/migrations/0001_initial.py +115 -0
  96. core/migrations/0002_auth_tokens_a11y.py +60 -0
  97. core/migrations/0003_default_admin.py +43 -0
  98. core/migrations/0004_project_members.py +28 -0
  99. core/migrations/0005_uploads_and_sources.py +55 -0
  100. core/migrations/0006_thresholds.py +28 -0
  101. core/migrations/0007_also_reported_by.py +18 -0
  102. core/migrations/0008_new_code_period.py +18 -0
  103. core/migrations/0009_resolution_safe.py +18 -0
  104. core/migrations/0010_analysis_progress.py +18 -0
  105. core/migrations/0011_overall_gate_conditions.py +20 -0
  106. core/migrations/__init__.py +0 -0
  107. core/models.py +323 -0
  108. core/network.py +41 -0
  109. core/paths.py +29 -0
  110. core/ratings.py +67 -0
  111. core/runner.py +168 -0
  112. core/templates/core/account.html +30 -0
  113. core/templates/core/analysis.html +49 -0
  114. core/templates/core/base.html +121 -0
  115. core/templates/core/form_table.html +12 -0
  116. core/templates/core/gate_badge.html +1 -0
  117. core/templates/core/index.html +26 -0
  118. core/templates/core/issues.html +38 -0
  119. core/templates/core/login.html +13 -0
  120. core/templates/core/new_project.html +27 -0
  121. core/templates/core/password_change.html +10 -0
  122. core/templates/core/project.html +40 -0
  123. core/templates/core/rating.html +1 -0
  124. core/templates/core/ratings.html +32 -0
  125. core/templates/core/rule.html +33 -0
  126. core/templates/core/rules.html +42 -0
  127. core/templates/core/scan_progress.html +2 -0
  128. core/templates/core/settings.html +36 -0
  129. core/templates/core/source.html +15 -0
  130. core/templates/core/standards.html +30 -0
  131. core/templates/core/user_edit.html +15 -0
  132. core/templates/core/users.html +18 -0
  133. core/templatetags/__init__.py +0 -0
  134. core/templatetags/codewatch.py +42 -0
  135. core/tool_setup.py +68 -0
  136. core/views.py +564 -0
@@ -0,0 +1,203 @@
1
+ # codewatch built-in rules for misused cryptography, TLS and credentials in Python: API calls that are wrong
2
+ # whatever data reaches them, so they are reported as vulnerabilities rather than hotspots.
3
+ rules:
4
+ - id: codewatch.python.tls-verification-disabled
5
+ languages: [python]
6
+ severity: ERROR
7
+ message: >-
8
+ Certificate or hostname verification is switched off, so anyone on the network path can impersonate the
9
+ server. Keep verification on; for a private CA, pass its bundle (verify="/path/ca.pem", cafile=...).
10
+ metadata:
11
+ category: security
12
+ subcategory: [vuln]
13
+ type: vulnerability
14
+ title: TLS certificate verification disabled
15
+ cwe: ["CWE-295: Improper Certificate Validation"]
16
+ owasp: ["A04:2025 - Cryptographic Failures"]
17
+ references:
18
+ - https://docs.python.org/3/library/ssl.html#ssl.SSLContext.verify_mode
19
+ - https://requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification
20
+ pattern-either:
21
+ - pattern: $CTX.check_hostname = False
22
+ - pattern: $CTX.verify_mode = ssl.CERT_NONE
23
+ - pattern: ssl._create_unverified_context(...)
24
+ - pattern: ssl.wrap_socket(..., cert_reqs=ssl.CERT_NONE, ...)
25
+ - pattern: requests.$METHOD(..., verify=False, ...)
26
+ - pattern: requests.Session(...).$METHOD(..., verify=False, ...)
27
+ - pattern: httpx.$METHOD(..., verify=False, ...)
28
+ - pattern: $SESSION.verify = False
29
+
30
+ - id: codewatch.python.weak-cipher
31
+ languages: [python]
32
+ severity: ERROR
33
+ message: >-
34
+ This cipher or mode doesn't protect the data: DES, 3DES, RC4 and Blowfish are broken, and ECB mode leaks
35
+ patterns in the plaintext. Use AES-GCM or ChaCha20-Poly1305 (cryptography's AESGCM, ChaCha20Poly1305).
36
+ metadata:
37
+ category: security
38
+ subcategory: [vuln]
39
+ type: vulnerability
40
+ title: Weak cipher or ECB mode
41
+ cwe: ["CWE-327: Use of a Broken or Risky Cryptographic Algorithm"]
42
+ owasp: ["A04:2025 - Cryptographic Failures"]
43
+ references:
44
+ - https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/
45
+ - https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html
46
+ pattern-either:
47
+ - pattern: Crypto.Cipher.AES.new($KEY, Crypto.Cipher.AES.MODE_ECB, ...)
48
+ - pattern: Cryptodome.Cipher.AES.new($KEY, Cryptodome.Cipher.AES.MODE_ECB, ...)
49
+ - pattern: cryptography.hazmat.primitives.ciphers.modes.ECB(...)
50
+ - patterns:
51
+ - pattern-either:
52
+ - pattern: Crypto.Cipher.$WEAK.new(...)
53
+ - pattern: Cryptodome.Cipher.$WEAK.new(...)
54
+ - pattern: cryptography.hazmat.primitives.ciphers.algorithms.$WEAK(...)
55
+ - metavariable-regex: {metavariable: $WEAK, regex: '^(DES|DES3|ARC2|ARC4|Blowfish|CAST|TripleDES|IDEA|SEED)$'}
56
+
57
+ - id: codewatch.python.static-iv
58
+ languages: [python]
59
+ severity: ERROR
60
+ message: >-
61
+ The IV (or nonce) is a constant, so the same plaintext always encrypts the same way and, for CTR/GCM, the
62
+ key stream repeats. Generate a fresh one per message with os.urandom() and store it with the ciphertext.
63
+ metadata:
64
+ category: security
65
+ subcategory: [vuln]
66
+ type: vulnerability
67
+ title: Constant IV or nonce
68
+ cwe: ["CWE-329: Generation of Predictable IV with CBC Mode", "CWE-1204: Generation of Weak Initialization Vector (IV)"]
69
+ owasp: ["A04:2025 - Cryptographic Failures"]
70
+ references:
71
+ - https://cryptography.io/en/latest/hazmat/primitives/symmetric-encryption/#modes
72
+ # The IV is a literal, or a variable assigned one (b"x" * 16, bytes(16)) earlier in the function.
73
+ pattern-either:
74
+ - patterns:
75
+ - pattern-either: &iv-calls
76
+ - pattern: Crypto.Cipher.AES.new($KEY, $MODE, $IV, ...)
77
+ - pattern: Crypto.Cipher.AES.new($KEY, $MODE, ..., iv=$IV, ...)
78
+ - pattern: Crypto.Cipher.AES.new($KEY, $MODE, ..., nonce=$IV, ...)
79
+ - pattern: Cryptodome.Cipher.AES.new($KEY, $MODE, $IV, ...)
80
+ - pattern: Cryptodome.Cipher.AES.new($KEY, $MODE, ..., iv=$IV, ...)
81
+ - pattern: cryptography.hazmat.primitives.ciphers.modes.$CHAIN($IV, ...)
82
+ - metavariable-pattern:
83
+ metavariable: $IV
84
+ pattern-either: &constant-bytes
85
+ - pattern: '"..."'
86
+ - pattern: '"..." * $N'
87
+ - pattern: bytes($N)
88
+ - pattern: bytearray($N)
89
+ - patterns:
90
+ - pattern-either: *iv-calls
91
+ - pattern-inside: |
92
+ $IV = $INIT
93
+ ...
94
+ - metavariable-pattern:
95
+ metavariable: $INIT
96
+ pattern-either: *constant-bytes
97
+
98
+ - id: codewatch.python.constant-salt
99
+ languages: [python]
100
+ severity: ERROR
101
+ message: >-
102
+ The password hash uses a constant salt, so equal passwords hash equally and one precomputed table cracks
103
+ them all. Use a random salt per password (os.urandom(16)) stored next to the hash, or a password hasher.
104
+ metadata:
105
+ category: security
106
+ subcategory: [vuln]
107
+ type: vulnerability
108
+ title: Constant salt in a password hash
109
+ cwe: ["CWE-760: Use of a One-Way Hash with a Predictable Salt"]
110
+ owasp: ["A04:2025 - Cryptographic Failures"]
111
+ references:
112
+ - https://docs.python.org/3/library/hashlib.html#key-derivation
113
+ - https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
114
+ pattern-either:
115
+ - pattern: hashlib.pbkdf2_hmac($HASH, $PASSWORD, "...", ...)
116
+ - pattern: hashlib.pbkdf2_hmac(..., salt="...", ...)
117
+ - pattern: hashlib.scrypt($PASSWORD, salt="...", ...)
118
+ - pattern: cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC(..., salt="...", ...)
119
+ - pattern: bcrypt.hashpw($PASSWORD, "...")
120
+
121
+ - id: codewatch.python.jwt-not-verified
122
+ languages: [python]
123
+ severity: ERROR
124
+ message: >-
125
+ The token is decoded without checking its signature (or with the "none" algorithm), so anyone can forge
126
+ one. Pass the key and an explicit algorithms=[...] list, and leave signature verification on.
127
+ metadata:
128
+ category: security
129
+ subcategory: [vuln]
130
+ type: vulnerability
131
+ title: JWT accepted without signature verification
132
+ cwe: ["CWE-347: Improper Verification of Cryptographic Signature"]
133
+ owasp: ["A07:2025 - Authentication Failures"]
134
+ references:
135
+ - https://pyjwt.readthedocs.io/en/stable/api.html#jwt.decode
136
+ pattern-either:
137
+ - pattern: jwt.decode(..., verify=False, ...)
138
+ - pattern: 'jwt.decode(..., options={..., "verify_signature": False, ...}, ...)'
139
+ - pattern: jwt.decode(..., algorithms=[..., "none", ...], ...)
140
+ - patterns:
141
+ - pattern: python_jwt.process_jwt(...)
142
+ - pattern-not-inside: |
143
+ $X = python_jwt.process_jwt(...)
144
+ ...
145
+ python_jwt.verify_jwt(...)
146
+
147
+ - id: codewatch.python.insecure-temp-file
148
+ languages: [python]
149
+ severity: WARNING
150
+ message: >-
151
+ mktemp() only returns a name: another process can create that file first and have you write into it.
152
+ Use tempfile.NamedTemporaryFile() or mkstemp(), which create the file atomically.
153
+ metadata:
154
+ category: security
155
+ subcategory: [vuln]
156
+ type: vulnerability
157
+ title: Insecure temporary file name
158
+ cwe: ["CWE-377: Insecure Temporary File"]
159
+ owasp: ["A01:2025 - Broken Access Control"]
160
+ references:
161
+ - https://docs.python.org/3/library/tempfile.html#tempfile.mktemp
162
+ pattern-either:
163
+ - pattern: tempfile.mktemp(...)
164
+ - pattern: os.tempnam(...)
165
+ - pattern: os.tmpnam(...)
166
+
167
+ - id: codewatch.python.cleartext-protocol
168
+ languages: [python]
169
+ severity: WARNING
170
+ message: >-
171
+ FTP and Telnet send credentials and data unencrypted. Use SFTP/FTPS (ftplib.FTP_TLS) or SSH instead.
172
+ metadata:
173
+ category: security
174
+ subcategory: [vuln]
175
+ type: vulnerability
176
+ title: Cleartext protocol
177
+ cwe: ["CWE-319: Cleartext Transmission of Sensitive Information"]
178
+ owasp: ["A04:2025 - Cryptographic Failures"]
179
+ references:
180
+ - https://docs.python.org/3/library/ftplib.html#ftplib.FTP_TLS
181
+ pattern-either:
182
+ - pattern: ftplib.FTP(...)
183
+ - pattern: telnetlib.Telnet(...)
184
+
185
+ - id: codewatch.python.database-url-without-password
186
+ languages: [python]
187
+ severity: WARNING
188
+ message: >-
189
+ This database URL logs in without a password. Require one and load the URL from configuration.
190
+ metadata:
191
+ category: security
192
+ subcategory: [vuln]
193
+ type: vulnerability
194
+ title: Database login without a password
195
+ cwe: ["CWE-521: Weak Password Requirements"]
196
+ owasp: ["A07:2025 - Authentication Failures"]
197
+ references:
198
+ - https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls
199
+ patterns:
200
+ - pattern: '"$URL"'
201
+ - metavariable-regex:
202
+ metavariable: $URL
203
+ regex: '^(postgres(ql)?|mysql|mariadb|mssql|oracle|mongodb)(\+\w{1,20})?://[^:/@\s]{1,100}:@'
@@ -0,0 +1,267 @@
1
+ # codewatch built-in rules for Django views and Django/Jinja templates.
2
+ rules:
3
+ - id: codewatch.django.view-without-method-restriction
4
+ languages: [python]
5
+ severity: INFO
6
+ message: >-
7
+ This view changes state (saves, deletes or reads POST data) but accepts every HTTP method, so a plain
8
+ GET link can trigger it without Django's CSRF check. Add @require_POST (or @require_http_methods), or
9
+ branch on request.method. Read-only views are not flagged.
10
+ metadata:
11
+ category: security
12
+ subcategory: [audit]
13
+ type: security_hotspot
14
+ title: State-changing Django view without an HTTP method restriction
15
+ cwe: ["CWE-650: Trusting HTTP Permission Methods on the Server Side"]
16
+ owasp: ["A01:2025 - Broken Access Control"]
17
+ references:
18
+ - https://docs.djangoproject.com/en/stable/topics/http/decorators/#allowed-http-methods
19
+ - https://docs.djangoproject.com/en/stable/ref/csrf/
20
+ paths:
21
+ include: ["**/*views.py", "**/views/*.py"]
22
+ patterns:
23
+ - pattern: |
24
+ def $VIEW(request, ...):
25
+ ...
26
+ return <... $RESPONDER(...) ...>
27
+ - metavariable-regex:
28
+ metavariable: $RESPONDER
29
+ regex: '(.*\.)?(render|redirect|HttpResponse\w{0,20}|JsonResponse|TemplateResponse|FileResponse|StreamingHttpResponse)$'
30
+ # Only views that change something: a read-only page that also answers POST does no harm.
31
+ - pattern-either:
32
+ - patterns:
33
+ - pattern: |
34
+ def $VIEW(request, ...):
35
+ ...
36
+ <... $OBJ.$MUTATE(...) ...>
37
+ ...
38
+ - metavariable-regex:
39
+ metavariable: $MUTATE
40
+ regex: '^(save|delete|create|update|bulk_create|bulk_update|get_or_create|update_or_create|send_mail|set_password)$'
41
+ - patterns:
42
+ - pattern: |
43
+ def $VIEW(request, ...):
44
+ ...
45
+ <... $AUTH(request, ...) ...>
46
+ ...
47
+ - metavariable-regex:
48
+ metavariable: $AUTH
49
+ regex: '^(login|logout)$'
50
+ - pattern: |
51
+ def $VIEW(request, ...):
52
+ ...
53
+ <... request.POST ...>
54
+ ...
55
+ - pattern: |
56
+ def $VIEW(request, ...):
57
+ ...
58
+ <... request.FILES ...>
59
+ ...
60
+ - pattern-not-inside: |
61
+ class $CLASS(...):
62
+ ...
63
+ - pattern-not: |
64
+ @require_http_methods(...)
65
+ def $VIEW(request, ...):
66
+ ...
67
+ - pattern-not: |
68
+ @require_POST
69
+ def $VIEW(request, ...):
70
+ ...
71
+ - pattern-not: |
72
+ @require_GET
73
+ def $VIEW(request, ...):
74
+ ...
75
+ - pattern-not: |
76
+ @require_safe
77
+ def $VIEW(request, ...):
78
+ ...
79
+ - pattern-not: |
80
+ def $VIEW(request, ...):
81
+ ...
82
+ if <... request.method ...>:
83
+ ...
84
+ ...
85
+
86
+ - id: codewatch.templates.unescaped-output
87
+ languages: [generic]
88
+ severity: WARNING
89
+ message: >-
90
+ Template auto-escaping is switched off here (|safe or {% autoescape off %}), so any HTML in the value is
91
+ rendered as-is. Make sure the value can't contain user input, or escape it before marking it safe.
92
+ metadata:
93
+ category: security
94
+ subcategory: [audit]
95
+ type: security_hotspot
96
+ title: Auto-escaping turned off in a template
97
+ cwe: ["CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')"]
98
+ owasp: ["A05:2025 - Injection"]
99
+ references:
100
+ - https://docs.djangoproject.com/en/stable/ref/templates/builtins/#safe
101
+ - https://docs.djangoproject.com/en/stable/ref/templates/builtins/#autoescape
102
+ - https://jinja.palletsprojects.com/en/stable/templates/#html-escaping
103
+ - https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html
104
+ paths:
105
+ include: ["*.html", "*.htm", "*.jinja", "*.jinja2", "*.j2", "*.djhtml"]
106
+ pattern-either:
107
+ - pattern-regex: '\{\{[^{}\n]{0,200}\|\s{0,5}safe\b'
108
+ - pattern-regex: '\{%-?\s{0,5}autoescape\s{1,5}(off|false)\b'
109
+
110
+ - id: codewatch.templates.request-in-url-attribute
111
+ languages: [generic]
112
+ severity: ERROR
113
+ message: >-
114
+ A value from the request's query or form is printed at the start of a link (href, src, action) or inside an
115
+ event handler. Autoescaping keeps it inside the attribute, but a "javascript:" URL still runs, and in an
116
+ onclick the escaped quotes are decoded before the script runs. Build the link on the server and check it
117
+ (url_for, or an allowlist of hosts and schemes), and pass handler values through data attributes.
118
+ # Formspree GHSA-2wvm-9j4x-757c: <a href="{{ request.args.get('next') }}">. A link with a fixed start
119
+ # (href="/search?q={{ ... }}") can't change scheme, so only the start of the value counts.
120
+ metadata:
121
+ category: security
122
+ subcategory: [vuln]
123
+ type: vulnerability
124
+ title: Request data as a template's link or event handler
125
+ cwe: ["CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')"]
126
+ owasp: ["A05:2025 - Injection"]
127
+ references:
128
+ - https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html
129
+ - https://jinja.palletsprojects.com/en/stable/templates/#html-escaping
130
+ paths:
131
+ include: ["*.html", "*.htm", "*.jinja", "*.jinja2", "*.j2", "*.djhtml"]
132
+ pattern-either:
133
+ - pattern-regex: '(?i)\b(href|src|action|formaction|xlink:href)\s{0,5}=\s{0,5}["'']?\s{0,5}\{\{-?\s{0,5}request\.(args|GET|values|query_params|form|POST)\b'
134
+ - pattern-regex: '(?i)\bon[a-z]{3,20}\s{0,5}=\s{0,5}"[^"<>]{0,200}\{\{-?\s{0,5}request\.(args|GET|values|query_params|form|POST)\b'
135
+ - pattern-regex: '(?i)\bon[a-z]{3,20}\s{0,5}=\s{0,5}''[^''<>]{0,200}\{\{-?\s{0,5}request\.(args|GET|values|query_params|form|POST)\b'
136
+
137
+ - id: codewatch.templates.not-autoescaped
138
+ languages: [generic]
139
+ severity: WARNING
140
+ message: >-
141
+ Flask chooses autoescaping by extension: only .html, .htm, .xml, .xhtml and .svg templates are escaped, so a
142
+ .jinja, .jinja2 or .j2 template prints every value raw. If this app is Flask, HTML in a value here is rendered
143
+ as markup. Rename it to .html, or escape each value (|e) or sanitize it. Django's Jinja backend does
144
+ autoescape these extensions, so this is a hotspot for review rather than a definite flaw.
145
+ metadata:
146
+ category: security
147
+ subcategory: [audit]
148
+ type: security_hotspot
149
+ title: Template extension Flask does not autoescape
150
+ cwe: ["CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')"]
151
+ owasp: ["A05:2025 - Injection"]
152
+ references:
153
+ - https://flask.palletsprojects.com/en/stable/config/#TEMPLATES_AUTO_RELOAD
154
+ - https://flask.palletsprojects.com/en/stable/api/#flask.Flask.select_jinja_autoescape
155
+ - https://jinja.palletsprojects.com/en/stable/templates/#html-escaping
156
+ - https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html
157
+ paths:
158
+ include: ["*.jinja", "*.jinja2", "*.j2"]
159
+ # {{ name }}, but not a literal, a form field or a server value, and not an expression a filter escapes (|e,
160
+ # |escape, |tojson, |sanitize...); |safe is reported by codewatch.templates.unescaped-output.
161
+ pattern-regex: >-
162
+ \{\{-?\s{0,5}(?!(form|url_for|get_flashed_messages|config|loop|super|self|caller|csrf_token)\b)[A-Za-z_][\w.]*(?![^{}\n]{0,200}\|\s{0,5}(e|escape|safe|tojson|sanitize\w*|clean\w*|bleach\w*|striptags|int|float|length|count|urlencode|filesizeformat)\b)
163
+
164
+ - id: codewatch.django.allauth-microsoft-any-tenant
165
+ languages: [python]
166
+ severity: WARNING
167
+ message: >-
168
+ django-allauth's Microsoft provider signs in accounts from any Entra tenant here ("common", "organizations"
169
+ or "consumers"; "common" is also what it uses when no tenant is set). Anyone with a Microsoft account in
170
+ another organisation can sign in, and with SOCIALACCOUNT_AUTO_SIGNUP gets an account. Set the tenant to
171
+ your directory's ID, or reject other tenants (the token's tid claim) in your social account adapter.
172
+ metadata:
173
+ category: security
174
+ subcategory: [audit]
175
+ type: security_hotspot
176
+ title: Microsoft sign-in open to any Entra tenant
177
+ cwe: ["CWE-284: Improper Access Control"]
178
+ owasp: ["A01:2025 - Broken Access Control"]
179
+ references:
180
+ - https://docs.allauth.org/en/latest/socialaccount/providers/microsoft.html
181
+ - https://learn.microsoft.com/en-us/entra/identity-platform/howto-convert-app-to-be-multi-tenant
182
+ pattern-either:
183
+ - patterns:
184
+ - pattern-inside: SOCIALACCOUNT_PROVIDERS = {...}
185
+ - pattern: '{..., $KEY: $VALUE, ...}'
186
+ - metavariable-regex: {metavariable: $KEY, regex: '^[rbu]*["''](tenant|TENANT)["'']$'}
187
+ - metavariable-regex: {metavariable: $VALUE, regex: '^[rbu]*["''](common|organizations|consumers)["'']$'}
188
+ - focus-metavariable: $VALUE
189
+ - patterns:
190
+ - pattern-inside: SOCIALACCOUNT_PROVIDERS = {...}
191
+ - pattern: '{..., $PROVIDER: $CONFIG, ...}'
192
+ - metavariable-regex: {metavariable: $PROVIDER, regex: '^[rbu]*["'']microsoft["'']$'}
193
+ - metavariable-regex: {metavariable: $CONFIG, regex: '^(?![\s\S]*["''](tenant|TENANT)["''])\{'}
194
+ - focus-metavariable: $PROVIDER
195
+
196
+ - id: codewatch.django.allauth-trusts-provider-email
197
+ languages: [python]
198
+ severity: WARNING
199
+ message: >-
200
+ django-allauth is told to trust email addresses from a social login: VERIFIED_EMAIL treats every address a
201
+ provider sends as verified, and SOCIALACCOUNT_EMAIL_AUTHENTICATION signs a social login into the local
202
+ account with the same address. Together, anyone who can make a provider send someone else's address signs in
203
+ as them (Microsoft Entra's email claim can be set by the admin of any tenant: "nOAuth"). Leave both off, or
204
+ match accounts on the provider's immutable ID (Entra's oid/sub) instead.
205
+ metadata:
206
+ category: security
207
+ subcategory: [audit]
208
+ type: security_hotspot
209
+ title: Social sign-in trusts the provider's email address
210
+ cwe: ["CWE-287: Improper Authentication"]
211
+ owasp: ["A07:2025 - Authentication Failures"]
212
+ references:
213
+ - https://docs.allauth.org/en/latest/socialaccount/configuration.html
214
+ - https://www.descope.com/blog/post/noauth
215
+ pattern-either:
216
+ - patterns:
217
+ - pattern-inside: SOCIALACCOUNT_PROVIDERS = {...}
218
+ - pattern: '{..., $KEY: True, ...}'
219
+ - metavariable-regex: {metavariable: $KEY, regex: '^[rbu]*["'']VERIFIED_EMAIL["'']$'}
220
+ - focus-metavariable: $KEY
221
+ - pattern: SOCIALACCOUNT_EMAIL_AUTHENTICATION = True
222
+
223
+ - id: codewatch.python.csp-allows-inline-script
224
+ languages: [python]
225
+ severity: WARNING
226
+ message: >-
227
+ This Content-Security-Policy lets scripts run inline ('unsafe-inline') or from strings ('unsafe-eval'), so
228
+ it no longer stops injected script: the policy's main protection against XSS is off. Use nonces or hashes
229
+ for inline scripts ('unsafe-inline' is ignored next to a nonce or hash, so keeping it there as a fallback
230
+ for old browsers is fine) and remove eval-style code.
231
+ metadata:
232
+ category: security
233
+ subcategory: [audit]
234
+ type: security_hotspot
235
+ title: Content-Security-Policy allows inline or eval'd script
236
+ cwe: ["CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')"]
237
+ owasp: ["A02:2025 - Security Misconfiguration"]
238
+ references:
239
+ - https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src
240
+ - https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html
241
+ pattern-either:
242
+ # A policy written out as a header value.
243
+ - patterns:
244
+ - pattern: '"$POLICY"'
245
+ - metavariable-regex:
246
+ metavariable: $POLICY
247
+ regex: '(?i)(.*;)?\s*(script-src|script-src-elem|default-src)\s[^;]*''unsafe-(inline|eval)'''
248
+ # 'unsafe-inline' next to a nonce or hash is ignored by browsers that know them (a fallback).
249
+ - metavariable-regex:
250
+ metavariable: $POLICY
251
+ regex: '(?is)^(?!.*(script-src|default-src)\s[^;]*''(nonce-|sha(256|384|512)-|strict-dynamic))|.*''unsafe-eval'''
252
+ # Directives kept as lists (django-csp's CSP_SCRIPT_SRC and CONTENT_SECURITY_POLICY, Django 6's SECURE_CSP,
253
+ # a project's own dict) with an unsafe source among them.
254
+ - patterns:
255
+ - pattern-either:
256
+ - pattern: '$SETTING = [..., "$SOURCE", ...]'
257
+ - pattern: '$SETTING = (..., "$SOURCE", ...)'
258
+ - pattern: '{..., $SETTING: [..., "$SOURCE", ...], ...}'
259
+ - pattern: '{..., $SETTING: (..., "$SOURCE", ...), ...}'
260
+ - pattern: '$D[$SETTING].append("$SOURCE")'
261
+ # The scanned copy writes a constant key as an attribute: d["script-src"] -> d.k_script_src.
262
+ - pattern: '$D.$SETTING.append("$SOURCE")'
263
+ - pattern: '$D[$SETTING] += [..., "$SOURCE", ...]'
264
+ - metavariable-regex: {metavariable: $SETTING, regex: '(?i)^[rbu]*["'']?(CSP_|k_)?(script[-_]src(_elem|[-_]elem)?|default[-_]src)["'']?$'}
265
+ - metavariable-regex: {metavariable: $SOURCE, regex: '^''unsafe-(inline|eval)''$'}
266
+ - pattern-not: '$SETTING = [..., "=~/^.(nonce-|sha(256|384|512)-|strict-dynamic)/", ...]'
267
+ - pattern-not: '{..., $SETTING: [..., "=~/^.(nonce-|sha(256|384|512)-|strict-dynamic)/", ...], ...}'
@@ -0,0 +1,115 @@
1
+ # codewatch built-in rules for Python injection flaws visible in the code itself, wherever the data comes from:
2
+ # a value spliced into a query or archive path that should have been bound or checked.
3
+ rules:
4
+ - id: codewatch.python.sql-value-formatting
5
+ languages: [python]
6
+ severity: ERROR
7
+ message: >-
8
+ A value is formatted into the SQL text where the query compares or stores it, so a quote in it changes the
9
+ query (SQL injection). Pass it as a parameter instead: cursor.execute("... WHERE name = %s", (name,)).
10
+ metadata:
11
+ category: security
12
+ subcategory: [vuln]
13
+ type: vulnerability
14
+ title: Value formatted into SQL instead of bound
15
+ cwe: ["CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')"]
16
+ owasp: ["A05:2025 - Injection"]
17
+ references:
18
+ - https://peps.python.org/pep-0249/#paramstyle
19
+ - https://cheatsheetseries.owasp.org/cheatsheets/Query_Parameterization_Cheat_Sheet.html
20
+ # Only values inside SQL quotes, which are always data that should have been bound: table and column names
21
+ # can't be bound, and an unquoted value is often a number (the taint rules cover request data there).
22
+ # Migrations rewrite the app's own rows, run by an operator: Alembic's upgrade()/downgrade() and the
23
+ # run_migrations_* functions of its env.py (dispatch), and Django's migrations folders.
24
+ paths:
25
+ exclude: ["**/migrations/**"]
26
+ patterns:
27
+ - pattern-not-inside: |
28
+ def upgrade(...):
29
+ ...
30
+ - pattern-not-inside: |
31
+ def downgrade(...):
32
+ ...
33
+ - pattern-not-inside: |
34
+ def run_migrations_online(...):
35
+ ...
36
+ - pattern-not-inside: |
37
+ def run_migrations_offline(...):
38
+ ...
39
+ # A value checked to hold no quote can't close the one around it (CTFd: `if "'" not in table_name`).
40
+ - pattern-not-inside: |
41
+ if <... "'" not in $CHECKED ...>:
42
+ ...
43
+ - pattern-either:
44
+ - patterns:
45
+ - pattern-either:
46
+ - pattern: $CUR.$EXEC("$SQL" % $ARGS, ...)
47
+ - pattern: $CUR.$EXEC("$SQL".format(...), ...)
48
+ - patterns:
49
+ - pattern: $CUR.$EXEC($QUERY, ...)
50
+ - pattern-either:
51
+ - pattern-inside: |
52
+ $QUERY = "$SQL" % $ARGS
53
+ ...
54
+ - pattern-inside: |
55
+ $QUERY = "$SQL".format(...)
56
+ ...
57
+ # A model's own metadata (wger: relname = '%s' % self.model._meta.db_table) is the server's.
58
+ - pattern-not: $CUR.$EXEC("$SQL" % $MODEL._meta.$ATTR, ...)
59
+ - pattern-not-inside: |
60
+ $QUERY = "$SQL" % $MODEL._meta.$ATTR
61
+ ...
62
+ - metavariable-regex: {metavariable: $EXEC, regex: ^(execute|executemany|executescript|raw)$}
63
+ - metavariable-regex:
64
+ metavariable: $SQL
65
+ regex: (?is).*['"](%s|\{[^}]{0,40}\})['"].*
66
+ - patterns:
67
+ - pattern-either:
68
+ - pattern: $CUR.$EXEC($QUERY, ...)
69
+ - patterns:
70
+ - pattern: $CUR.$EXEC($VAR, ...)
71
+ - pattern-inside: |
72
+ $VAR = $QUERY
73
+ ...
74
+ - metavariable-regex: {metavariable: $EXEC, regex: ^(execute|executemany|executescript|raw)$}
75
+ - metavariable-pattern:
76
+ metavariable: $QUERY
77
+ patterns:
78
+ - pattern: f"..."
79
+ # Not an upper-case constant (dispatch: set search_path to "{CORE_SCHEMA_NAME}", an identifier).
80
+ - pattern-regex: (?s)['"]\{(?![A-Z][A-Z0-9_]*\})[^}]{1,80}\}['"]
81
+
82
+ - id: codewatch.python.tar-extraction-without-filter
83
+ languages: [python]
84
+ severity: ERROR
85
+ message: >-
86
+ Tar members are extracted without a filter, so an archive with "../" paths, absolute paths or links can
87
+ write outside the target directory (tar slip). Pass filter="data" to extract()/extractall().
88
+ metadata:
89
+ category: security
90
+ subcategory: [vuln]
91
+ type: vulnerability
92
+ title: Archive extracted without a path filter
93
+ cwe: ["CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')"]
94
+ owasp: ["A01:2025 - Broken Access Control"]
95
+ references:
96
+ - https://docs.python.org/3/library/tarfile.html#tarfile-extraction-filter
97
+ patterns:
98
+ - pattern-either:
99
+ - pattern: $TAR.extractall(...)
100
+ - pattern: $TAR.extract(...)
101
+ - pattern-either:
102
+ - pattern-inside: |
103
+ with tarfile.open(...) as $TAR:
104
+ ...
105
+ - pattern-inside: |
106
+ $TAR = tarfile.open(...)
107
+ ...
108
+ - pattern-inside: |
109
+ with tarfile.TarFile(...) as $TAR:
110
+ ...
111
+ - pattern-inside: |
112
+ $TAR = tarfile.TarFile(...)
113
+ ...
114
+ - pattern-not: $TAR.extractall(..., filter=$F, ...)
115
+ - pattern-not: $TAR.extract(..., filter=$F, ...)