Skip to content

Commit 35eb103

Browse files
authored
feat(oauth2-redirect): externalize inline script for CSP compliance (#10559)
1 parent 4dea377 commit 35eb103

File tree

2 files changed

+70
-71
lines changed

2 files changed

+70
-71
lines changed

dev-helpers/oauth2-redirect.html

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,4 @@
33
<body>
44
</body>
55
</html>
6-
<script>
7-
'use strict';
8-
function run () {
9-
var oauth2 = window.opener.swaggerUIRedirectOauth2;
10-
var sentState = oauth2.state;
11-
var redirectUrl = oauth2.redirectUrl;
12-
var isValid, qp, arr;
13-
14-
if (/code|token|error/.test(window.location.hash)) {
15-
qp = window.location.hash.substring(1).replace('?', '&');
16-
} else {
17-
qp = location.search.substring(1);
18-
}
19-
20-
arr = qp.split("&")
21-
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';})
22-
qp = qp ? JSON.parse('{' + arr.join() + '}',
23-
function (key, value) {
24-
return key === "" ? value : decodeURIComponent(value)
25-
}
26-
) : {}
27-
28-
isValid = qp.state === sentState
29-
30-
if ((
31-
oauth2.auth.schema.get("flow") === "accessCode" ||
32-
oauth2.auth.schema.get("flow") === "authorizationCode" ||
33-
oauth2.auth.schema.get("flow") === "authorization_code"
34-
) && !oauth2.auth.code) {
35-
if (!isValid) {
36-
oauth2.errCb({
37-
authId: oauth2.auth.name,
38-
source: "auth",
39-
level: "warning",
40-
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
41-
});
42-
}
43-
44-
if (qp.code) {
45-
delete oauth2.state;
46-
oauth2.auth.code = qp.code;
47-
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
48-
} else {
49-
let oauthErrorMsg
50-
if (qp.error) {
51-
oauthErrorMsg = "["+qp.error+"]: " +
52-
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
53-
(qp.error_uri ? "More info: "+qp.error_uri : "");
54-
}
55-
56-
oauth2.errCb({
57-
authId: oauth2.auth.name,
58-
source: "auth",
59-
level: "error",
60-
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
61-
});
62-
}
63-
} else {
64-
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
65-
}
66-
window.close();
67-
}
68-
69-
if( document.readyState !== 'loading' ) {
70-
run();
71-
} else {
72-
document.addEventListener('DOMContentLoaded', function () {
73-
run();
74-
});
75-
}
76-
</script>
6+
<script src="oauth2-redirect.js"></script>

dev-helpers/oauth2-redirect.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict"
2+
function run () {
3+
var oauth2 = window.opener.swaggerUIRedirectOauth2
4+
var sentState = oauth2.state
5+
var redirectUrl = oauth2.redirectUrl
6+
var isValid, qp, arr
7+
8+
if (/code|token|error/.test(window.location.hash)) {
9+
qp = window.location.hash.substring(1).replace("?", "&")
10+
} else {
11+
qp = location.search.substring(1)
12+
}
13+
14+
arr = qp.split("&")
15+
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace("=", '":"') + '"' })
16+
qp = qp ? JSON.parse("{" + arr.join() + "}",
17+
function (key, value) {
18+
return key === "" ? value : decodeURIComponent(value)
19+
}
20+
) : {}
21+
22+
isValid = qp.state === sentState
23+
24+
if ((
25+
oauth2.auth.schema.get("flow") === "accessCode" ||
26+
oauth2.auth.schema.get("flow") === "authorizationCode" ||
27+
oauth2.auth.schema.get("flow") === "authorization_code"
28+
) && !oauth2.auth.code) {
29+
if (!isValid) {
30+
oauth2.errCb({
31+
authId: oauth2.auth.name,
32+
source: "auth",
33+
level: "warning",
34+
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
35+
})
36+
}
37+
38+
if (qp.code) {
39+
delete oauth2.state
40+
oauth2.auth.code = qp.code
41+
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl})
42+
} else {
43+
let oauthErrorMsg
44+
if (qp.error) {
45+
oauthErrorMsg = "["+qp.error+"]: " +
46+
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
47+
(qp.error_uri ? "More info: "+qp.error_uri : "")
48+
}
49+
50+
oauth2.errCb({
51+
authId: oauth2.auth.name,
52+
source: "auth",
53+
level: "error",
54+
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
55+
})
56+
}
57+
} else {
58+
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl})
59+
}
60+
window.close()
61+
}
62+
63+
if( document.readyState !== "loading" ) {
64+
run()
65+
} else {
66+
document.addEventListener("DOMContentLoaded", function () {
67+
run()
68+
})
69+
}

0 commit comments

Comments
 (0)