Skip to content

Commit

Permalink
feat: add doctype caster security rule
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Jul 4, 2024
1 parent 92e1241 commit d63d04a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions rules/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ def function_name(input):

# ok: frappe-codeinjection-eval
eval("1 + 1")

31 changes: 31 additions & 0 deletions rules/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,34 @@ rules:
dynamic content. Avoid it or use safe_eval().
languages: [python]
severity: ERROR

- id: require-permission-decorator-on-conversion-methods
patterns:
- pattern-inside: |
class $CLASS(...):
...
- pattern: |
def $CONVERSION_METHOD(...):
...
- pattern-not: |
@requires_permission(...)
def $CONVERSION_METHOD(...):
...
- pattern-not: |
@frappe.requires_permission(...)
def $CONVERSION_METHOD(...):
...
- metavariable-regex:
metavariable: '$CONVERSION_METHOD'
regex: '^_(from|into)_(.*)$'

message: |
Conversion method '$CONVERSION_METHOD' of class '$CLASS' spans the security boundary between two doctypes.
It therefore must declare its security context exiplictly and upfront.
Do this by setting at least one explicit @frappe.requires_permission(<doctype>, <perm>) decorator on '$CONVERSION_METHOD'.
languages: [python]
severity: ERROR
paths:
include:
- "*/**/doctype/*"

25 changes: 25 additions & 0 deletions rules/some/doctype/security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from frappe.model import Document
import frappe
from frappe import requires_permission

# ruleid: require-permission-decorator-on-conversion-methods
class MyDocument(Document):
def _into_sales_invoice(self, so):
...

# ok: require-permission-decorator-on-conversion-methods
class MyDocument(Document):
@requires_permission("Sales Invoice", "create")
def _into_sales_invoice(self, so):
...

# ruleid: require-permission-decorator-on-conversion-methods
class MyDocument(Document):
def _from_sales_invoice(self, so):
...

# ok: require-permission-decorator-on-conversion-methods
class MyDocument(Document):
@frappe.requires_permission("Sales Invoice", "read")
def _from_sales_invoice(self, so):
...

0 comments on commit d63d04a

Please sign in to comment.