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

fix(filesystem): allow copy if from is not parent of to #546

Merged
merged 8 commits into from
Aug 23, 2021
32 changes: 31 additions & 1 deletion filesystem/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@ import type {
Directory,
} from './definitions';

function resolve(path: string): string {
const posix = path.split('/').filter(item => item !== '.');
const newPosix: string[] = [];

posix.forEach(item => {
if (
item === '..' &&
newPosix.length > 0 &&
newPosix[newPosix.length - 1] !== '..'
) {
newPosix.pop();
} else {
newPosix.push(item);
}
});

return newPosix.join('/');
}
function isPathParent(parent: string, children: string): boolean {
parent = resolve(parent);
children = resolve(children);
const pathsA = parent.split('/');
const pathsB = children.split('/');

return (
parent !== children &&
pathsA.every((value, index) => value === pathsB[index])
);
}

export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
DB_VERSION = 1;
DB_NAME = 'Disc';
Expand Down Expand Up @@ -431,7 +461,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
return;
}

if (toPath.startsWith(fromPath)) {
if (isPathParent(fromPath, toPath)) {
throw Error('To path cannot contain the from path');
}

Expand Down