Skip to content

Commit efe89f2

Browse files
Merge pull request frappe#13392 from frappe/v12-pre-release
2 parents a27335f + 22dd3bf commit efe89f2

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

frappe/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
reload(sys)
2424
sys.setdefaultencoding("utf-8")
2525

26-
__version__ = '12.18.0'
26+
__version__ = '12.18.1'
2727
__title__ = "Frappe Framework"
2828

2929
local = Local()
@@ -1559,6 +1559,23 @@ def safe_eval(code, eval_globals=None, eval_locals=None):
15591559
"round": round
15601560
}
15611561

1562+
UNSAFE_ATTRIBUTES = {
1563+
# Generator Attributes
1564+
"gi_frame", "gi_code",
1565+
# Coroutine Attributes
1566+
"cr_frame", "cr_code", "cr_origin",
1567+
# Async Generator Attributes
1568+
"ag_code", "ag_frame",
1569+
# Traceback Attributes
1570+
"tb_frame", "tb_next",
1571+
# Format Attributes
1572+
"format", "format_map",
1573+
}
1574+
1575+
for attribute in UNSAFE_ATTRIBUTES:
1576+
if attribute in code:
1577+
throw('Illegal rule {0}. Cannot use "{1}"'.format(bold(code), attribute))
1578+
15621579
if '__' in code:
15631580
throw('Illegal rule {0}. Cannot use "__"'.format(bold(code)))
15641581

frappe/utils/safe_exec.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def get_safe_globals():
4444

4545
out = frappe._dict(
4646
# make available limited methods of frappe
47-
json = json,
47+
json = frappe._dict(
48+
loads = json.loads,
49+
dumps = json.dumps),
4850
dict = dict,
4951
frappe = frappe._dict(
5052
flags = frappe._dict(),
@@ -119,6 +121,7 @@ def get_safe_globals():
119121
# default writer allows write access
120122
out._write_ = _write
121123
out._getitem_ = _getitem
124+
out._getattr_ = _getattr
122125

123126
# allow iterators and list comprehension
124127
out._getiter_ = iter
@@ -134,6 +137,27 @@ def _getitem(obj, key):
134137
raise SyntaxError('Key starts with _')
135138
return obj[key]
136139

140+
def _getattr(object, name, default=None):
141+
# guard function for RestrictedPython
142+
# allow any key to be accessed as long as
143+
# 1. it does not start with an underscore (safer_getattr)
144+
# 2. it is not an UNSAFE_ATTRIBUTES
145+
146+
UNSAFE_ATTRIBUTES = {
147+
# Generator Attributes
148+
"gi_frame", "gi_code",
149+
# Coroutine Attributes
150+
"cr_frame", "cr_code", "cr_origin",
151+
# Async Generator Attributes
152+
"ag_code", "ag_frame",
153+
# Traceback Attributes
154+
"tb_frame", "tb_next",
155+
}
156+
157+
if isinstance(name, str) and (name in UNSAFE_ATTRIBUTES):
158+
raise SyntaxError("{name} is an unsafe attribute".format(name=name))
159+
return RestrictedPython.Guards.safer_getattr(object, name, default=default)
160+
137161
def _write(obj):
138162
# guard function for RestrictedPython
139163
# allow writing to any object
@@ -255,4 +279,4 @@ def add_module_properties(module, data, filter_method):
255279
"md_to_html",
256280
"is_subset",
257281
"generate_hash"
258-
)
282+
)

0 commit comments

Comments
 (0)