Skip to content

Commit abbcafe

Browse files
authored
Fix secure linter warnings (#457)
* fix object injection sinks (disable warning for test code) Signed-off-by: Jens Reinecke <[email protected]> * use of variables in fs functions Signed-off-by: Jens Reinecke <[email protected]> --------- Signed-off-by: Jens Reinecke <[email protected]>
1 parent 1209fe9 commit abbcafe

File tree

8 files changed

+46
-17
lines changed

8 files changed

+46
-17
lines changed

src/__test__/test-data-factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export function makeFactory<T extends object>(initializer: Initializer<T>): Fact
3535
const result = { ...options } as Mutable<T>;
3636
for (const key in initializer) {
3737
if (!(key in result)) {
38+
// disable rule for next line, not used in production code
39+
// eslint-disable-next-line security/detect-object-injection
3840
result[key] = initializer[key].call(result, result);
3941
}
4042
}

src/cbuild-run/cbuild-run-reader.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,29 @@
1515
*/
1616

1717
import * as yaml from 'yaml';
18-
import { CbuildRunType } from './cbuild-run-types';
18+
import { CbuildRunRootType, CbuildRunType } from './cbuild-run-types';
1919
import { FileReader, VscodeFileReader } from '../desktop/file-reader';
2020
import { getCmsisPackRootPath } from '../utils';
2121

22-
const ROOT_NODE = 'cbuild-run';
2322
const CMSIS_PACK_ROOT_ENVVAR = '${CMSIS_PACK_ROOT}';
2423

2524
export class CbuildRunReader {
26-
private cbuildRun?: CbuildRunType;
25+
private cbuildRun: CbuildRunType | undefined;
2726

2827
constructor(private reader: FileReader = new VscodeFileReader()) {}
2928

3029
public hasContents(): boolean {
3130
return !!this.cbuildRun;
3231
}
3332

34-
public getContents(): CbuildRunType|undefined {
33+
public getContents(): CbuildRunType | undefined {
3534
return this.cbuildRun;
3635
}
3736

3837
public async parse(filePath: string): Promise<void> {
3938
const fileContents = await this.reader.readFileToString(filePath);
40-
const fileRoot = yaml.parse(fileContents);
41-
this.cbuildRun = fileRoot ? fileRoot[ROOT_NODE] : undefined;
39+
const fileRoot = yaml.parse(fileContents) as CbuildRunRootType;
40+
this.cbuildRun = fileRoot ? fileRoot['cbuild-run'] : undefined;
4241
if (!this.cbuildRun) {
4342
throw new Error(`Invalid '*.cbuild-run.yml' file: ${filePath}`);
4443
}

src/cbuild-run/cbuild-run-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,7 @@ export interface CbuildRunType {
173173
programming?: ProgrammingType[];
174174
'debug-topology'?: DebugTopologyType;
175175
};
176+
177+
export interface CbuildRunRootType {
178+
'cbuild-run': CbuildRunType;
179+
};

src/debug-configuration/gdbtarget-configuration-provider.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ export class GDBTargetConfigurationProvider implements vscode.DebugConfiguration
7171
logger.debug(`\t${resolvedGDBConfig.target?.server} ${resolvedGDBConfig.target?.serverParameters?.join(' ')}`);
7272
}
7373

74+
private hasResolverFunction(resolverType: ResolverType, provider: vscode.DebugConfigurationProvider): boolean {
75+
switch (resolverType) {
76+
case 'resolveDebugConfiguration':
77+
return !!provider.resolveDebugConfiguration;
78+
case 'resolveDebugConfigurationWithSubstitutedVariables':
79+
return !!provider.resolveDebugConfigurationWithSubstitutedVariables;
80+
}
81+
}
82+
7483
private isRelevantSubprovider(resolverType: ResolverType, serverType: string, subProvider: GDBTargetConfigurationSubProvider): boolean {
7584
const serverTypeMatch = subProvider.serverRegExp.test(serverType);
76-
const hasResolverFunction = !!subProvider.provider[resolverType];
77-
return serverTypeMatch && hasResolverFunction;
85+
return serverTypeMatch && this.hasResolverFunction(resolverType, subProvider.provider);
7886
}
7987

8088
private getRelevantSubproviders(resolverType: ResolverType, serverType?: string): GDBTargetConfigurationSubProvider[] {
@@ -96,7 +104,7 @@ export class GDBTargetConfigurationProvider implements vscode.DebugConfiguration
96104
subproviders.forEach((subprovider, index) => logger.warn(`#${index}: '${subprovider.serverRegExp}'`));
97105
}
98106
const relevantProvider = subproviders[0];
99-
if (!relevantProvider.provider[resolverType]) {
107+
if (!this.hasResolverFunction(resolverType, relevantProvider.provider)) {
100108
logger.debug(`${resolverType}: Subprovider '${relevantProvider.serverRegExp}' does not implement '${resolverType}'.`);
101109
return undefined;
102110
}
@@ -118,7 +126,9 @@ export class GDBTargetConfigurationProvider implements vscode.DebugConfiguration
118126
return debugConfiguration;
119127
}
120128
logger.debug(`${resolverType}: Resolve config with subprovider '${subprovider.serverRegExp}'`);
121-
const resolvedConfig = await subprovider.provider[resolverType]!(folder, debugConfiguration, token);
129+
const resolvedConfig = resolverType === 'resolveDebugConfiguration'
130+
? await subprovider.provider.resolveDebugConfiguration!(folder, debugConfiguration, token)
131+
: await subprovider.provider.resolveDebugConfigurationWithSubstitutedVariables!(folder, debugConfiguration, token);
122132
if (!resolvedConfig) {
123133
logger.error(`${resolverType}: Resolving config failed with subprovider '${subprovider.serverRegExp}'`);
124134
return undefined;

src/debug-configuration/gdbtarget-configuration.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@ export interface GDBTargetConfiguration extends vscode.DebugConfiguration {
7575
target?: TargetConfiguration;
7676
cmsis?: CMSISConfiguration;
7777
};
78+
79+
export interface ExtendedGDBTargetConfiguration extends GDBTargetConfiguration {
80+
// For additional configuration items not part of GDBTargetConfiguration but known to other extensions
81+
definitionPath?: string; // Default SVD path setting for Peripheral Inspector
82+
};

src/debug-configuration/subproviders/base-configuration-provider.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
*/
1616

1717
import * as vscode from 'vscode';
18-
import { GDBTargetConfiguration } from '../gdbtarget-configuration';
18+
import { ExtendedGDBTargetConfiguration, GDBTargetConfiguration } from '../gdbtarget-configuration';
1919
import { CbuildRunReader } from '../../cbuild-run';
2020
import { logger } from '../../logger';
2121

22-
const DEFAULT_SVD_SETTING_NAME = 'definitionPath';
23-
2422
export abstract class BaseConfigurationProvider implements vscode.DebugConfigurationProvider {
2523
protected _cbuildRunReader?: CbuildRunReader;
2624

@@ -54,14 +52,15 @@ export abstract class BaseConfigurationProvider implements vscode.DebugConfigura
5452
}
5553

5654
protected resolveSvdFile(debugConfiguration: GDBTargetConfiguration) {
55+
const extDebugConfig = debugConfiguration as ExtendedGDBTargetConfiguration;
5756
const cbuildRunFilePath = debugConfiguration.cmsis?.cbuildRunFile;
5857
// 'definitionPath' is current default name for SVD file settings in Eclipse CDT Cloud Peripheral Inspector.
59-
if (debugConfiguration[DEFAULT_SVD_SETTING_NAME] || !cbuildRunFilePath?.length) {
58+
if (extDebugConfig.definitionPath !== undefined || !cbuildRunFilePath?.length) {
6059
return;
6160
}
6261
const svdFilePaths = this.cbuildRunReader.getSvdFilePaths(debugConfiguration?.target?.environment?.CMSIS_PACK_ROOT);
6362
// Needs update when we better support multiple `debugger:` YAML nodes
64-
debugConfiguration[DEFAULT_SVD_SETTING_NAME] = svdFilePaths[0];
63+
extDebugConfig.definitionPath = svdFilePaths[0];
6564
}
6665

6766
protected abstract resolveServerParameters(debugConfiguration: GDBTargetConfiguration): Promise<GDBTargetConfiguration>;

src/desktop/builtin-tool-path.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ describe('BuiltinToolPath', () => {
4141
it('should return the correct path for a given tool', () => {
4242
const builtinToolPath = new BuiltinToolPath('tools/pyocd/pyocd');
4343

44+
// eslint-disable-next-line security/detect-non-literal-fs-filename
4445
fs.mkdirSync(`${testFolder}/tools/pyocd`, { recursive: true });
46+
// eslint-disable-next-line security/detect-non-literal-fs-filename
4547
fs.writeFileSync(`${testFolder}/tools/pyocd/pyocd${TOOL_EXTENSION}`, '');
4648

4749
const expected = vscode.Uri.file(`${testFolder}/tools/pyocd/pyocd${TOOL_EXTENSION}`);
@@ -58,7 +60,9 @@ describe('BuiltinToolPath', () => {
5860

5961
it('should return the directory of the tool', () => {
6062
const builtinToolPath = new BuiltinToolPath('tools/pyocd/pyocd');
63+
// eslint-disable-next-line security/detect-non-literal-fs-filename
6164
fs.mkdirSync(`${testFolder}/tools/pyocd`, { recursive: true });
65+
// eslint-disable-next-line security/detect-non-literal-fs-filename
6266
fs.writeFileSync(`${testFolder}/tools/pyocd/pyocd${TOOL_EXTENSION}`, '');
6367

6468
const expected = vscode.Uri.file(`${testFolder}/tools/pyocd`);

src/desktop/builtin-tool-path.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ export class BuiltinToolPath {
2929
public getAbsolutePath(): vscode.Uri | undefined {
3030
const extensionUri = vscode.extensions.getExtension(EXTENSION_ID)?.extensionUri;
3131
const absoluteUri = extensionUri?.with({ path: `${extensionUri.path}/${this.toolPath}${isWindows ? '.exe' : ''}` });
32-
const fsPath = absoluteUri?.fsPath;
33-
return (fsPath && fs.existsSync(fsPath)) ? absoluteUri : undefined;
32+
if (!absoluteUri?.fsPath) {
33+
return undefined;
34+
}
35+
const normalizedUri = vscode.Uri.file(path.normalize(absoluteUri.fsPath));
36+
const normalizedFsPath = normalizedUri.fsPath;
37+
// Cannot avoid it, but also no real user input here. `toolPath` only given programmatically.
38+
// eslint-disable-next-line security/detect-non-literal-fs-filename
39+
return fs.existsSync(normalizedFsPath) ? normalizedUri : undefined;
3440
}
3541

3642
public getAbsolutePathDir(): string | undefined{

0 commit comments

Comments
 (0)