Skip to content

Commit

Permalink
Handle any as a part of issuecommand
Browse files Browse the repository at this point in the history
  • Loading branch information
thboop committed Apr 9, 2020
1 parent b86cd20 commit 0c74104
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
4 changes: 2 additions & 2 deletions packages/core/__tests__/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('@actions/core/src/command', () => {
])
})

it('should not crash on bad user input', () => {
it('should handle issueing commands for non-string objects', () => {
command.issueCommand(
'some-command',
{
Expand All @@ -124,7 +124,7 @@ describe('@actions/core/src/command', () => {
({test: 'object'} as unknown) as string
)
assertWriteCalls([
`::some-command prop1=[object Object],prop2=123,prop3=true::[object Object]${os.EOL}`
`::some-command prop1={\"test\"%3A\"object\"},prop2=123,prop3=true::{\"test\":\"object\"}${os.EOL}`
])
})
})
Expand Down
32 changes: 24 additions & 8 deletions packages/core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import * as os from 'os'

// For internal use, subject to change.

// We use any as a valid input type
/* eslint-disable @typescript-eslint/no-explicit-any */

interface CommandProperties {
[key: string]: string
[key: string]: any
}

/**
Expand All @@ -19,7 +22,7 @@ interface CommandProperties {
export function issueCommand(
command: string,
properties: CommandProperties,
message: string
message: any
): void {
const cmd = new Command(command, properties, message)
process.stdout.write(cmd.toString() + os.EOL)
Expand Down Expand Up @@ -63,27 +66,40 @@ class Command {
}
// safely escape the property - avoid blowing up when attempting to
// call .replace() if property is not a string for some reason
cmdStr += `${key}=${escapeProperty(`${val || ''}`)}`
cmdStr += `${key}=${escapeProperty(val)}`
}
}
}
}
// safely append the message - avoid blowing up when attempting to
// call .replace() if message is not a string for some reason
cmdStr += `${CMD_STRING}${escapeData(`${this.message || ''}`)}`
cmdStr += `${CMD_STRING}${escapeData(this.message)}`
return cmdStr
}
}

function escapeData(s: string): string {
return (s || '')
/**
* Sanatizes an input into a string so it can be passed into issueCommand safely
* @param input input to sanitize into a string
*/
export function toCommandValue(input: any): string {
if (input === null || input === undefined) {
return ''
} else if (typeof input === 'string' || input instanceof String) {
return input as string
}
return JSON.stringify(input)
}

function escapeData(s: any): string {
return toCommandValue(s)
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
}

function escapeProperty(s: string): string {
return (s || '')
function escapeProperty(s: any): string {
return toCommandValue(s)
.replace(/%/g, '%25')
.replace(/\r/g, '%0D')
.replace(/\n/g, '%0A')
Expand Down
46 changes: 13 additions & 33 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {issue, issueCommand} from './command'
import {issue, issueCommand, toCommandValue} from './command'

import * as os from 'os'
import * as path from 'path'
Expand Down Expand Up @@ -33,13 +33,11 @@ export enum ExitCode {
/**
* Sets env variable for this action and future actions in the job
* @param name the name of the variable to set
* @param val the value of the variable
* @param val the value of the variable. Will be converted to a string via JSON.stringify
*/
export function exportVariable(
name: string,
val: string | boolean | number
): void {
const convertedVal = sanitizeInput(val)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function exportVariable(name: string, val: any): void {
const convertedVal = toCommandValue(val)
process.env[name] = convertedVal
issueCommand('set-env', {name}, convertedVal)
}
Expand Down Expand Up @@ -82,13 +80,11 @@ export function getInput(name: string, options?: InputOptions): string {
* Sets the value of an output.
*
* @param name name of the output to set
* @param value value to store
* @param value value to store. Will be converted to a string via JSON.stringify
*/
export function setOutput(
name: string,
value: string | boolean | number
): void {
issueCommand('set-output', {name}, sanitizeInput(value))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function setOutput(name: string, value: any): void {
issueCommand('set-output', {name}, value)
}

//-----------------------------------------------------------------------
Expand All @@ -102,7 +98,8 @@ export function setOutput(
*/
export function setFailed(message: string | Error): void {
process.exitCode = ExitCode.Failure
error(sanitizeInput(message))

error(message)
}

//-----------------------------------------------------------------------
Expand All @@ -129,7 +126,7 @@ export function debug(message: string): void {
* @param message error issue message
*/
export function error(message: string | Error): void {
issue('error', sanitizeInput(message))
issue('error', message instanceof Error ? message.toString() : message)
}

/**
Expand Down Expand Up @@ -202,7 +199,7 @@ export function saveState(
name: string,
value: string | number | boolean
): void {
issueCommand('save-state', {name}, sanitizeInput(value))
issueCommand('save-state', {name}, value)
}

/**
Expand All @@ -214,20 +211,3 @@ export function saveState(
export function getState(name: string): string {
return process.env[`STATE_${name}`] || ''
}

/**
* Sanatizes an input into a string so it can be passed into issueCommand safely
* @param input input to sanitize into a string
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function sanitizeInput(input: any): string {
if (input == null) {
return ''
} else if (typeof input === 'string' || input instanceof String) {
return input as string
} else if (input instanceof Error) {
return input.toString()
} else {
return JSON.stringify(input)
}
}

0 comments on commit 0c74104

Please sign in to comment.