Skip to content

Commit b82e662

Browse files
authored
Merge pull request #1722 from arturcic/fix/1676
#1676 - Fixes file path validation in dotnet tool
2 parents 1d898d1 + 084f803 commit b82e662

File tree

7 files changed

+32
-21
lines changed

7 files changed

+32
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ dist-ssr
3333
.debug/
3434

3535
junit-report.xml
36+
37+
src/__tests__/test.env

dist/tools/libs/gitversion.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class GitVersionTool extends DotnetTool {
122122
builder.addArgument("/nonormalize");
123123
}
124124
if (configFilePath) {
125-
if (await this.isValidInputFile("configFilePath", configFilePath)) {
125+
if (await this.isValidInputFile(workDir, configFilePath)) {
126126
builder.addArgument("/config").addArgument(configFilePath);
127127
} else {
128128
throw new Error(`GitVersion configuration file not found at ${configFilePath}`);
@@ -139,7 +139,7 @@ class GitVersionTool extends DotnetTool {
139139
if (updateAssemblyInfo) {
140140
builder.addArgument("/updateassemblyinfo");
141141
if (updateAssemblyInfoFilename) {
142-
if (await this.isValidInputFile("updateAssemblyInfoFilename", updateAssemblyInfoFilename)) {
142+
if (await this.isValidInputFile(workDir, updateAssemblyInfoFilename)) {
143143
builder.addArgument(updateAssemblyInfoFilename);
144144
} else {
145145
throw new Error(`AssemblyInfoFilename file not found at ${updateAssemblyInfoFilename}`);

dist/tools/libs/gitversion.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tools/libs/tools.mjs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,18 @@ class DotnetTool {
293293
args = ["--roll-forward Major", ...args];
294294
return await this.execute(toolPath, args);
295295
}
296-
async isValidInputFile(input, file) {
297-
return this.filePathSupplied(input) && await this.buildAgent.fileExists(file);
298-
}
299-
filePathSupplied(file) {
300-
const pathValue = path.resolve(this.buildAgent.getInput(file) || "");
301-
const repoRoot = this.buildAgent.sourceDir;
302-
return pathValue !== repoRoot;
296+
async isValidInputFile(workDir, file) {
297+
if (!file) {
298+
this.buildAgent.debug("No file path supplied");
299+
return false;
300+
}
301+
if (path.isAbsolute(file)) {
302+
this.buildAgent.debug("File path is absolute");
303+
return await this.buildAgent.fileExists(file);
304+
}
305+
const filePath = path.resolve(workDir, file);
306+
this.buildAgent.debug(`Resolved file path: ${filePath}`);
307+
return await this.buildAgent.fileExists(filePath);
303308
}
304309
async getRepoPath(targetPath) {
305310
const srcDir = this.buildAgent.sourceDir || ".";

dist/tools/libs/tools.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/common/dotnet-tool.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,18 @@ export abstract class DotnetTool implements IDotnetTool {
200200
return await this.execute(toolPath, args)
201201
}
202202

203-
protected async isValidInputFile(input: string, file: string): Promise<boolean> {
204-
return this.filePathSupplied(input) && (await this.buildAgent.fileExists(file))
205-
}
206-
207-
protected filePathSupplied(file: string): boolean {
208-
const pathValue = path.resolve(this.buildAgent.getInput(file) || '')
209-
const repoRoot = this.buildAgent.sourceDir
210-
return pathValue !== repoRoot
203+
protected async isValidInputFile(workDir: string, file: string): Promise<boolean> {
204+
if (!file) {
205+
this.buildAgent.debug('No file path supplied')
206+
return false
207+
}
208+
if (path.isAbsolute(file)) {
209+
this.buildAgent.debug('File path is absolute')
210+
return await this.buildAgent.fileExists(file)
211+
}
212+
const filePath = path.resolve(workDir, file)
213+
this.buildAgent.debug(`Resolved file path: ${filePath}`)
214+
return await this.buildAgent.fileExists(filePath)
211215
}
212216

213217
protected async getRepoPath(targetPath: string): Promise<string> {

src/tools/gitversion/tool.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class GitVersionTool extends DotnetTool {
103103
}
104104

105105
if (configFilePath) {
106-
if (await this.isValidInputFile('configFilePath', configFilePath)) {
106+
if (await this.isValidInputFile(workDir, configFilePath)) {
107107
builder.addArgument('/config').addArgument(configFilePath)
108108
} else {
109109
throw new Error(`GitVersion configuration file not found at ${configFilePath}`)
@@ -124,7 +124,7 @@ export class GitVersionTool extends DotnetTool {
124124

125125
// You can specify 'updateAssemblyInfo' without 'updateAssemblyInfoFilename'.
126126
if (updateAssemblyInfoFilename) {
127-
if (await this.isValidInputFile('updateAssemblyInfoFilename', updateAssemblyInfoFilename)) {
127+
if (await this.isValidInputFile(workDir, updateAssemblyInfoFilename)) {
128128
builder.addArgument(updateAssemblyInfoFilename)
129129
} else {
130130
throw new Error(`AssemblyInfoFilename file not found at ${updateAssemblyInfoFilename}`)

0 commit comments

Comments
 (0)