Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved validation for goIntoDirectory #2801

Merged
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
35 changes: 21 additions & 14 deletions src/common/types/chainner-builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,20 @@ export const getParentDirectory = wrapBinary<StringPrimitive, Int, StringPrimiti

// eslint-disable-next-line no-control-regex
const INVALID_PATH_CHARS = /[<>:"|?*\x00-\x1F]/;
const goIntoDirectoryImpl = (basePath: string, relPath: string): string | Error => {
const validateRelPath = (relPath: string): string | undefined => {
const isAbsolute = /^[/\\]/.test(relPath) || path.isAbsolute(relPath);
if (isAbsolute) {
return new Error('Absolute paths are not allowed as folders.');
return 'Absolute paths are not allowed as folders.';
}

const invalid = INVALID_PATH_CHARS.exec(relPath);
if (invalid) {
return new Error(`Invalid character '${invalid[0]}' in folder name.`);
return `Invalid character '${invalid[0]}' in folder name.`;
}

return undefined;
};
const goIntoDirectoryImpl = (basePath: string, relPath: string): string => {
const joined = path.join(basePath, relPath);
return path.resolve(joined);
};
Expand All @@ -438,20 +441,24 @@ export const goIntoDirectory = wrapScopedBinary(
): Arg<StringPrimitive | StructInstanceType> => {
const errorDesc = getStructDescriptor(scope, 'Error');

if (basePath.type === 'literal' && relPath.type === 'literal') {
try {
const result = goIntoDirectoryImpl(basePath.value, relPath.value);
if (typeof result === 'string') {
try {
if (relPath.type === 'literal') {
const error = validateRelPath(relPath.value);
if (error) {
return createInstance(errorDesc, {
message: literal(error),
});
}

if (basePath.type === 'literal') {
const result = goIntoDirectoryImpl(basePath.value, relPath.value);
return literal(result);
}
return createInstance(errorDesc, {
message: literal(result.message),
});
} catch (e) {
return createInstance(errorDesc, {
message: literal(String(e)),
});
}
} catch (e) {
return createInstance(errorDesc, {
message: literal(String(e)),
});
}

return union(StringType.instance, errorDesc.default);
Expand Down
Loading