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 cb3b67a
Show file tree
Hide file tree
Showing 3 changed files with 55 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")

29 changes: 29 additions & 0 deletions rules/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,32 @@ 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' in '$CLASS' crosses doctype boundaries.
Explicitly declare its extended security context with @frappe.requires_permission(<doctype>, <perm>).
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 cb3b67a

Please sign in to comment.