|
1 | 1 | const instance = require('../helpers/weak_cache');
|
| 2 | +const bodyParser = require('../shared/selective_body'); |
| 3 | +const noCache = require('../shared/no_cache'); |
| 4 | +const getParams = require('../shared/assemble_params'); |
| 5 | +const presence = require('../helpers/validate_presence'); |
| 6 | +const { InvalidRequest, InvalidClient } = require('../helpers/errors'); |
| 7 | + |
| 8 | +const PARAM_LIST = new Set(['client_id', 'origin']); |
| 9 | +const buildParams = getParams(PARAM_LIST); |
2 | 10 |
|
3 | 11 | module.exports = function checkSessionAction(provider) {
|
4 | 12 | const removeHeaders = !instance(provider).configuration('features.sessionManagement.keepHeaders');
|
5 | 13 | const thirdPartyCheckUrl = instance(provider).configuration('cookies.thirdPartyCheckUrl');
|
6 | 14 |
|
7 |
| - return async function checkSessionIframe(ctx, next) { |
8 |
| - const debug = ctx.query.debug !== undefined; |
9 |
| - |
10 |
| - if (removeHeaders) { |
11 |
| - ctx.response.remove('X-Frame-Options'); |
12 |
| - const csp = ctx.response.get('Content-Security-Policy'); |
13 |
| - if (csp.includes('frame-ancestors')) { |
14 |
| - ctx.response.set('Content-Security-Policy', csp.replace(/ ?frame-ancestors [^;]+;/, '')); |
| 15 | + return { |
| 16 | + get: async function checkSessionIframe(ctx, next) { |
| 17 | + if (removeHeaders) { |
| 18 | + ctx.response.remove('X-Frame-Options'); |
| 19 | + const csp = ctx.response.get('Content-Security-Policy'); |
| 20 | + if (csp.includes('frame-ancestors')) { |
| 21 | + ctx.response.set('Content-Security-Policy', csp.replace(/ ?frame-ancestors [^;]+;/, '')); |
| 22 | + } |
15 | 23 | }
|
16 |
| - } |
17 | 24 |
|
18 |
| - ctx.type = 'html'; |
19 |
| - ctx.body = `<!DOCTYPE html> |
20 |
| - <html> |
21 |
| - <head lang="en"> |
22 |
| - <meta charset="UTF-8"> |
23 |
| - <title>Session Management - OP iframe</title> |
24 |
| - <script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/2.3.1/sha256.js" integrity="sha256-NyuvLfsvfCfE+ceV6/W19H+qVp3M8c9FzAgj72CW39w=" crossorigin="anonymous"></script> |
25 |
| - </head> |
26 |
| - <body> |
| 25 | + ctx.type = 'html'; |
| 26 | + ctx.body = `<!DOCTYPE html> |
| 27 | +<html> |
| 28 | +<head lang="en"> |
| 29 | +<meta charset="UTF-8"> |
| 30 | +<title>Session Management - OP iframe</title> |
| 31 | +<script src="https://cdnjs.cloudflare.com/ajax/libs/jsSHA/2.3.1/sha256.js" integrity="sha256-NyuvLfsvfCfE+ceV6/W19H+qVp3M8c9FzAgj72CW39w=" crossorigin="anonymous"></script> |
| 32 | +<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch&rum=0"></script> |
| 33 | +</head> |
| 34 | +<body> |
27 | 35 |
|
28 |
| - <script type="application/javascript"> |
29 |
| - var debug = ${debug}; |
30 |
| - var thirdPartyCookies = true; |
| 36 | +<script type="application/javascript"> |
| 37 | +(function () { |
| 38 | + var thirdPartyCookies = true; |
| 39 | + var originCheckResult; |
31 | 40 |
|
32 |
| - function receiveMessage(e) { |
33 |
| - if (e.data === 'MM:3PCunsupported') { |
34 |
| - thirdPartyCookies = false; |
35 |
| - return; |
36 |
| - } else if (e.data === 'MM:3PCsupported') { |
37 |
| - return; |
| 41 | + function calculate(clientId, origin, actual, salt, cb) { |
| 42 | + try { |
| 43 | + if (originCheckResult.clientId !== clientId || originCheckResult.origin !== origin) { |
| 44 | + throw new Error('client_id and/or origin mismatch'); |
38 | 45 | }
|
39 |
| - try { |
40 |
| - var message_parts = e.data.split(' '); |
41 |
| - var clientId = message_parts[0]; |
42 |
| - var actual = message_parts[1]; |
43 |
| - if (debug && console) console.log('OP recv session state: ' + actual); |
44 |
| - var salt = actual.split('.')[1]; |
| 46 | + var opbs = getOPBrowserState(clientId); |
| 47 | + var stat = 'changed'; |
45 | 48 |
|
46 |
| - var opbs = getOPBrowserState(clientId); |
| 49 | + if (opbs) { |
47 | 50 | var shaObj = new jsSHA('SHA-256', 'TEXT');
|
48 |
| - shaObj.update(clientId + ' ' + e.origin + ' ' + opbs + ' ' + salt); |
49 |
| - var expected = shaObj.getHash('HEX') + ['.' + salt]; |
50 |
| - if (debug && console) console.log('OP computed session state: ' + expected); |
| 51 | + shaObj.update(clientId + ' ' + origin + ' ' + opbs + ' ' + salt); |
| 52 | + var expected = shaObj.getHash('HEX') + '.' + salt; |
51 | 53 |
|
52 |
| - var stat; |
53 | 54 | if (actual === expected) {
|
54 | 55 | stat = 'unchanged';
|
55 |
| - } else { |
56 |
| - stat = 'changed'; |
57 | 56 | }
|
| 57 | + } |
58 | 58 |
|
59 |
| - if (debug && console) console.log('OP status: ' + stat); |
| 59 | + cb(stat); |
| 60 | + } catch (err) { |
| 61 | + cb('error'); |
| 62 | + } |
| 63 | + } |
60 | 64 |
|
61 |
| - e.source.postMessage(stat, e.origin); |
62 |
| - } catch (err) { |
63 |
| - e.source.postMessage('error', e.origin); |
64 |
| - } |
| 65 | + function check(clientId, origin, actual, salt, cb) { |
| 66 | + if (!originCheckResult) { |
| 67 | + fetch(location.pathname, { |
| 68 | + method: 'POST', |
| 69 | + headers: { |
| 70 | + 'Content-Type': 'application/json; charset=utf-8', |
| 71 | + }, |
| 72 | + body: JSON.stringify({ client_id: clientId, origin: origin }), |
| 73 | + redirect: 'error', |
| 74 | + }).then(function (response) { |
| 75 | + if (response.ok) { |
| 76 | + originCheckResult = { |
| 77 | + origin: origin, |
| 78 | + clientId: clientId, |
| 79 | + }; |
| 80 | + calculate(clientId, origin, actual, salt, cb); |
| 81 | + } else { |
| 82 | + throw new Error('invalid client_id and/or origin'); |
| 83 | + } |
| 84 | + }).catch(function () { |
| 85 | + cb('error'); |
| 86 | + }); |
| 87 | + } else { |
| 88 | + calculate(clientId, origin, actual, salt, cb); |
65 | 89 | }
|
| 90 | + } |
66 | 91 |
|
67 |
| - function getOPBrowserState(clientId) { |
68 |
| - var cookie = readCookie('${provider.cookieName('state')}.' + clientId); |
69 |
| - if (debug && console) console.log('session state cookie: ' + cookie); |
70 |
| - if (!thirdPartyCookies && !cookie) throw new Error('third party cookies are most likely blocked'); |
71 |
| - return cookie; |
| 92 | + function receiveMessage(e) { |
| 93 | + if (typeof e.data !== 'string') { |
| 94 | + return; |
| 95 | + } |
| 96 | + if (e.data === 'MM:3PCunsupported') { |
| 97 | + thirdPartyCookies = false; |
| 98 | + return; |
| 99 | + } |
| 100 | + if (e.data === 'MM:3PCsupported') { |
| 101 | + return; |
| 102 | + } |
| 103 | + var parts = e.data.split(' '); |
| 104 | + var clientId = parts[0]; |
| 105 | + var actual = parts[1]; |
| 106 | + if (parts.length !== 2 || !clientId || !actual) { |
| 107 | + return; |
| 108 | + } |
| 109 | + var actualParts = actual.split('.'); |
| 110 | + var sessionStr = actualParts[0]; |
| 111 | + var salt = actualParts[1]; |
| 112 | + if (actualParts.length !== 2 || !salt || !sessionStr) { |
| 113 | + return; |
72 | 114 | }
|
| 115 | + check(clientId, e.origin, actual, salt, function (stat) { |
| 116 | + e.source.postMessage(stat, e.origin); |
| 117 | + }); |
| 118 | + } |
73 | 119 |
|
74 |
| - function readCookie(name) { |
75 |
| - var nameEQ = name + "="; |
76 |
| - var ca = document.cookie.split(';'); |
77 |
| - for(var i=0;i < ca.length;i++) { |
78 |
| - var c = ca[i]; |
79 |
| - while (c.charAt(0)==' ') c = c.substring(1,c.length); |
80 |
| - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); |
81 |
| - } |
82 |
| - return null; |
| 120 | + function getOPBrowserState(clientId) { |
| 121 | + var cookie = readCookie('${provider.cookieName('state')}.' + clientId); |
| 122 | + if (!thirdPartyCookies && !cookie) throw new Error('third party cookies are most likely blocked'); |
| 123 | + return cookie; |
| 124 | + } |
| 125 | +
|
| 126 | + function readCookie(name) { |
| 127 | + var nameEQ = name + '='; |
| 128 | + var ca = document.cookie.split(';'); |
| 129 | + for (var i=0; i < ca.length; i++) { |
| 130 | + var c = ca[i]; |
| 131 | + while (c.charAt(0) === ' ') c = c.substring(1, c.length); |
| 132 | + if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); |
83 | 133 | }
|
| 134 | + return null; |
| 135 | + } |
84 | 136 |
|
85 |
| - window.addEventListener('message', receiveMessage, false); |
86 |
| - </script> |
87 |
| - <iframe src="${thirdPartyCheckUrl}" style="display:none" /> |
| 137 | + window.addEventListener('message', receiveMessage, false); |
| 138 | +})(); |
| 139 | +</script> |
| 140 | +<iframe src="${thirdPartyCheckUrl}" style="display:none" /> |
88 | 141 |
|
89 |
| - </body> |
90 |
| - </html>`; |
91 |
| - await next(); |
| 142 | +</body> |
| 143 | +</html>`; |
| 144 | + await next(); |
| 145 | + }, |
| 146 | + post: [ |
| 147 | + noCache, |
| 148 | + bodyParser('application/json'), |
| 149 | + buildParams, |
| 150 | + async function checkClientOrigin(ctx, next) { |
| 151 | + presence(ctx, 'origin', 'client_id'); |
| 152 | + const { client_id: clientId, origin } = ctx.oidc.params; |
| 153 | + [clientId, origin].forEach((value) => { |
| 154 | + if (typeof value !== 'string') { |
| 155 | + throw new InvalidRequest('only string parameter values are expected'); |
| 156 | + } |
| 157 | + }); |
| 158 | + const client = await provider.Client.find(clientId); |
| 159 | + ctx.oidc.entity('Client', client); |
| 160 | + if (!client) { |
| 161 | + throw new InvalidClient(); |
| 162 | + } |
| 163 | + if (!client.redirectUriOrigins.has(origin)) { |
| 164 | + throw new InvalidRequest('origin not allowed', 403); |
| 165 | + } |
| 166 | + ctx.status = 204; |
| 167 | + await next(); |
| 168 | + }, |
| 169 | + ], |
92 | 170 | };
|
93 | 171 | };
|
0 commit comments