Only mark cookies Secure for https requests, so /admin works over plain http on the LAN
This commit is contained in:
@@ -184,7 +184,7 @@ export function registerAdminRoutes (app: Express) {
|
|||||||
}
|
}
|
||||||
loginFailures.delete(ip)
|
loginFailures.delete(ip)
|
||||||
res.cookie(ADMIN_COOKIE, sign(Date.now() + ADMIN_SESSION_MS), {
|
res.cookie(ADMIN_COOKIE, sign(Date.now() + ADMIN_SESSION_MS), {
|
||||||
httpOnly: true, sameSite: 'strict', secure: secureCookies(), path: '/admin', maxAge: ADMIN_SESSION_MS
|
httpOnly: true, sameSite: 'strict', secure: secureCookies(req), path: '/admin', maxAge: ADMIN_SESSION_MS
|
||||||
})
|
})
|
||||||
res.redirect(303, '/admin')
|
res.redirect(303, '/admin')
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -34,24 +34,37 @@ export function readCookie (req: Request, name: string): string | undefined {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Whether cookies should be `Secure`: true when the public URL is https. */
|
/**
|
||||||
export function secureCookies (): boolean {
|
* Whether cookies set on this response should be `Secure`: when the request
|
||||||
return (process.env.PUBLIC_BASE_URL || '').toLowerCase().startsWith('https://')
|
* came over https (directly, or per X-Forwarded-Proto from a trusted proxy),
|
||||||
|
* or was addressed to the https public host. A request straight to the
|
||||||
|
* container over plain http (e.g. http://<lan ip>:3000/admin) gets a
|
||||||
|
* non-Secure cookie, since browsers drop Secure cookies set over http.
|
||||||
|
*/
|
||||||
|
export function secureCookies (req: Request): boolean {
|
||||||
|
if (req.secure) return true
|
||||||
|
let publicUrl: URL
|
||||||
|
try {
|
||||||
|
publicUrl = new URL(process.env.PUBLIC_BASE_URL || '')
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return publicUrl.protocol === 'https:' && req.hostname === publicUrl.hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
function sessionCookieOptions (): CookieOptions {
|
function sessionCookieOptions (req: Request): CookieOptions {
|
||||||
return { httpOnly: true, sameSite: 'lax', secure: secureCookies(), path: '/', maxAge: SESSION_MAX_AGE_MS }
|
return { httpOnly: true, sameSite: 'lax', secure: secureCookies(req), path: '/', maxAge: SESSION_MAX_AGE_MS }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sign a recipient in on this browser with a long-lived cookie. */
|
/** Sign a recipient in on this browser with a long-lived cookie. */
|
||||||
export function startSession (res: Response, recipient: Recipient) {
|
export function startSession (res: Response, recipient: Recipient) {
|
||||||
const token = getStore().createSession(recipient.id, nowIso())
|
const token = getStore().createSession(recipient.id, nowIso())
|
||||||
res.cookie(SESSION_COOKIE, token, sessionCookieOptions())
|
res.cookie(SESSION_COOKIE, token, sessionCookieOptions(res.req))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function endSession (req: Request, res: Response) {
|
export function endSession (req: Request, res: Response) {
|
||||||
if (req.sessionToken) getStore().deleteSession(req.sessionToken)
|
if (req.sessionToken) getStore().deleteSession(req.sessionToken)
|
||||||
res.clearCookie(SESSION_COOKIE, { ...sessionCookieOptions(), maxAge: undefined })
|
res.clearCookie(SESSION_COOKIE, { ...sessionCookieOptions(req), maxAge: undefined })
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Attach `req.recipient` from the session cookie, if it is valid. Never blocks. */
|
/** Attach `req.recipient` from the session cookie, if it is valid. Never blocks. */
|
||||||
@@ -65,7 +78,7 @@ export function loadRecipient (req: Request, res: Response, next: NextFunction)
|
|||||||
req.sessionToken = token
|
req.sessionToken = token
|
||||||
if (Date.now() - Date.parse(session.lastSeenAt) > SESSION_REFRESH_MS) {
|
if (Date.now() - Date.parse(session.lastSeenAt) > SESSION_REFRESH_MS) {
|
||||||
store.touchSession(token, nowIso())
|
store.touchSession(token, nowIso())
|
||||||
res.cookie(SESSION_COOKIE, token, sessionCookieOptions())
|
res.cookie(SESSION_COOKIE, token, sessionCookieOptions(res.req))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user