Skip to content

Commit

Permalink
fix(filesystem): allow copy if from is not parent of to (#546)
Browse files Browse the repository at this point in the history
Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
tachibana-shin and jcesarmobile authored Aug 23, 2021
1 parent 4271b5d commit a70414e
Showing 1 changed file with 31 additions and 1 deletion.
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

0 comments on commit a70414e

Please sign in to comment.