Skip to content

Commit 58e7986

Browse files
committed
Don't put queries in the querystring
The querystring will be logged to webserver logs, so is not suitable for sensitive data. This does not remove the ability to do queries over GET, but does remove GraphiQL doing it by default, which is unsafe.
1 parent 9351626 commit 58e7986

File tree

2 files changed

+10
-74
lines changed

2 files changed

+10
-74
lines changed

graphene_django/templates/graphene/graphiql.html

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,13 @@
2424
</head>
2525
<body>
2626
<script>
27+
2728
// Parse the cookie value for a CSRF token
2829
var csrftoken;
2930
var cookies = ('; ' + document.cookie).split('; csrftoken=');
3031
if (cookies.length == 2)
3132
csrftoken = cookies.pop().split(';').shift();
3233

33-
// Collect the URL parameters
34-
var parameters = {};
35-
window.location.search.substr(1).split('&').forEach(function (entry) {
36-
var eq = entry.indexOf('=');
37-
if (eq >= 0) {
38-
parameters[decodeURIComponent(entry.slice(0, eq))] =
39-
decodeURIComponent(entry.slice(eq + 1));
40-
}
41-
});
42-
// Produce a Location query string from a parameter object.
43-
function locationQuery(params) {
44-
return '?' + Object.keys(params).map(function (key) {
45-
return encodeURIComponent(key) + '=' +
46-
encodeURIComponent(params[key]);
47-
}).join('&');
48-
}
49-
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
50-
var graphqlParamNames = {
51-
query: true,
52-
variables: true,
53-
operationName: true
54-
};
55-
var otherParams = {};
56-
for (var k in parameters) {
57-
if (parameters.hasOwnProperty(k) && graphqlParamNames[k] !== true) {
58-
otherParams[k] = parameters[k];
59-
}
60-
}
61-
var fetchURL = locationQuery(otherParams);
6234
// Defines a GraphQL fetcher using the fetch API.
6335
function graphQLFetcher(graphQLParams) {
6436
var headers = {
@@ -68,7 +40,7 @@
6840
if (csrftoken) {
6941
headers['X-CSRFToken'] = csrftoken;
7042
}
71-
return fetch(fetchURL, {
43+
return fetch('', {
7244
method: 'post',
7345
headers: headers,
7446
body: JSON.stringify(graphQLParams),
@@ -83,41 +55,13 @@
8355
}
8456
});
8557
}
86-
// When the query and variables string is edited, update the URL bar so
87-
// that it can be easily shared.
88-
function onEditQuery(newQuery) {
89-
parameters.query = newQuery;
90-
updateURL();
91-
}
92-
function onEditVariables(newVariables) {
93-
parameters.variables = newVariables;
94-
updateURL();
95-
}
96-
function onEditOperationName(newOperationName) {
97-
parameters.operationName = newOperationName;
98-
updateURL();
99-
}
100-
function updateURL() {
101-
history.replaceState(null, null, locationQuery(parameters));
102-
}
58+
10359
// Render <GraphiQL /> into the body.
10460
ReactDOM.render(
105-
React.createElement(GraphiQL, {
106-
fetcher: graphQLFetcher,
107-
onEditQuery: onEditQuery,
108-
onEditVariables: onEditVariables,
109-
onEditOperationName: onEditOperationName,
110-
query: '{{ query|escapejs }}',
111-
response: '{{ result|escapejs }}',
112-
{% if variables %}
113-
variables: '{{ variables|escapejs }}',
114-
{% endif %}
115-
{% if operation_name %}
116-
operationName: '{{ operation_name|escapejs }}',
117-
{% endif %}
118-
}),
61+
React.createElement(GraphiQL, {fetcher: graphQLFetcher}),
11962
document.body
12063
);
64+
12165
</script>
12266
</body>
12367
</html>

graphene_django/views.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def dispatch(self, request, *args, **kwargs):
124124
data = self.parse_body(request)
125125
show_graphiql = self.graphiql and self.can_display_graphiql(request, data)
126126

127+
if show_graphiql:
128+
return self.render_graphiql(
129+
request, graphiql_version=self.graphiql_version
130+
)
131+
127132
if self.batch:
128133
responses = [self.get_response(request, entry) for entry in data]
129134
result = "[{}]".format(
@@ -137,19 +142,6 @@ def dispatch(self, request, *args, **kwargs):
137142
else:
138143
result, status_code = self.get_response(request, data, show_graphiql)
139144

140-
if show_graphiql:
141-
query, variables, operation_name, id = self.get_graphql_params(
142-
request, data
143-
)
144-
return self.render_graphiql(
145-
request,
146-
graphiql_version=self.graphiql_version,
147-
query=query or "",
148-
variables=json.dumps(variables) or "",
149-
operation_name=operation_name or "",
150-
result=result or "",
151-
)
152-
153145
return HttpResponse(
154146
status=status_code, content=result, content_type="application/json"
155147
)

0 commit comments

Comments
 (0)