Skip to content

Commit

Permalink
Bugfix isAtPath to accept identical prefix/path values
Browse files Browse the repository at this point in the history
The previous fix checking for length being less than 2 was not accurate
because it was letting paths with just a trailing slash to pass through.
We roll it back to checking if the length is exactly 1 in difference, and
also do an earlier check for prefix and path being identical in order to
better handle the exception cases.

Signed-off-by: Jeremy Ho <jujaga@gmail.com>
  • Loading branch information
jujaga committed Aug 31, 2023
1 parent 6855c10 commit af39350
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/src/components/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,12 @@ const utils = {
*/
isAtPath(prefix, path) {
if (typeof prefix !== 'string' || typeof path !== 'string') return false;
if (prefix === path) return true; // Matching strings are always at the at the path

const pathParts = path.split(DELIMITER).filter(part => part);
const prefixParts = prefix.split(DELIMITER).filter(part => part);
return prefixParts.every((part, i) => pathParts[i] === part)
&& pathParts.filter(part => !prefixParts.includes(part)).length < 2;
&& pathParts.filter(part => !prefixParts.includes(part)).length === 1;
},

/**
Expand Down
6 changes: 4 additions & 2 deletions app/tests/unit/components/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,22 @@ describe('isAtPath', () => {
it.each([
[false, undefined, undefined],
[false, null, null],
[true, '', ''], // Root level empty string identies should technically be true
[true, '', ''], // Root level empty string identities should technically be true
[true, '', 'file'],
[false, '', 'file/bleep'],
[true, '/', 'file'],
[false, '/', 'file/bleep'],
[true, 'foo', 'foo'], // Root level file identies should be true
[true, 'foo', 'foo'], // Root level file identities should be true
[false, 'foo', 'bar'], // Non-matching root level path and prefix should be false
[true, 'foo', 'foo/bar'],
[true, 'foo', '/foo/bar'],
[true, '/foo', 'foo/bar'],
[true, '/foo', '/foo/bar'],
[true, 'a/b', 'a/b/foo.jpg'],
[false, 'a/b', 'a/b/'], // Trailing slashes references the folder and should be excluded
[false, 'a/b', 'a/b/z/deep.jpg'],
[false, 'a/b', 'a/b/y/z/deep.jpg'],
[false, 'a/b/c', 'a/b/c/'], // Trailing slashes references the folder and should be excluded
[false, 'a/b/c', 'a/bar.png'],
[false, 'c/b/a', 'a/b/c/bar.png'],
[false, 'c/a/b', 'a/b/c/bar.png'],
Expand Down

0 comments on commit af39350

Please sign in to comment.