Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processEnv - initial module; #1796

Open
wants to merge 9 commits into
base: minor
Choose a base branch
from
26 changes: 11 additions & 15 deletions api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ app.get(`/`, api)
@module /api
*/

const env = require('../mapp_env.js');

const logger = require('../mod/utils/logger')

const login = require('../mod/user/login')
Expand All @@ -28,7 +30,7 @@ const register = require('../mod/user/register')

const auth = require('../mod/user/auth')

const saml = process.env.SAML_ENTITY_ID && require('../mod/user/saml')
const saml = env.saml_entity_id && require('../mod/user/saml')

const routes = {
provider: require('../mod/provider/_provider'),
Expand All @@ -40,12 +42,6 @@ const routes = {
workspace: require('../mod/workspace/_workspace'),
}

process.env.COOKIE_TTL ??= 36000

process.env.TITLE ??= 'GEOLYTIX | XYZ'

process.env.DIR ??= ''

/**
@global
@typedef {Object} req
Expand Down Expand Up @@ -79,8 +75,8 @@ Requests are passed to individual API modules from the api() method.
module.exports = async function api(req, res) {

// redirect if dir is missing in url path.
if (process.env.DIR && req.url.length === 1) {
res.setHeader('location', `${process.env.DIR}`)
if (env.dir && req.url.length === 1) {
res.setHeader('location', `${env.dir}`)
return res.status(302).send()
}

Expand Down Expand Up @@ -159,10 +155,10 @@ module.exports = async function api(req, res) {
if (req.params.logout) {

// Remove cookie.
res.setHeader('Set-Cookie', `${process.env.TITLE}=null;HttpOnly;Max-Age=0;Path=${process.env.DIR || '/'}`)
res.setHeader('Set-Cookie', `${env.title}=null;HttpOnly;Max-Age=0;Path=${env.dir || '/'}`)

// Remove logout parameter.
res.setHeader('location', (process.env.DIR || '/') + (req.params.msg && `?msg=${req.params.msg}` || ''))
res.setHeader('location', (env.dir || '/') + (req.params.msg && `?msg=${req.params.msg}` || ''))

return res.status(302).send()
}
Expand All @@ -182,7 +178,7 @@ module.exports = async function api(req, res) {
}

// Remove cookie.
res.setHeader('Set-Cookie', `${process.env.TITLE}=null;HttpOnly;Max-Age=0;Path=${process.env.DIR || '/'};SameSite=Strict${!req.headers.host.includes('localhost') && ';Secure' || ''}`)
res.setHeader('Set-Cookie', `${env.title}=null;HttpOnly;Max-Age=0;Path=${env.dir || '/'};SameSite=Strict${!req.headers.host.includes('localhost') && ';Secure' || ''}`)

req.params.msg = user.msg || user.message

Expand All @@ -201,10 +197,10 @@ module.exports = async function api(req, res) {
}

// The login view will be returned for all PRIVATE requests without a valid user.
if (!user && process.env.PRIVATE) {
if (!user && env.private) {

if (process.env.SAML_LOGIN) {
res.setHeader('location', `${process.env.DIR}/saml/login`)
if (env.saml_login) {
res.setHeader('location', `${env.dir}/saml/login`)
return res.status(302).send()
}

Expand Down
48 changes: 25 additions & 23 deletions express.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,58 @@
extensions: ['html']
}))

app.use(`${process.env.DIR || ''}/public`, express.static('public'))
const env = require('./mapp_env.js');

app.use(process.env.DIR || '', express.static('public'))
app.use(`${env.dir || ''}/public`, express.static('public'))

app.use(`${process.env.DIR || ''}/tests`, express.static('tests'))
app.use(env.dir || '', express.static('public'))

app.use(process.env.DIR || '', express.static('tests'))
app.use(`${env.dir || ''}/tests`, express.static('tests'))

app.use(env.dir || '', express.static('tests'))

app.use(cookieParser())

Check failure

Code scanning / CodeQL

Missing CSRF middleware High

This cookie middleware is serving a
request handler
without CSRF protection.
This cookie middleware is serving a
request handler
without CSRF protection.
This cookie middleware is serving a
request handler
without CSRF protection.
This cookie middleware is serving a
request handler
without CSRF protection.
This cookie middleware is serving a
request handler
without CSRF protection.
This cookie middleware is serving a
request handler
without CSRF protection.

const api = require('./api/api')

app.get(`${process.env.DIR || ''}/api/provider/:provider?`, api)
app.get(`${env.dir || ''}/api/provider/:provider?`, api)
Fixed Show fixed Hide fixed

app.post(`${process.env.DIR || ''}/api/provider/:provider?`, express.json({ limit: '5mb' }), api)
app.post(`${env.dir || ''}/api/provider/:provider?`, express.json({ limit: '5mb' }), api)
Fixed Show fixed Hide fixed

app.get(`${process.env.DIR || ''}/api/sign/:provider?`, api)
app.get(`${env.dir || ''}/api/sign/:provider?`, api)
Fixed Show fixed Hide fixed

app.post(`${process.env.DIR || ''}/api/sign/:provider?`, express.json({ limit: '5mb' }), api)
app.post(`${env.dir || ''}/api/sign/:provider?`, express.json({ limit: '5mb' }), api)
Fixed Show fixed Hide fixed


app.get(`${process.env.DIR || ''}/api/query/:template?`, api)
app.get(`${env.dir || ''}/api/query/:template?`, api)
Fixed Show fixed Hide fixed

app.post(`${process.env.DIR || ''}/api/query/:template?`, express.json({ limit: '5mb' }), api)
app.post(`${env.dir || ''}/api/query/:template?`, express.json({ limit: '5mb' }), api)
Fixed Show fixed Hide fixed


app.get(`${process.env.DIR || ''}/api/fetch/:template?`, api)
app.get(`${env.dir || ''}/api/fetch/:template?`, api)
Fixed Show fixed Hide fixed

app.post(`${process.env.DIR || ''}/api/fetch/:template?`, express.json({ limit: '5mb' }), api)
app.post(`${env.dir || ''}/api/fetch/:template?`, express.json({ limit: '5mb' }), api)
Fixed Show fixed Hide fixed


app.get(`${process.env.DIR || ''}/api/workspace/:key?`, api)
app.get(`${env.dir || ''}/api/workspace/:key?`, api)
Fixed Show fixed Hide fixed


app.get(`${process.env.DIR || ''}/api/user/:method?/:key?`, api)
app.get(`${env.dir || ''}/api/user/:method?/:key?`, api)
Fixed Show fixed Hide fixed

app.post(`${process.env.DIR || ''}/api/user/:method?`, [express.urlencoded({ extended: true }), express.json({ limit: '5mb' })], api)
app.post(`${env.dir || ''}/api/user/:method?`, [express.urlencoded({ extended: true }), express.json({ limit: '5mb' })], api)
Fixed Show fixed Hide fixed

app.get(`${process.env.DIR || ''}/saml/metadata`, api)
app.get(`${env.dir || ''}/saml/metadata`, api)
Fixed Show fixed Hide fixed

app.get(`${process.env.DIR || ''}/saml/logout`, api)
app.get(`${env.dir || ''}/saml/logout`, api)
Fixed Show fixed Hide fixed

app.get(`${process.env.DIR || ''}/saml/login`, api)
app.get(`${env.dir || ''}/saml/login`, api)
Fixed Show fixed Hide fixed

app.post(`${process.env.DIR || ''}/saml/acs`, express.urlencoded({ extended: true }), api)
app.post(`${env.dir || ''}/saml/acs`, express.urlencoded({ extended: true }), api)
Fixed Show fixed Hide fixed

app.get(`${process.env.DIR || ''}/view/:template?`, api)
app.get(`${env.dir || ''}/view/:template?`, api)
Fixed Show fixed Hide fixed

app.get(`${process.env.DIR || ''}/:locale?`, api)
app.get(`${env.dir || ''}/:locale?`, api)
Fixed Show fixed Hide fixed

process.env.DIR && app.get(`/`, api)
env.dir && app.get(`/`, api)
Fixed Show fixed Hide fixed

app.listen(process.env.PORT || 3000)
app.listen(env.port || 3000)
13 changes: 13 additions & 0 deletions mapp_env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
process.env.COOKIE_TTL ??= 36000

process.env.TITLE ??= 'GEOLYTIX | XYZ'

process.env.DIR ??= ''

const env = {}

Object.entries(process.env).forEach(entry => {
env[entry[0].toLowerCase()] = entry[1]
})

module.exports = env
Loading