@@ -169,6 +169,27 @@ function initWebId (argv, app, ldp) {
169
169
const useSecureCookies = ! ! argv . sslKey // use secure cookies when over HTTPS
170
170
const sessionHandler = session ( sessionSettings ( useSecureCookies , argv . host ) )
171
171
app . use ( sessionHandler )
172
+ // Reject cookies from third-party applications.
173
+ // Otherwise, when a user is logged in to their Solid server,
174
+ // any third-party application could perform authenticated requests
175
+ // without permission by including the credentials set by the Solid server.
176
+ app . use ( ( req , res , next ) => {
177
+ const origin = req . headers . origin
178
+ const userId = req . session . userId
179
+ // Exception: allow logout requests from all third-party apps
180
+ // such that OIDC client can log out via cookie auth
181
+ // TODO: remove this exception when OIDC clients
182
+ // use Bearer token to authenticate instead of cookie
183
+ // (https://github.com/solid/node-solid-server/pull/835#issuecomment-426429003)
184
+ if ( ! argv . host . allowsSessionFor ( userId , origin ) && ! isLogoutRequest ( req ) ) {
185
+ debug ( `Rejecting session for ${ userId } from ${ origin } ` )
186
+ // Destroy session data
187
+ delete req . session . userId
188
+ // Ensure this modified session is not saved
189
+ req . session . save = ( done ) => done ( )
190
+ }
191
+ next ( )
192
+ } )
172
193
173
194
let accountManager = AccountManager . from ( {
174
195
authMethod : argv . auth ,
@@ -187,30 +208,20 @@ function initWebId (argv, app, ldp) {
187
208
// Set up authentication-related API endpoints and app.locals
188
209
initAuthentication ( app , argv )
189
210
190
- // Protect against requests from third-party applications
191
- app . use ( ( req , res , next ) => {
192
- // Reject cookies from third-party applications.
193
- // Otherwise, when a user is logged in to their Solid server,
194
- // any third-party application could perform authenticated requests
195
- // without permission by including the credentials set by the Solid server.
196
- const origin = req . headers . origin
197
- const userId = req . session . userId
198
- if ( ! argv . host . allowsSessionFor ( userId , origin ) ) {
199
- debug ( `Rejecting session for ${ userId } from ${ origin } ` )
200
- // Destroy session data
201
- delete req . session . userId
202
- // Ensure this modified session is not saved
203
- req . session . save = done => done ( )
204
- }
205
- next ( )
206
- } )
207
-
208
- // Set up per-host LDP middleware
209
211
if ( argv . multiuser ) {
210
212
app . use ( vhost ( '*' , LdpMiddleware ( corsSettings ) ) )
211
213
}
212
214
}
213
215
216
+ /**
217
+ * Determines whether the given request is a logout request
218
+ */
219
+ function isLogoutRequest ( req ) {
220
+ // TODO: this is a hack that hard-codes OIDC paths,
221
+ // this code should live in the OIDC module
222
+ return req . path === '/logout' || req . path === '/goodbye'
223
+ }
224
+
214
225
/**
215
226
* Sets up authentication-related routes and handlers for the app.
216
227
*
0 commit comments