Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,64 @@ export function addDevEnvVariables() {
} catch (error) {
// Ignore errors if the .env file doesn't exist
}

// Validate the loaded environment variables
validateDevEnvVariables();
}

/**
* Validate and sanitize the `DEV_USER_EXTENSIONS_PATH` environment variable.
* Removes invalid paths from the environment with logged errors.
*/
function validateDevEnvVariables() {
// Validate DEV_USER_EXTENSIONS_PATH if it was loaded
if (process.env.DEV_USER_EXTENSIONS_PATH) {
// Trim whitespace and resolve the path to an absolute path
let devPath = path.resolve(process.env.DEV_USER_EXTENSIONS_PATH.trim());

let stats: fs.Stats;
let errorMsg: string;
let errorData: Error;

// Get the file system stats for the path to check if it exists.
// statSync throws an exception if the no file system data exists for the path,
// so we catch it to handle errors gracefully.
try {
stats = fs.statSync(devPath);
} catch (error) {
const nodeError = error as NodeJS.ErrnoException;
const errorCode = nodeError.code || "UNKNOWN";

// Handle specific file system errors with user-friendly messages.
const errorMessages = {
ENOENT: "Path from env variable 'DEV_USER_EXTENSIONS_PATH' does not exist",
EACCES: "Permission denied accessing path from env variable 'DEV_USER_EXTENSIONS_PATH'",
UNKNOWN: "Unknown error accessing the path from env variable 'DEV_USER_EXTENSIONS_PATH'",
};

errorMsg = `${errorCode}: ${errorMessages[errorCode]}: "${devPath}". Removing from environment.`;

errorData = error as Error;

delete process.env.DEV_USER_EXTENSIONS_PATH;
}

// If stats has data AND the path is NOT a directory
if (stats && !stats.isDirectory()) {
errorMsg = `DEV_USER_EXTENSIONS_PATH is not a directory: "${devPath}". Removing from environment.`;
}

// If there was an error...
if (errorMsg.length > 0) {
// Delete the environment variable to prevent issues later on and log the error.
delete process.env.DEV_USER_EXTENSIONS_PATH;
logger.error(errorMsg, errorData);
}
// Otherwise, if there are no errors...
else {
// Update the environment variable with the sanitized path and log a success message.
process.env.DEV_USER_EXTENSIONS_PATH = devPath;
logger.info(`Loaded DEV_USER_EXTENSIONS_PATH: "${devPath}"`);
}
}
}