Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/swift-dodos-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@chainlink/ea-bootstrap': patch
'@chainlink/observation': patch
'@chainlink/ea-scripts': patch
'@chainlink/por-address-list-adapter': patch
---

fix(security): resolve CodeQL code alerts

- js/insecure-randomness: replace Math.random with crypto.randomInt in util
- js/http-to-file-access: validate output filename before file write
- js/indirect-command-line-injection: sanitize branch name for shell
- js/incomplete-sanitization: replace all apostrophes, not first only
- js/prototype-polluting-assignment: skip `__proto__`/constructor/prototype
1 change: 1 addition & 0 deletions packages/core/bootstrap/src/lib/modules/overrider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class Overrider {
): AdapterOverrides => {
const combinedOverrides = internalOverrides || {}
for (const symbol of Object.keys(inputOverrides)) {
if (symbol === '__proto__' || symbol === 'constructor' || symbol === 'prototype') continue
combinedOverrides[symbol] = inputOverrides[symbol]
}
return combinedOverrides
Expand Down
5 changes: 3 additions & 2 deletions packages/core/bootstrap/src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto'
import type {
AdapterContext,
AdapterImplementation,
Expand Down Expand Up @@ -93,7 +94,7 @@ export const getRandomEnv = (name: string, delimiter = ',', prefix = ''): string
const val = getEnv(name, prefix)
if (!val) return val
const items = val.split(delimiter)
return items[Math.floor(Math.random() * items.length)]
return items[crypto.randomInt(items.length)]
}

// pick a random string from env var after splitting with the delimiter ("a&b&c" "&" -> choice(["a","b","c"]))
Expand All @@ -104,7 +105,7 @@ export const getRandomRequiredEnv = (
): string | undefined => {
const val = getRequiredEnv(name, prefix)
const items = val.split(delimiter)
return items[Math.floor(Math.random() * items.length)]
return items[crypto.randomInt(items.length)]
}

// We generate an UUID per instance
Expand Down
9 changes: 5 additions & 4 deletions packages/core/bootstrap/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto'
import {
AdapterContext,
AdapterRequest,
Expand Down Expand Up @@ -200,10 +201,10 @@ describe('utils', () => {
const varName = 'RANDOM_TEST_ENV_VAR'
process.env[varName] = 'one,two,three'
jest
.spyOn(global.Math, 'random')
.mockReturnValueOnce(0.1)
.mockReturnValueOnce(0.5)
.mockReturnValueOnce(0.7)
.spyOn(crypto, 'randomInt')
.mockImplementationOnce(() => 0)
.mockImplementationOnce(() => 1)
.mockImplementationOnce(() => 2)

expect(getRandomRequiredEnv(varName)).toBe('one')
expect(getRandomRequiredEnv(varName)).toBe('two')
Expand Down
15 changes: 13 additions & 2 deletions packages/observation/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import axios from 'axios'
import fs from 'fs'
import path from 'path'
import { config } from './config'

const HEADERS = 'round,staging,production,timestamp'

/** Validate outputFileName to prevent path traversal; only allow safe basenames */
function getSafeOutputPath(fileName: string): string {
const basename = path.basename(fileName)
if (basename !== fileName || basename.includes('..')) {
throw new Error(`Invalid output file name: ${fileName}`)
}
return basename
}
const stagingURL = `https://adapters.main.stage.cldev.sh/${config.adapterName}`
const prodURL = `https://adapters.main.prod.cldev.sh/${config.adapterName}`

Expand Down Expand Up @@ -33,15 +43,16 @@
}

;(async () => {
fs.writeFileSync(`${config.outputFileName}`, HEADERS)
const outputPath = getSafeOutputPath(config.outputFileName)
fs.writeFileSync(outputPath, HEADERS)
const numRequests = config.testDurationInSeconds / config.reqIntervalInSeconds
console.log(HEADERS)
for (let i = 0; i < numRequests; i++) {
const result = await getResultFromAdapter(i)
let content = `\n${i}`
content += `, ${result.stagingResult}, ${result.prodResult}, ${new Date().toISOString()}`
console.log(content)
fs.appendFileSync(`${config.outputFileName}`, content)
fs.appendFileSync(outputPath, content)

Check warning

Code scanning / CodeQL

Network data written to file Medium

Write to file system depends on
Untrusted data
.
Write to file system depends on
Untrusted data
.
await sleep(config.reqIntervalInSeconds * 1000)
}
})()
16 changes: 10 additions & 6 deletions packages/scripts/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ export const PUBLIC_ADAPTER_TYPES = [
]
const scope = '@chainlink/'

/** Sanitize branch name for safe use in shell; only allow alphanumeric, /, -, _, . */
function sanitizeBranchForShell(branch: string): string {
if (!/^[a-zA-Z0-9/_.-]*$/.test(branch)) {
throw new Error(`Invalid branch name: contains disallowed characters`)
}
return branch
}

export type WorkspacePackages = ReturnType<typeof getWorkspaceAdapters>
export function getWorkspacePackages(changedFromBranch = ''): WorkspacePackage[] {
const sinceArg = changedFromBranch ? ` --since=${sanitizeBranchForShell(changedFromBranch)}` : ''
return s
.exec(
changedFromBranch
? `yarn workspaces list -R --json --since=${changedFromBranch}`
: 'yarn workspaces list -R --json',
{ silent: true },
)
.exec(`yarn workspaces list -R --json${sinceArg}`.trim(), { silent: true })
.split('\n')
.filter(Boolean)
.map((v) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class LombardAddressManager extends AddressManager<LombardAddressManagerR
.flatMap((r) => r[0])
.filter((address) => address != '')
.map((address) => ({
address: address.replace("'", ''),
address: address.replace(/'/g, ''),
network: network,
chainId: chainId,
}))
Expand Down
Loading