Skip to content

Commit

Permalink
Merge pull request #302 from sasjs/cookies-with-domain
Browse files Browse the repository at this point in the history
fix: added domain for session cookies
  • Loading branch information
allanbowe authored Oct 9, 2022
2 parents b64c0c1 + b1a2677 commit d5daafc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MODE=[desktop|server] default considered as desktop
CORS=[disable|enable] default considered as disable for server MODE & enable for desktop MODE
ALLOWED_DOMAIN=<just domain e.g. example.com >
WHITELIST=<space separated urls, each starting with protocol `http` or `https`>

PROTOCOL=[http|https] default considered as http
Expand Down
14 changes: 11 additions & 3 deletions api/src/app-modules/configureExpressSession.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Express } from 'express'
import { Express, CookieOptions } from 'express'
import mongoose from 'mongoose'
import session from 'express-session'
import MongoStore from 'connect-mongo'

import { ModeType } from '../utils'
import { cookieOptions } from '../app'
import { ModeType, ProtocolType } from '../utils'

export const configureExpressSession = (app: Express) => {
const { MODE } = process.env
Expand All @@ -19,6 +18,15 @@ export const configureExpressSession = (app: Express) => {
})
}

const { PROTOCOL, ALLOWED_DOMAIN } = process.env
const cookieOptions: CookieOptions = {
secure: PROTOCOL === ProtocolType.HTTPS,
httpOnly: true,
sameSite: PROTOCOL === ProtocolType.HTTPS ? 'none' : undefined,
maxAge: 24 * 60 * 60 * 1000, // 24 hours
domain: ALLOWED_DOMAIN?.trim() || undefined
}

app.use(
session({
secret: process.secrets.SESSION_SECRET,
Expand Down
12 changes: 1 addition & 11 deletions api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import express, { ErrorRequestHandler, CookieOptions } from 'express'
import express, { ErrorRequestHandler } from 'express'
import cookieParser from 'cookie-parser'
import dotenv from 'dotenv'

Expand All @@ -8,7 +8,6 @@ import {
getWebBuildFolder,
instantiateLogger,
loadAppStreamConfig,
ProtocolType,
ReturnCode,
setProcessVariables,
setupFolders,
Expand All @@ -29,15 +28,6 @@ if (verifyEnvVariables()) process.exit(ReturnCode.InvalidEnv)

const app = express()

const { PROTOCOL } = process.env

export const cookieOptions: CookieOptions = {
secure: PROTOCOL === ProtocolType.HTTPS,
httpOnly: true,
sameSite: PROTOCOL === ProtocolType.HTTPS ? 'none' : undefined,
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}

const onError: ErrorRequestHandler = (err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
Expand Down
5 changes: 4 additions & 1 deletion api/src/routes/web/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ webRouter.get('/', async (req, res) => {
} catch (_) {
response = '<html><head></head><body>Web Build is not present</body></html>'
} finally {
const codeToInject = `<script>document.cookie = 'XSRF-TOKEN=${generateCSRFToken()}; Max-Age=86400; SameSite=Strict; Path=/;'</script>`
const { ALLOWED_DOMAIN } = process.env
const allowedDomain = ALLOWED_DOMAIN?.trim()
const domain = allowedDomain ? ` Domain=${allowedDomain};` : ''
const codeToInject = `<script>document.cookie = 'XSRF-TOKEN=${generateCSRFToken()};${domain} Max-Age=86400; SameSite=Strict; Path=/;'</script>`
const injectedContent = response?.replace(
'</head>',
`${codeToInject}</head>`
Expand Down
2 changes: 1 addition & 1 deletion api/src/utils/verifyEnvVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ const verifyRUN_TIMES = (): string[] => {
return errors
}

const verifyExecutablePaths = () => {
const verifyExecutablePaths = (): string[] => {
const errors: string[] = []
const { RUN_TIMES, SAS_PATH, NODE_PATH, PYTHON_PATH, R_PATH, MODE } =
process.env
Expand Down

0 comments on commit d5daafc

Please sign in to comment.