Skip to content

Commit 057b491

Browse files
authored
GraphiQL cleanup (#1002)
* Add integrity checks for GraphiQL CDN resources Also fixes an erroneous assignment preventing a setting from getting to the UI. * Pass SRIs and new versions to the template * Update hashes * Use SRI-stable artifacts for GraphiQL resources
1 parent 6aa6aaa commit 057b491

File tree

3 files changed

+84
-37
lines changed

3 files changed

+84
-37
lines changed

graphene_django/static/graphene_django/graphiql.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
11
(function (
22
document,
3+
34
GRAPHENE_SETTINGS,
45
GraphiQL,
56
React,
67
ReactDOM,
78
SubscriptionsTransportWs,
9+
fetch,
810
history,
911
location,
1012
) {
1113
// Parse the cookie value for a CSRF token
1214
var csrftoken;
13-
var cookies = ('; ' + document.cookie).split('; csrftoken=');
15+
var cookies = ("; " + document.cookie).split("; csrftoken=");
1416
if (cookies.length == 2) {
15-
csrftoken = cookies.pop().split(';').shift();
17+
csrftoken = cookies.pop().split(";").shift();
1618
} else {
1719
csrftoken = document.querySelector("[name=csrfmiddlewaretoken]").value;
1820
}
1921

2022
// Collect the URL parameters
2123
var parameters = {};
22-
location.hash.substr(1).split('&').forEach(function (entry) {
23-
var eq = entry.indexOf('=');
24-
if (eq >= 0) {
25-
parameters[decodeURIComponent(entry.slice(0, eq))] =
26-
decodeURIComponent(entry.slice(eq + 1));
27-
}
28-
});
24+
location.hash
25+
.substr(1)
26+
.split("&")
27+
.forEach(function (entry) {
28+
var eq = entry.indexOf("=");
29+
if (eq >= 0) {
30+
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(
31+
entry.slice(eq + 1),
32+
);
33+
}
34+
});
2935
// Produce a Location fragment string from a parameter object.
3036
function locationQuery(params) {
31-
return '#' + Object.keys(params).map(function (key) {
32-
return encodeURIComponent(key) + '=' +
33-
encodeURIComponent(params[key]);
34-
}).join('&');
37+
return (
38+
"#" +
39+
Object.keys(params)
40+
.map(function (key) {
41+
return (
42+
encodeURIComponent(key) + "=" + encodeURIComponent(params[key])
43+
);
44+
})
45+
.join("&")
46+
);
3547
}
3648
// Derive a fetch URL from the current URL, sans the GraphQL parameters.
3749
var graphqlParamNames = {
3850
query: true,
3951
variables: true,
40-
operationName: true
52+
operationName: true,
4153
};
4254
var otherParams = {};
4355
for (var k in parameters) {
@@ -51,26 +63,28 @@
5163
// Defines a GraphQL fetcher using the fetch API.
5264
function httpClient(graphQLParams) {
5365
var headers = {
54-
'Accept': 'application/json',
55-
'Content-Type': 'application/json'
66+
Accept: "application/json",
67+
"Content-Type": "application/json",
5668
};
5769
if (csrftoken) {
58-
headers['X-CSRFToken'] = csrftoken;
70+
headers["X-CSRFToken"] = csrftoken;
5971
}
6072
return fetch(fetchURL, {
61-
method: 'post',
73+
method: "post",
6274
headers: headers,
6375
body: JSON.stringify(graphQLParams),
64-
credentials: 'include',
65-
}).then(function (response) {
66-
return response.text();
67-
}).then(function (responseBody) {
68-
try {
69-
return JSON.parse(responseBody);
70-
} catch (error) {
71-
return responseBody;
72-
}
73-
});
76+
credentials: "include",
77+
})
78+
.then(function (response) {
79+
return response.text();
80+
})
81+
.then(function (responseBody) {
82+
try {
83+
return JSON.parse(responseBody);
84+
} catch (error) {
85+
return responseBody;
86+
}
87+
});
7488
}
7589

7690
// Derive the subscription URL. If the SUBSCRIPTION_URL setting is specified, uses that value. Otherwise
@@ -157,7 +171,7 @@
157171
onEditVariables: onEditVariables,
158172
onEditOperationName: onEditOperationName,
159173
query: parameters.query,
160-
}
174+
};
161175
if (parameters.variables) {
162176
options.variables = parameters.variables;
163177
}
@@ -167,15 +181,17 @@
167181
// Render <GraphiQL /> into the body.
168182
ReactDOM.render(
169183
React.createElement(GraphiQL, options),
170-
document.getElementById("editor")
184+
document.getElementById("editor"),
171185
);
172186
})(
173187
document,
188+
174189
window.GRAPHENE_SETTINGS,
175190
window.GraphiQL,
176191
window.React,
177192
window.ReactDOM,
178193
window.SubscriptionsTransportWs,
194+
window.fetch,
179195
window.history,
180196
window.location,
181197
);

