Skip to content

Commit

Permalink
feat(python): add sensitive data in cookie rule for Python http lib (…
Browse files Browse the repository at this point in the history
…CWE-315)
  • Loading branch information
elsapet committed May 6, 2024
1 parent 336b73e commit 276cc36
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
45 changes: 45 additions & 0 deletions rules/python/lang/cookies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
imports:
- python_shared_lang_datatype
patterns:
- pattern: $<COOKIE_INIT>[$<_>] = $<DATA_TYPE>
filters:
- variable: COOKIE_INIT
detection: python_lang_cookies_init
scope: cursor
- variable: DATA_TYPE
detection: python_shared_lang_datatype
scope: result
auxiliary:
- id: python_lang_cookies_init
patterns:
- pattern: $<COOKIE>()
filters:
- variable: COOKIE
regex: \A(http\.)?(cookies\.)?(Simple|Base)Cookie\z
languages:
- python
severity: high
metadata:
description: Leakage of sensitive data in cookie
remediation_message: |-
## Description
Storing sensitive data in cookies can lead to a data breach. This vulnerability occurs when sensitive information is stored in browser cookies, putting it at risk of unauthorized access.
## Remediations
- **Do not** store sensitive data in unencrypted cookies. This practice can expose sensitive information to potential security threats.
```python
HttpResponse.set_cookie("user", "[email protected]", ...) # unsafe
```
- **Do** use encrypted cookies to protect sensitive data stored in cookies.
```python
HttpResponse.set_signed_cookie("user", "[email protected]", ...)
```
## References
cwe_id:
- 315
id: python_lang_cookies
documentation_url: https://docs.bearer.com/reference/rules/python_lang_cookies
20 changes: 20 additions & 0 deletions tests/python/lang/cookies/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const {
createNewInvoker,
getEnvironment,
} = require("../../../helper.js")
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname)

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

test("cookies", () => {
const testCase = "main.py"

const results = invoke(testCase)

expect(results).toEqual({
Missing: [],
Extra: []
})
})
})
16 changes: 16 additions & 0 deletions tests/python/lang/cookies/testdata/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from http import cookies

def bad(user):
myCookie = cookies.SimpleCookie()
# bearer:expected python_lang_cookies
myCookie["user"] = user.email

def bad2(customer):
myBasicCookie = http.cookies.BaseCookie()
# bearer:expected python_lang_cookies
myBasicCookie["logged customer"] = customer.email

def ok(user):
safeCookie = cookies.SimpleCookie()
safeCookie["user"] = "current"

0 comments on commit 276cc36

Please sign in to comment.