Skip to content

Commit 0d51eb3

Browse files
committed
#39 Allow runner.temp directory
1 parent 7ae9272 commit 0d51eb3

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

dist/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function run() {
6565
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
6666
const resolvedToolpath = path.resolve(workspace, toolpath);
6767
try {
68-
(0, sanitize_1.assertWithinWorkspace)(resolvedToolpath, 'toolpath', workspace);
68+
(0, sanitize_1.assertWithinWorkspaceOrTempDirectory)(resolvedToolpath, 'toolpath', workspace);
6969
}
7070
catch (_a) {
7171
core.setFailed(`'toolpath' resolves outside the workspace: ${resolvedToolpath}`);
@@ -269,23 +269,25 @@ var __importStar = (this && this.__importStar) || (function () {
269269
};
270270
})();
271271
Object.defineProperty(exports, "__esModule", ({ value: true }));
272-
exports.assertWithinWorkspace = assertWithinWorkspace;
272+
exports.assertWithinWorkspaceOrTempDirectory = assertWithinWorkspaceOrTempDirectory;
273273
exports.assertPathsWithinWorkspace = assertPathsWithinWorkspace;
274274
exports.validateCustomSetting = validateCustomSetting;
275275
const path = __importStar(__nccwpck_require__(6928));
276-
function assertWithinWorkspace(resolvedPath, inputName, workspace) {
276+
function assertWithinWorkspaceOrTempDirectory(resolvedPath, inputName, workspace) {
277277
const normalizedResolved = path.resolve(resolvedPath);
278278
const normalizedWorkspace = path.resolve(workspace);
279-
if (normalizedResolved !== normalizedWorkspace
280-
&& !normalizedResolved.startsWith(normalizedWorkspace + path.sep)) {
281-
throw new Error(`Input '${inputName}' resolves outside the workspace: ${resolvedPath}`);
279+
const runnerTemp = process.env.RUNNER_TEMP;
280+
const normalizedRunnerTemp = runnerTemp ? path.resolve(runnerTemp) : null;
281+
if (!isWithinPath(normalizedResolved, normalizedWorkspace)
282+
&& !isWithinPath(normalizedResolved, normalizedRunnerTemp)) {
283+
throw new Error(`Input '${inputName}' resolves outside the workspace and temp directory: ${resolvedPath}`);
282284
}
283285
}
284286
function assertPathsWithinWorkspace(value, inputName, workspace) {
285287
value.split(/[;]/).forEach(segment => {
286288
const trimmed = segment.trim();
287289
if (trimmed.length > 0) {
288-
assertWithinWorkspace(path.resolve(workspace, trimmed), inputName, workspace);
290+
assertWithinWorkspaceOrTempDirectory(path.resolve(workspace, trimmed), inputName, workspace);
289291
}
290292
});
291293
}
@@ -299,6 +301,12 @@ function validateCustomSetting(setting) {
299301
}
300302
return trimmed;
301303
}
304+
function isWithinPath(normalizedPath, normalizedTargetPath) {
305+
if (normalizedPath === null || normalizedTargetPath === null) {
306+
return false;
307+
}
308+
return normalizedPath === normalizedTargetPath || normalizedPath.startsWith(normalizedTargetPath + path.sep);
309+
}
302310

303311

304312
/***/ }),

src/reportgenerator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as core from '@actions/core';
22
import * as exec from '@actions/exec';
33
import * as fs from 'fs';
44
import * as path from 'path';
5-
import { assertPathsWithinWorkspace, assertWithinWorkspace, validateCustomSetting } from './sanitize';
5+
import { assertPathsWithinWorkspace, assertWithinWorkspaceOrTempDirectory, validateCustomSetting } from './sanitize';
66

77
const VERSION = '5.5.5';
88

@@ -16,7 +16,7 @@ async function run() {
1616
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
1717
const resolvedToolpath = path.resolve(workspace, toolpath);
1818
try {
19-
assertWithinWorkspace(resolvedToolpath, 'toolpath', workspace);
19+
assertWithinWorkspaceOrTempDirectory(resolvedToolpath, 'toolpath', workspace);
2020
} catch {
2121
core.setFailed(`'toolpath' resolves outside the workspace: ${resolvedToolpath}`);
2222
return;

src/sanitize.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import * as path from 'path';
22

3-
export function assertWithinWorkspace(resolvedPath: string, inputName: string, workspace: string): void {
3+
export function assertWithinWorkspaceOrTempDirectory(resolvedPath: string, inputName: string, workspace: string): void {
44
const normalizedResolved = path.resolve(resolvedPath);
55
const normalizedWorkspace = path.resolve(workspace);
6-
if (normalizedResolved !== normalizedWorkspace
7-
&& !normalizedResolved.startsWith(normalizedWorkspace + path.sep)) {
8-
throw new Error(`Input '${inputName}' resolves outside the workspace: ${resolvedPath}`);
6+
7+
const runnerTemp = process.env.RUNNER_TEMP;
8+
const normalizedRunnerTemp = runnerTemp ? path.resolve(runnerTemp) : null;
9+
10+
if (!isWithinPath(normalizedResolved, normalizedWorkspace)
11+
&& !isWithinPath(normalizedResolved, normalizedRunnerTemp)) {
12+
throw new Error(`Input '${inputName}' resolves outside the workspace and temp directory: ${resolvedPath}`);
913
}
1014
}
1115

1216
export function assertPathsWithinWorkspace(value: string, inputName: string, workspace: string): void {
1317
value.split(/[;]/).forEach(segment => {
1418
const trimmed = segment.trim();
1519
if (trimmed.length > 0) {
16-
assertWithinWorkspace(path.resolve(workspace, trimmed), inputName, workspace);
20+
assertWithinWorkspaceOrTempDirectory(path.resolve(workspace, trimmed), inputName, workspace);
1721
}
1822
});
1923
}
@@ -30,3 +34,11 @@ export function validateCustomSetting(setting: string): string | null {
3034

3135
return trimmed;
3236
}
37+
38+
function isWithinPath(normalizedPath: string|null, normalizedTargetPath: string|null): boolean {
39+
if (normalizedPath === null || normalizedTargetPath === null) {
40+
return false;
41+
}
42+
43+
return normalizedPath === normalizedTargetPath || normalizedPath.startsWith(normalizedTargetPath + path.sep);
44+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
4747
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
4848
// "typeRoots": [], /* List of folders to include type definitions from. */
49-
// "types": [], /* Type declaration files to be included in compilation. */
49+
"types": ["node"], /* Type declaration files to be included in compilation. */
5050
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
5151
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5252
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

0 commit comments

Comments
 (0)