Skip to content

Correctly escape variables when templating GraphiQL response #97

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions graphql_server/render_graphiql.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ def escape_js_value(value: Any) -> Any:


def process_var(template: str, name: str, value: Any, jsonify=False) -> str:
pattern = r"{{\s*" + name + r"(\s*|[^}]+)*\s*}}"
pattern = r"{{\s*" + re.escape(name) + r"(\s*|[^}]+)*\s*}}"
if jsonify and value not in ["null", "undefined"]:
value = json.dumps(value)
value = escape_js_value(value)

return re.sub(pattern, value, template)
return re.sub(pattern, re.escape(value), template)


def simple_renderer(template: str, **values: Dict[str, Any]) -> str:
Expand Down
12 changes: 12 additions & 0 deletions tests/flask/test_graphiqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def test_graphiql_renders_pretty(app, client):

assert pretty_response in response.data.decode("utf-8")

def test_graphiql_renders_umlauts_pretty(app, client):
with app.test_request_context():
response = client.get(
url_for(
"graphql",
query="query helloWho($who: String){ test(who: $who) }",
variables='{"who": "Björn"}',
), headers={"Accept": "text/html"}
)
assert response.status_code == 200
assert "Hello\\ Bj\\\\u00f6rn" in response.data.decode("utf-8")


def test_graphiql_default_title(app, client):
with app.test_request_context():
Expand Down