Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion src/js/utils/pathHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,23 @@ export function resolveRelativePath(basePath, relativePath) {
export function normalizePath(path) {
if (!path) return '';

return path
const cleanPath = path
.replace(/\\/g, '/') // Convert backslashes to forward slashes
.replace(/\/+/g, '/') // Collapse multiple slashes
.replace(/^\/|\/$/g, ''); // Remove leading/trailing slashes

const segments = cleanPath.split('/');
const stack = [];

for (const segment of segments) {
if (segment === '..') {
stack.pop();
} else if (segment !== '.' && segment !== '') {
stack.push(segment);
}
}

return stack.join('/');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/services/LinkNavigationService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ describe('LinkNavigationService', () => {
name: 'test.md',
path: 'docs/test.md',
handle: mockFileHandle,
anchor: null,
});
});

Expand Down Expand Up @@ -253,6 +254,7 @@ describe('LinkNavigationService', () => {
name: 'test.md',
path: 'docs/test.md',
handle: mockFileHandle,
anchor: null,
});
});

Expand Down
5 changes: 1 addition & 4 deletions tests/unit/utils/pathHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,7 @@ describe('pathHelpers', () => {
});

it('should handle normalized paths that resolve outside root', () => {
// Normalization converts 'docs/../secret' to 'secret'
// The implementation normalizes the path but doesn't prevent paths that resolved outside root
// It only checks if the normalized path starts with '..' or is empty
expect(isWithinRoot('docs/../secret', 'docs')).toBe(true); // 'secret' is valid normalized path
expect(isWithinRoot('docs/../secret', 'docs')).toBe(false);
});

it('should allow nested paths within root', () => {
Expand Down