Skip to content

Commit 854e483

Browse files
authored
Merge pull request #37 from frappe/security_rules
feat: Security rules
2 parents 80e2f1a + 3b45be4 commit 854e483

7 files changed

Lines changed: 110 additions & 13 deletions

File tree

rules/security.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

rules/security/authorization.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
rules:
2+
- id: relaxed-permissions
3+
patterns:
4+
- pattern: |
5+
{
6+
"role": "All",
7+
...
8+
}
9+
message: |
10+
Avoid using "All" role. It's available to every user, including website user.
11+
languages: [json]
12+
severity: WARNING
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
def function_name(input):
22
# ruleid: frappe-codeinjection-eval
33
eval(input)
4-
5-
# ok: frappe-codeinjection-eval
6-
eval("1 + 1")

rules/security/rce.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
rules:
2+
- id: frappe-codeinjection-eval
3+
patterns:
4+
- pattern-either:
5+
- pattern: eval(...)
6+
- pattern: exec(...)
7+
- pattern: safe_exec(...)
8+
- pattern: safe_eval(...)
9+
message: |
10+
Detected the use of functions that can be dangerous if used to evaluate
11+
dynamic content. This code should be manually audited by security team.
12+
languages: [python]
13+
severity: ERROR
14+
15+
- id: frappe-ssti
16+
patterns:
17+
- pattern: render_template($ARG, ...)
18+
message: |
19+
Detected the use of render_template, make sure $ARG comes from trusted
20+
source. This code should be audited by security team.
21+
languages: [python]
22+
severity: ERROR

rules/security/sql.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
rules:
2+
- id: frappe-sql-format-injection
3+
languages: [python]
4+
severity: INFO
5+
message: >-
6+
Detected use of '.format()' or f-string in a Frappe SQL call.
7+
This can lead to SQL injection. Use parameterized queries instead.
8+
patterns:
9+
- pattern-either:
10+
- pattern: frappe.db.sql("...".format(...), ...)
11+
- pattern: frappe.db.sql(f"...", ...)
12+
- pattern: frappe.db.multisql("...".format(...), ...)
13+
- pattern: frappe.db.multisql(f"...", ...)
14+
# Matches cases where the string is formatted beforehand
15+
- pattern: |
16+
$QUERY = "...".format(...)
17+
...
18+
frappe.db.sql($QUERY, ...)
19+
- pattern: |
20+
$QUERY = f"..."
21+
...
22+
frappe.db.sql($QUERY, ...)

rules/security/whitelisted.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Any
2+
import frappe
3+
4+
5+
6+
# ruleid: missing-argument-type-hint
7+
@frappe.whitelist()
8+
def function_name(inject, abc):
9+
pass
10+
11+
# ok: missing-argument-type-hint
12+
@frappe.whitelist()
13+
def function_name(inject: str, abc: str):
14+
pass
15+
16+
# ok: missing-argument-type-hint
17+
@frappe.whitelist()
18+
def function_name():
19+
pass
20+
21+
# ok: missing-argument-type-hint
22+
@frappe.whitelist()
23+
def function_name(abc: Any):
24+
pass
25+
26+
27+
# ruleid: missing-argument-type-hint
28+
@frappe.whitelist()
29+
def function_name(inject, abc: str): # only one typed
30+
pass

rules/security/whitelisted.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
rules:
2+
- id: missing-argument-type-hint
3+
languages: [python]
4+
severity: WARNING
5+
patterns:
6+
- pattern: |
7+
@frappe.whitelist(...)
8+
def $FUNC(..., $ARG, ...): ...
9+
- pattern-not: |
10+
@frappe.whitelist(...)
11+
def $FUNC(..., $ARG: $TYPE, ...): ...
12+
- metavariable-regex:
13+
metavariable: $ARG
14+
# Exclude 'self' and 'cls' as they typically don't take hints
15+
regex: ^(?!self$|cls$).*$
16+
message: "The argument '$ARG' in function '$FUNC' is missing a type hint."
17+
- id: guest-whitelisted-method
18+
languages: [python]
19+
severity: WARNING
20+
patterns:
21+
- pattern: |
22+
@frappe.whitelist(..., allow_guest=True, ...)
23+
def $FUNC(...): ...
24+
message: "Whitelisted method that's accesible to guest should be manually reviewed by security team."

0 commit comments

Comments
 (0)