Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape variables in mkdocs data #5759

Merged
merged 3 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions readthedocs/core/templatetags/core_tags.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-

"""Template tags for core app."""

import hashlib
import json
from urllib.parse import urlencode

from django import template
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from django.utils.encoding import force_bytes, force_text
from django.utils.safestring import mark_safe

Expand Down Expand Up @@ -112,3 +112,29 @@ def key(d, key_name):
@register.simple_tag
def readthedocs_version():
return __version__


@register.filter
def escapejson(data, indent=None):
"""
Escape JSON correctly for inclusion in Django templates

This code was mostly taken from Django's implementation
https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#json-script
stsewd marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/django/django/blob/2.2.2/django/utils/html.py#L74-L92

After upgrading to Django 2.1+, we could replace this with Django's implementation
although the inputs and outputs are a bit different.

Example:

var jsvar = {{ dictionary_value | escapejson }}
"""
if indent:
indent = int(indent)
_json_script_escapes = {
ord('>'): '\\u003E',
ord('<'): '\\u003C',
ord('&'): '\\u0026',
}
return mark_safe(json.dumps(data, cls=DjangoJSONEncoder, indent=indent).translate(_json_script_escapes))
4 changes: 2 additions & 2 deletions readthedocs/doc_builder/backends/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
'global_analytics_code': settings.GLOBAL_ANALYTICS_CODE,
'user_analytics_code': analytics_code,
}
data_json = json.dumps(readthedocs_data, indent=4)

data_ctx = {
'data_json': data_json,
'readthedocs_data': readthedocs_data,
'current_version': readthedocs_data['version'],
'slug': readthedocs_data['project'],
'html_theme': readthedocs_data['theme'],
Expand Down
13 changes: 8 additions & 5 deletions readthedocs/doc_builder/templates/doc_builder/data.js.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var READTHEDOCS_DATA = {{ data_json|safe }}
{% load core_tags %}


var READTHEDOCS_DATA = {{ readthedocs_data|escapejson:4 }}

// Old variables
var doc_version = "{{ current_version }}";
var doc_slug = "{{ slug }}";
var page_name = "{{ pagename }}";
var html_theme = "{{ html_theme }}";
var doc_version = "{{ current_version|escapejs }}";
var doc_slug = "{{ slug|escapejs }}";
var page_name = "{{ pagename|escapejs }}";
var html_theme = "{{ html_theme|escapejs }}";
Copy link
Member

@stsewd stsewd Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think " are inserted by the filter (didn't tested it).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the django code, looks like they don't include "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure where you're looking but it is included as far as I can tell.

>>> from django.template.defaultfilters import escapejs_filter
>>> escapejs_filter('"')
'\\u0022'

https://github.com/django/django/blob/1.11/django/utils/html.py#L54-L76

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the " surrounding the variable. In jinja you just need to put var foo = {{ bar|tojson }}. Django needs var foo = "{{ bar|escapejs }"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Correct. That's different.


// mkdocs_page_input_path is only defined on the RTD mkdocs theme but it isn't
// available on all pages (e.g. missing in search result)
Expand Down
11 changes: 11 additions & 0 deletions readthedocs/rtd_tests/tests/test_core_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,14 @@ def test_restructured_text_invalid(self):
)
result = core_tags.restructuredtext(value)
self.assertEqual(result, value)

def test_escapejson(self):
tests = (
({}, '{}'),
({'a': 'b'}, '{"a": "b"}'),
({"'; //": '""'}, '{"\'; //": "\\"\\""}'),
({"<script>alert('hi')</script>": ''}, '{"\\u003Cscript\\u003Ealert(\'hi\')\\u003C/script\\u003E": ""}'),
)

for in_value, out_value in tests:
self.assertEqual(core_tags.escapejson(in_value), out_value)