graphene_django/templates/graphene/graphiql.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717
width: 100%;
1818
}
1919
</style>
20-
<link href="https://cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.css"
20+
<link href="https://cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.min.css"
21+
integrity="{{graphiql_css_sri}}"
2122
rel="stylesheet"
2223
crossorigin="anonymous" />
23-
<script src="https://cdn.jsdelivr.net/npm/whatwg-fetch@2.0.3/fetch.min.js"
24-
integrity="sha384-dcF7KoWRaRpjcNbVPUFgatYgAijf8DqW6NWuqLdfB5Sb4Cdbb8iHX7bHsl9YhpKa"
24+
<script src="https://cdn.jsdelivr.net/npm/whatwg-fetch@{{whatwg_fetch_version}}/dist/fetch.umd.js"
25+
integrity="{{whatwg_fetch_sri}}"
2526
crossorigin="anonymous"></script>
2627
<script src="https://cdn.jsdelivr.net/npm/react@{{react_version}}/umd/react.production.min.js"
28+
integrity="{{react_sri}}"
2729
crossorigin="anonymous"></script>
2830
<script src="https://cdn.jsdelivr.net/npm/react-dom@{{react_version}}/umd/react-dom.production.min.js"
31+
integrity="{{react_dom_sri}}"
2932
crossorigin="anonymous"></script>
3033
<script src="https://cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.min.js"
34+
integrity="{{graphiql_sri}}"
3135
crossorigin="anonymous"></script>
3236
<script src="https://cdn.jsdelivr.net/npm/subscriptions-transport-ws@{{subscriptions_transport_ws_version}}/browser/client.js"
37+
integrity="{{subscriptions_transport_ws_sri}}"
3338
crossorigin="anonymous"></script>
3439
</head>
3540
<body>

graphene_django/views.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,27 @@ def instantiate_middleware(middlewares):
5252

5353

5454
class GraphQLView(View):
55-
graphiql_version = "1.0.3"
5655
graphiql_template = "graphene/graphiql.html"
56+
57+
# Polyfill for window.fetch.
58+
whatwg_fetch_version = "3.2.0"
59+
whatwg_fetch_sri = "sha256-l6HCB9TT2v89oWbDdo2Z3j+PSVypKNLA/nqfzSbM8mo="
60+
61+
# React and ReactDOM.
5762
react_version = "16.13.1"
58-
subscriptions_transport_ws_version = "0.9.16"
63+
react_sri = "sha256-yUhvEmYVhZ/GGshIQKArLvySDSh6cdmdcIx0spR3UP4="
64+
react_dom_sri = "sha256-vFt3l+illeNlwThbDUdoPTqF81M8WNSZZZt3HEjsbSU="
65+
66+
# The GraphiQL React app.
67+
graphiql_version = "1.0.3"
68+
graphiql_sri = "sha256-VR4buIDY9ZXSyCNFHFNik6uSe0MhigCzgN4u7moCOTk="
69+
graphiql_css_sri = "sha256-LwqxjyZgqXDYbpxQJ5zLQeNcf7WVNSJ+r8yp2rnWE/E="
70+
71+
# The websocket transport library for subscriptions.
72+
subscriptions_transport_ws_version = "0.9.17"
73+
subscriptions_transport_ws_sri = (
74+
"sha256-kCDzver8iRaIQ/SVlfrIwxaBQ/avXf9GQFJRLlErBnk="
75+
)
5976

6077
schema = None
6178
graphiql = False
@@ -101,7 +118,7 @@ def __init__(
101118
self.batch = self.batch or batch
102119
self.backend = backend
103120
if subscription_path is None:
104-
subscription_path = graphene_settings.SUBSCRIPTION_PATH
121+
self.subscription_path = graphene_settings.SUBSCRIPTION_PATH
105122

106123
assert isinstance(
107124
self.schema, GraphQLSchema
@@ -137,9 +154,18 @@ def dispatch(self, request, *args, **kwargs):
137154
if show_graphiql:
138155
return self.render_graphiql(
139156
request,
140-
graphiql_version=self.graphiql_version,
157+
# Dependency parameters.
158+
whatwg_fetch_version=self.whatwg_fetch_version,
159+
whatwg_fetch_sri=self.whatwg_fetch_sri,
141160
react_version=self.react_version,
161+
react_sri=self.react_sri,
162+
react_dom_sri=self.react_dom_sri,
163+
graphiql_version=self.graphiql_version,
164+
graphiql_sri=self.graphiql_sri,
165+
graphiql_css_sri=self.graphiql_css_sri,
142166
subscriptions_transport_ws_version=self.subscriptions_transport_ws_version,
167+
subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
168+
# The SUBSCRIPTION_PATH setting.
143169
subscription_path=self.subscription_path,
144170
)
145171

0 commit comments

Comments
 (0)