Skip to content

Commit

Permalink
Merge branch 'master' into enh/support_az_cloud_config
Browse files Browse the repository at this point in the history
  • Loading branch information
grolu authored Sep 23, 2024
2 parents 1ba3faf + 74eb02b commit 9421b2b
Show file tree
Hide file tree
Showing 106 changed files with 1,156 additions and 580 deletions.
270 changes: 148 additions & 122 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions backend/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const { healthCheck } = require('./healthz')
const { port, metricsPort } = config
const periodSeconds = config.readinessProbe?.periodSeconds || 10

// protect against Prototype Pollution vulnerabilities
for (const ctor of [Object, Function, Array, String, Number, Boolean]) {
Object.freeze(ctor)
Object.freeze(ctor.prototype)
}

// resolve pathnames
const PUBLIC_DIRNAME = resolve(join(__dirname, '..', 'public'))
const INDEX_FILENAME = join(PUBLIC_DIRNAME, 'index.html')
Expand Down
47 changes: 27 additions & 20 deletions backend/lib/cache/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const _ = require('lodash')
const logger = require('../logger')

function init () {
const issues = {}
const commentsForIssues = {} // we could also think of getting rid of the comments cache
const issues = new Map()
const commentsForIssues = new Map() // we could also think of getting rid of the comments cache
const emitter = new EventEmitter()

function on (eventName, listener) {
Expand Down Expand Up @@ -38,15 +38,16 @@ function init () {
}

function getIssues () {
return _.values(issues)
return Array.from(issues.values())
}

function getCommentsForIssue ({ issueNumber }) {
return _.values(getCommentsForIssueCache({ issueNumber }))
const comments = getCommentsForIssueCache({ issueNumber })
return Array.from(comments.values())
}

function getIssue (number) {
return issues[number]
return issues.get(number)
}

function getIssueNumbers () {
Expand All @@ -62,23 +63,25 @@ function init () {
}

function getCommentsForIssueCache ({ issueNumber }) {
if (!commentsForIssues[issueNumber]) {
commentsForIssues[issueNumber] = {}
if (!commentsForIssues.has(issueNumber)) {
commentsForIssues.set(issueNumber, new Map())
}
return commentsForIssues[issueNumber]
return commentsForIssues.get(issueNumber)
}

function addOrUpdateIssues ({ issues }) {
_.forEach(issues, issue => addOrUpdateIssue({ issue }))
for (const issue of issues) {
addOrUpdateIssue({ issue })
}
}

function addOrUpdateIssue ({ issue }) {
updateIfNewer('issue', issues, issue, 'number')
updateIfNewer('issue', issues, issue)
}

function addOrUpdateComment ({ issueNumber, comment }) {
const comments = getCommentsForIssueCache({ issueNumber })
updateIfNewer('comment', comments, comment, 'id')
updateIfNewer('comment', comments, comment)
}

function removeIssue ({ issue }) {
Expand All @@ -87,37 +90,41 @@ function init () {

const comments = getCommentsForIssueCache({ issueNumber })

_.unset(issues, issueNumber)
_.unset(commentsForIssues, issueNumber)
issues.delete(issueNumber)
commentsForIssues.delete(issueNumber)

emitIssueDeleted(issue)
_.forEach(comments, emitCommmentDeleted)
for (const comment of comments.values()) {
emitCommmentDeleted(comment)
}
}

function removeComment ({ issueNumber, comment }) {
const identifier = comment.metadata.id
logger.trace('removing comment', identifier, 'of issue', issueNumber)
const commentsForIssuesCache = getCommentsForIssueCache({ issueNumber })
_.unset(commentsForIssuesCache, identifier)
commentsForIssuesCache.delete(identifier)
emitCommmentDeleted(comment)
}

function updateIfNewer (kind, cachedList, item, itemIdentifier) {
const identifier = item.metadata[itemIdentifier]
const cachedItem = cachedList[identifier]
function updateIfNewer (kind, cachedMap, item) {
const identifier = kind === 'issue'
? item.metadata.number
: item.metadata.id
const cachedItem = cachedMap.get(identifier)
if (cachedItem) {
if (isCachedItemOlder(cachedItem, item)) {
if (!_.isEqual(cachedItem, item)) {
logger.trace('updating', kind, identifier)
cachedList[identifier] = item
cachedMap.set(identifier, item)
emitModified(kind, item)
}
} else {
logger.warn(`skipped updating ${kind} with id ${identifier} as it was older`)
}
} else {
logger.trace('adding new', kind, identifier)
cachedList[identifier] = item
cachedMap.set(identifier, item)
emitAdded(kind, item)
}
return item
Expand Down
39 changes: 16 additions & 23 deletions backend/lib/config/gardener.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,22 @@ function parseConfigValue (value, type) {
}
}

function getValueFromFile (filePath, type) {
try {
const value = fs.readFileSync(filePath, 'utf8')
return parseConfigValue(value, type)
} catch (error) {
return undefined
}
}

function getValueFromEnvironmentOrFile (env, environmentVariableName, filePath, type) {
const value = parseConfigValue(env[environmentVariableName], type)
if (value !== undefined) {
return value
}

if (filePath) {
return getValueFromFile(filePath, type)
}
}

module.exports = {
assignConfigFromEnvironmentAndFileSystem (config, env) {
for (const { environmentVariableName, configPath, filePath, type = 'String' } of configMappings) {
const value = getValueFromEnvironmentOrFile(env, environmentVariableName, filePath, type)
for (const configMapping of configMappings) {
const {
environmentVariableName,
configPath,
filePath,
type = 'String'
} = configMapping
let rawValue = env[environmentVariableName] // eslint-disable-line security/detect-object-injection
if (!rawValue && filePath) {
try {
rawValue = fs.readFileSync(filePath, 'utf8') // eslint-disable-line security/detect-non-literal-fs-filename
} catch (err) { /* ignore error */ }
}
const value = parseConfigValue(rawValue, type)

if (value !== undefined) {
_.set(config, configPath, value)
Expand Down Expand Up @@ -224,6 +216,7 @@ module.exports = {
return config
},
readConfig (path) {
return yaml.load(fs.readFileSync(path, 'utf8'))
const data = fs.readFileSync(path, 'utf8') // eslint-disable-line security/detect-non-literal-fs-filename
return yaml.load(data)
}
}
7 changes: 4 additions & 3 deletions backend/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ class LifecycleHooks {
this.io = io(server, cache)
// register watches
for (const [key, watch] of Object.entries(watches)) {
if (informers[key]) {
const informer = informers[key] // eslint-disable-line security/detect-object-injection
if (informer) {
if (key === 'leases') {
watch(this.io, informers[key], { signal: this.ac.signal })
watch(this.io, informer, { signal: this.ac.signal })
} else {
watch(this.io, informers[key])
watch(this.io, informer)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ function errorToLocals (err, req) {
? { name, stack }
: { name }
let code = 500
let reason = STATUS_CODES[code]
let reason = _.get(STATUS_CODES, [code])
if (isHttpError(err)) {
code = err.statusCode
reason = STATUS_CODES[code]
reason = _.get(STATUS_CODES, [code])
}
if (code < 100 || code >= 600) {
code = 500
Expand Down
5 changes: 3 additions & 2 deletions backend/lib/routes/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ router.route('/')
function sanitizeFrontendConfig (frontendConfig) {
const converter = markdown.createConverter()
const convertAndSanitize = (obj, key) => {
if (obj[key]) {
obj[key] = converter.makeSanitizedHtml(obj[key])
const value = obj[key] // eslint-disable-line security/detect-object-injection -- key is a local fixed string
if (value) {
obj[key] = converter.makeSanitizedHtml(value) // eslint-disable-line security/detect-object-injection -- key is a local fixed string
}
}

Expand Down
30 changes: 26 additions & 4 deletions backend/lib/routes/terminals.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

const express = require('express')
const { terminals, authorization } = require('../services')
const _ = require('lodash')
const { UnprocessableEntity } = require('http-errors')
const { metricsRoute } = require('../middleware')

Expand Down Expand Up @@ -41,10 +40,33 @@ router.route('/')

const { method, params: body } = req.body

if (!_.includes(['create', 'fetch', 'list', 'config', 'remove', 'heartbeat', 'listProjectTerminalShortcuts'], method)) {
throw new UnprocessableEntity(`${method} not allowed for terminals`)
let terminalOperation
switch (method) {
case 'create':
terminalOperation = terminals.create
break
case 'fetch':
terminalOperation = terminals.fetch
break
case 'list':
terminalOperation = terminals.list
break
case 'config':
terminalOperation = terminals.config
break
case 'remove':
terminalOperation = terminals.remove
break
case 'heartbeat':
terminalOperation = terminals.heartbeat
break
case 'listProjectTerminalShortcuts':
terminalOperation = terminals.listProjectTerminalShortcuts
break
default:
throw new UnprocessableEntity(`${method} not allowed for terminals`)
}
res.send(await terminals[method]({ user, body }))
res.send(await terminalOperation.call(terminals, { user, body }))
} catch (err) {
next(err)
}
Expand Down
13 changes: 7 additions & 6 deletions backend/lib/security/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ async function authorizationCallback (req, res) {
secure: true,
path: '/'
}
const stateObject = {}
let stateObject = {}
if (COOKIE_STATE in req.cookies) {
Object.assign(stateObject, req.cookies[COOKIE_STATE])
stateObject = req.cookies[COOKIE_STATE] // eslint-disable-line security/detect-object-injection -- COOKIE_STATE is a constant
res.clearCookie(COOKIE_STATE, options)
}
const {
Expand All @@ -299,7 +299,7 @@ async function authorizationCallback (req, res) {
state
}
if (COOKIE_CODE_VERIFIER in req.cookies) {
checks.code_verifier = req.cookies[COOKIE_CODE_VERIFIER]
checks.code_verifier = req.cookies[COOKIE_CODE_VERIFIER] // eslint-disable-line security/detect-object-injection -- COOKIE_CODE_VERIFIER is a constant
res.clearCookie(COOKIE_CODE_VERIFIER, options)
}
const tokenSet = await authorizationCodeExchange(backendRedirectUri, parameters, checks)
Expand All @@ -316,8 +316,9 @@ function isXmlHttpRequest ({ headers = {} }) {
}

function getAccessToken (cookies) {
const [header, payload] = split(cookies[COOKIE_HEADER_PAYLOAD], '.')
const signature = cookies[COOKIE_SIGNATURE]
const headerAndPayload = cookies[COOKIE_HEADER_PAYLOAD] // eslint-disable-line security/detect-object-injection -- COOKIE_HEADER_PAYLOAD is a constant
const [header, payload] = split(headerAndPayload, '.')
const signature = cookies[COOKIE_SIGNATURE] // eslint-disable-line security/detect-object-injection -- COOKIE_SIGNATURE is a constant
if (header && payload && signature) {
return join([header, payload, signature], '.')
}
Expand Down Expand Up @@ -360,7 +361,7 @@ function csrfProtection (req) {

async function getTokenSet (cookies) {
const accessToken = getAccessToken(cookies)
const encryptedValues = cookies[COOKIE_TOKEN]
const encryptedValues = cookies[COOKIE_TOKEN] // eslint-disable-line security/detect-object-injection -- COOKIE_TOKEN is a constant
if (!encryptedValues) {
throw createError(401, 'No bearer token found in request', { code: 'ERR_JWE_NOT_FOUND' })
}
Expand Down
13 changes: 12 additions & 1 deletion backend/lib/services/members/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

const { pick } = require('lodash')

const PROPERTY_NAMES = ['createdBy', 'creationTimestamp', 'deletionTimestamp', 'description', 'kubeconfig', 'orphaned']
const PROPERTY_NAMES = Object.freeze([
'createdBy',
'creationTimestamp',
'deletionTimestamp',
'description',
'kubeconfig',
'orphaned'
])

class Member {
constructor (username, { roles, extensions } = {}) {
Expand All @@ -32,6 +39,10 @@ class Member {
name: username
}
}

static get allowedExtensionProperties () {
return PROPERTY_NAMES
}
}

module.exports = Member
Loading

0 comments on commit 9421b2b

Please sign in to comment.