Skip to content

Commit 129523e

Browse files
committed
fix: process wrapper module now allows sanitising arguments
- args have a body, which is the actual argument string - they also have a boolean indication for sanitising - Skopeo now uses the new argument structure to sanitise credentials
1 parent 944a1bd commit 129523e

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/common/process.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { spawn, SpawnPromiseResult } from 'child-process-promise';
22
import logger = require('./logger');
33

4-
export function exec(bin: string, ...args: string[]):
4+
export interface IProcessArgument {
5+
body: string;
6+
sanitise: boolean;
7+
}
8+
9+
export function exec(bin: string, ...processArgs: IProcessArgument[]):
510
Promise<SpawnPromiseResult> {
611
if (process.env.DEBUG === 'true') {
7-
args.push('--debug');
12+
processArgs.push({body: '--debug', sanitise: false});
813
}
914

1015
// Ensure we're not passing the whole environment to the shelled out process...
@@ -13,11 +18,12 @@ export function exec(bin: string, ...args: string[]):
1318
PATH: process.env.PATH,
1419
};
1520

16-
return spawn(bin, args, { env, capture: [ 'stdout', 'stderr' ] })
21+
const allArguments = processArgs.map((arg) => arg.body);
22+
return spawn(bin, allArguments, { env, capture: [ 'stdout', 'stderr' ] })
1723
.catch((error) => {
1824
const message = (error && error.stderr) || 'Unknown reason';
19-
// TODO: sanitise args for secrets
20-
logger.warn({message, bin, args}, 'could not spawn the process');
25+
const loggableArguments = processArgs.filter((arg) => !arg.sanitise).map((arg) => arg.body);
26+
logger.warn({message, bin, loggableArguments}, 'could not spawn the process');
2127
throw error;
2228
});
2329
}

src/images/skopeo.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { SpawnPromiseResult } from 'child-process-promise';
22

3-
import { exec } from '../common/process';
3+
import * as processWrapper from '../common/process';
44
import * as config from'../common/config';
55
import * as credentials from './credentials';
66
import { SkopeoRepositoryType } from '../kube-scanner/types';
77

8-
98
function getUniqueIdentifier(): string {
109
const [seconds, nanoseconds] = process.hrtime();
1110
return `${seconds}_${nanoseconds}`;
@@ -38,17 +37,20 @@ export async function pull(
3837
const creds = await credentials.getSourceCredentials(image);
3938
const credentialsParameters = getCredentialParameters(creds);
4039

41-
return exec('skopeo', 'copy', ...credentialsParameters,
42-
prefixRespository(image, SkopeoRepositoryType.ImageRegistry),
43-
prefixRespository(destination, SkopeoRepositoryType.DockerArchive),
44-
);
40+
const args: Array<processWrapper.IProcessArgument> = [];
41+
args.push({body: 'copy', sanitise: false});
42+
args.push(...credentialsParameters);
43+
args.push({body: prefixRespository(image, SkopeoRepositoryType.ImageRegistry), sanitise: false});
44+
args.push({body: prefixRespository(destination, SkopeoRepositoryType.DockerArchive), sanitise: false});
45+
46+
return processWrapper.exec('skopeo', ...args);
4547
}
4648

47-
export function getCredentialParameters(credentials: string | undefined): Array<string> {
48-
const credentialsParameters: Array<string> = [];
49+
export function getCredentialParameters(credentials: string | undefined): Array<processWrapper.IProcessArgument> {
50+
const credentialsParameters: Array<processWrapper.IProcessArgument> = [];
4951
if (credentials) {
50-
credentialsParameters.push('--src-creds');
51-
credentialsParameters.push(credentials);
52+
credentialsParameters.push({body: '--src-creds', sanitise: true});
53+
credentialsParameters.push({body: credentials, sanitise: true});
5254
}
5355
return credentialsParameters;
5456
}

test/unit/skopeo.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ tap.test('getCredentialParameters()', async (t) => {
1515
const credentialParametersForSomeCredentials = skopeo.getCredentialParameters(someCredentials);
1616
t.same(
1717
credentialParametersForSomeCredentials,
18-
['--src-creds', someCredentials],
18+
[
19+
{body: '--src-creds', sanitise: true},
20+
{body: someCredentials, sanitise: true}
21+
],
1922
'returns Skopeo\'s args for source credentials',
2023
);
2124
});

0 commit comments

Comments
 (0)