Closed dcolley closed 3 years ago
Just use whitelisted utility function for stripping HTML? it's kinda hard to automatically detect this.
So the reason is you've not created a correct JSON string, there's not much Framework can do about this. Even if we tried to escape it, it's still ugly HTML. Best option for you is to strip HTML. There are strip_html
, html2text
and escape_html
whitelisted utility methods.
@ankush thanks for your answer. Is there any documentation on how to use these methods please?
I tried the following:
{
"name": "{{doc.name}}",
...
"is_milestone": "{{doc.is_milestone}}",
"description": "{{strip_html(doc.description)}}",
"depends_on": "{{doc.depends_on}}",
...
}
But the method is not found
Traceback (most recent call last):
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/jinja.py", line 86, in render_template
return get_jenv().from_string(template).render(context)
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
self.environment.handle_exception()
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "<template>", line 26, in top-level template code
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/sandbox.py", line 460, in call
if not __self.is_safe_callable(__obj):
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/sandbox.py", line 360, in is_safe_callable
getattr(obj, "unsafe_callable", False) or getattr(obj, "alters_data", False)
jinja2.exceptions.UndefinedError: 'strip_html' is undefined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/background_jobs.py", line 104, in execute_job
method(**kwargs)
File "/home/frappe/frappe-bench/apps/frappe/frappe/integrations/doctype/webhook/webhook.py", line 83, in enqueue_webhook
data = get_webhook_data(doc, webhook)
File "/home/frappe/frappe-bench/apps/frappe/frappe/integrations/doctype/webhook/webhook.py", line 143, in get_webhook_data
data = frappe.render_template(webhook.webhook_json, get_context(doc))
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/jinja.py", line 88, in render_template
throw(title="Jinja Template Error", msg="<pre>{template}</pre><pre>{tb}</pre>".format(template=template, tb=get_traceback()))
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 438, in throw
msgprint(msg, raise_exception=exc, title=title, indicator='red', is_minimizable=is_minimizable, wide=wide, as_list=as_list)
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 417, in msgprint
_raise_exception()
File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 371, in _raise_exception
raise raise_exception(msg)
frappe.exceptions.ValidationError: <pre>{
"name": "{{doc.name}}",
"id": "{{doc.id}}",
...
"description": "{{strip_html(doc.description)}}",
...
"rgt": "{{doc.rgt}}",
"old_parent": "{{doc.old_parent}}"
}</pre><pre>Traceback (most recent call last):
File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/jinja.py", line 86, in render_template
return get_jenv().from_string(template).render(context)
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
self.environment.handle_exception()
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "<template>", line 26, in top-level template code
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/sandbox.py", line 460, in call
if not __self.is_safe_callable(__obj):
File "/home/frappe/frappe-bench/env/lib/python3.7/site-packages/jinja2/sandbox.py", line 360, in is_safe_callable
getattr(obj, "unsafe_callable", False) or getattr(obj, "alters_data", False)
jinja2.exceptions.UndefinedError: 'strip_html' is undefined
</pre>
try frappe.utils.strip_html
Description of the issue
Webhook that triggers
Task
fails withdescription
fieldContext information (for bug reports)
Output of
bench version
n/a
Steps to reproduce the issue
{"description": "{{doc.description}}"}
in the message formatObserved result
Expected result
No error, webhook should send data successfully
Stacktrace / full error message
Additional information
using all the other fields works
I can call the API to get the task details: GET https://my-company.frappe.cloud/api/resource/Task/TASK-2021-00004/
So, I suspect the problem is with escaping the HTML text in the description
OS version / distribution,
ERPNext
install method, etc.Frappe.cloud