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

Update exec to not leak INPUT env variables to child processes #790

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions packages/exec/__tests__/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as exec from '../src/exec'
import * as tr from '../src/toolrunner'
import * as im from '../src/interfaces'

import * as childProcess from 'child_process'
Expand Down Expand Up @@ -620,6 +621,14 @@ describe('@actions/exec', () => {
expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`)
})

it('tool runner strips INPUT_ params from environment for child process', () => {
const env = {INPUT_TEST: 'input value', SOME_OTHER_ENV: 'some other value'}
const sanitizedEnv = tr.stripInputEnvironmentVariables(env)

expect(sanitizedEnv).not.toHaveProperty('INPUT_TEST')
expect(sanitizedEnv).toHaveProperty('SOME_OTHER_ENV')
})

if (IS_WINDOWS) {
it('Exec roots relative tool path using process.cwd (Windows path separator)', async () => {
let exitCode: number
Expand Down
2 changes: 1 addition & 1 deletion packages/exec/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ExecOptions {
/** optional working directory. defaults to current */
cwd?: string

/** optional envvar dictionary. defaults to current process's env */
/** optional envvar dictionary. defaults to current process's env with `INPUT_*` variables removed */
env?: {[key: string]: string}

/** optional. defaults to false */
Expand Down
16 changes: 15 additions & 1 deletion packages/exec/src/toolrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class ToolRunner extends events.EventEmitter {
options = options || <im.ExecOptions>{}
const result = <child.SpawnOptions>{}
result.cwd = options.cwd
result.env = options.env
result.env = options.env || stripInputEnvironmentVariables(process.env)
result['windowsVerbatimArguments'] =
options.windowsVerbatimArguments || this._isCmdFile()
if (options.windowsVerbatimArguments) {
Expand Down Expand Up @@ -600,6 +600,20 @@ export function argStringToArray(argString: string): string[] {
return args
}

// Strips INPUT_ environment variables to prevent them leaking to child processes
export function stripInputEnvironmentVariables(
env: NodeJS.ProcessEnv
): NodeJS.ProcessEnv {
return Object.entries(env)
.filter(([key]) => {
return !key.startsWith('INPUT_')
})
.reduce((obj: NodeJS.ProcessEnv, [key, value]) => {
obj[key] = value
return obj
}, {})
}

class ExecState extends events.EventEmitter {
constructor(options: im.ExecOptions, toolPath: string) {
super()
Expand Down