Allow generated user config file to be outside the workspace

This commit is contained in:
Henry Mercer 2024-02-15 11:56:11 +00:00
parent d88d538126
commit 381e65f460
3 changed files with 32 additions and 22 deletions

23
lib/config-utils.js generated
View file

@ -257,9 +257,15 @@ async function downloadCacheWithTime(trapCachingEnabled, codeQL, languages, logg
async function loadConfig({ languagesInput, queriesInput, packsInput, buildModeInput, configFile, dbLocation, trapCachingEnabled, debugMode, debugArtifactName, debugDatabaseName, repository, tempDir, codeql, workspacePath, githubVersion, apiDetails, features, logger, }) { async function loadConfig({ languagesInput, queriesInput, packsInput, buildModeInput, configFile, dbLocation, trapCachingEnabled, debugMode, debugArtifactName, debugDatabaseName, repository, tempDir, codeql, workspacePath, githubVersion, apiDetails, features, logger, }) {
let parsedYAML; let parsedYAML;
if (isLocal(configFile)) { if (isLocal(configFile)) {
// Treat the config file as relative to the workspace if (configFile !== userConfigFromActionPath(tempDir)) {
configFile = path.resolve(workspacePath, configFile); // If the config file is not generated by the Action, it should be relative to the workspace.
parsedYAML = getLocalConfig(configFile, workspacePath); configFile = path.resolve(workspacePath, configFile);
// Error if the config file is now outside of the workspace
if (!(configFile + path.sep).startsWith(workspacePath + path.sep)) {
throw new util_1.ConfigurationError(getConfigFileOutsideWorkspaceErrorMessage(configFile));
}
}
parsedYAML = getLocalConfig(configFile);
} }
else { else {
parsedYAML = await getRemoteConfig(configFile, apiDetails); parsedYAML = await getRemoteConfig(configFile, apiDetails);
@ -457,6 +463,9 @@ function shouldCombine(inputValue) {
function dbLocationOrDefault(dbLocation, tempDir) { function dbLocationOrDefault(dbLocation, tempDir) {
return dbLocation || path.resolve(tempDir, "codeql_databases"); return dbLocation || path.resolve(tempDir, "codeql_databases");
} }
function userConfigFromActionPath(tempDir) {
return path.resolve(tempDir, "user-config-from-action.yml");
}
/** /**
* Load and return the config. * Load and return the config.
* *
@ -471,7 +480,7 @@ async function initConfig(inputs) {
if (inputs.configFile) { if (inputs.configFile) {
logger.warning(`Both a config file and config input were provided. Ignoring config file.`); logger.warning(`Both a config file and config input were provided. Ignoring config file.`);
} }
inputs.configFile = path.resolve(tempDir, "user-config-from-action.yml"); inputs.configFile = userConfigFromActionPath(tempDir);
fs.writeFileSync(inputs.configFile, inputs.configInput); fs.writeFileSync(inputs.configFile, inputs.configInput);
logger.debug(`Using config from action input: ${inputs.configFile}`); logger.debug(`Using config from action input: ${inputs.configFile}`);
} }
@ -506,11 +515,7 @@ function isLocal(configPath) {
} }
return configPath.indexOf("@") === -1; return configPath.indexOf("@") === -1;
} }
function getLocalConfig(configFile, workspacePath) { function getLocalConfig(configFile) {
// Error if the config file is now outside of the workspace
if (!(configFile + path.sep).startsWith(workspacePath + path.sep)) {
throw new util_1.ConfigurationError(getConfigFileOutsideWorkspaceErrorMessage(configFile));
}
// Error if the file does not exist // Error if the file does not exist
if (!fs.existsSync(configFile)) { if (!fs.existsSync(configFile)) {
throw new util_1.ConfigurationError(getConfigFileDoesNotExistErrorMessage(configFile)); throw new util_1.ConfigurationError(getConfigFileDoesNotExistErrorMessage(configFile));

File diff suppressed because one or more lines are too long

View file

@ -543,9 +543,17 @@ async function loadConfig({
let parsedYAML: UserConfig; let parsedYAML: UserConfig;
if (isLocal(configFile)) { if (isLocal(configFile)) {
// Treat the config file as relative to the workspace if (configFile !== userConfigFromActionPath(tempDir)) {
configFile = path.resolve(workspacePath, configFile); // If the config file is not generated by the Action, it should be relative to the workspace.
parsedYAML = getLocalConfig(configFile, workspacePath); configFile = path.resolve(workspacePath, configFile);
// Error if the config file is now outside of the workspace
if (!(configFile + path.sep).startsWith(workspacePath + path.sep)) {
throw new ConfigurationError(
getConfigFileOutsideWorkspaceErrorMessage(configFile),
);
}
}
parsedYAML = getLocalConfig(configFile);
} else { } else {
parsedYAML = await getRemoteConfig(configFile, apiDetails); parsedYAML = await getRemoteConfig(configFile, apiDetails);
} }
@ -823,6 +831,10 @@ function dbLocationOrDefault(
return dbLocation || path.resolve(tempDir, "codeql_databases"); return dbLocation || path.resolve(tempDir, "codeql_databases");
} }
function userConfigFromActionPath(tempDir: string): string {
return path.resolve(tempDir, "user-config-from-action.yml");
}
/** /**
* Load and return the config. * Load and return the config.
* *
@ -841,7 +853,7 @@ export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
`Both a config file and config input were provided. Ignoring config file.`, `Both a config file and config input were provided. Ignoring config file.`,
); );
} }
inputs.configFile = path.resolve(tempDir, "user-config-from-action.yml"); inputs.configFile = userConfigFromActionPath(tempDir);
fs.writeFileSync(inputs.configFile, inputs.configInput); fs.writeFileSync(inputs.configFile, inputs.configInput);
logger.debug(`Using config from action input: ${inputs.configFile}`); logger.debug(`Using config from action input: ${inputs.configFile}`);
} }
@ -883,14 +895,7 @@ function isLocal(configPath: string): boolean {
return configPath.indexOf("@") === -1; return configPath.indexOf("@") === -1;
} }
function getLocalConfig(configFile: string, workspacePath: string): UserConfig { function getLocalConfig(configFile: string): UserConfig {
// Error if the config file is now outside of the workspace
if (!(configFile + path.sep).startsWith(workspacePath + path.sep)) {
throw new ConfigurationError(
getConfigFileOutsideWorkspaceErrorMessage(configFile),
);
}
// Error if the file does not exist // Error if the file does not exist
if (!fs.existsSync(configFile)) { if (!fs.existsSync(configFile)) {
throw new ConfigurationError( throw new ConfigurationError(