|
@@ -13,12 +13,25 @@ const queryString = (params, prefix = '?') => {
|
|
|
* @param {object} res
|
|
|
*/
|
|
|
module.exports = function connect(req, res) {
|
|
|
- const { secret } = req.companion.options
|
|
|
+ const { secret, oauthOrigin } = req.companion.options
|
|
|
const stateObj = oAuthState.generateState()
|
|
|
|
|
|
- if (req.query.state) {
|
|
|
+ // not sure if we need to store origin in the session state (e.g. we could've just gotten it directly inside send-token)
|
|
|
+ // but we're afraid to change the logic there
|
|
|
+ if (oauthOrigin && !Array.isArray(oauthOrigin)) {
|
|
|
+ // If the server only allows a single origin, we ignore the client-supplied
|
|
|
+ // origin from query because we don't need it.
|
|
|
+ stateObj.origin = oauthOrigin
|
|
|
+ } else if (oauthOrigin && oauthOrigin.length < 2) {
|
|
|
+ // eslint-disable-next-line prefer-destructuring
|
|
|
+ stateObj.origin = oauthOrigin[0]
|
|
|
+ } else {
|
|
|
+ // If we have multiple allowed origins, we need to check the client-supplied origin from query.
|
|
|
+ // If the client provides an untrusted origin,
|
|
|
+ // we want to send `undefined`. `undefined` means `/`, which is the same origin when passed to `postMessage`.
|
|
|
+ // https://html.spec.whatwg.org/multipage/web-messaging.html#dom-window-postmessage-options-dev
|
|
|
const { origin } = JSON.parse(atob(req.query.state))
|
|
|
- stateObj.origin = origin
|
|
|
+ stateObj.origin = oauthOrigin ? oauthOrigin.find(o => o === origin) : origin
|
|
|
}
|
|
|
|
|
|
if (req.companion.options.server.oauthDomain) {
|