1
0
mirror of https://github.com/immich-app/immich.git synced 2024-11-28 09:33:27 +02:00

Fix external library path validation #8319 (#8366)

* Fix isImmichPath

* prettier write

* Fis isImmichPath code comment

* Refactor isImmichPath function based on team suggestions

* Test isImmichPath

* fix: clean comments

* Refactor isImmichPath test based on team suggestions

* Clean code with lintern suggestions
This commit is contained in:
Pablo Diz 2024-03-31 16:47:03 +02:00 committed by GitHub
parent 34cbb18ecd
commit 6a4bc777a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,29 @@
import { StorageCore } from 'src/cores/storage.core';
jest.mock('src/constants', () => ({
APP_MEDIA_LOCATION: '/photos',
}));
describe('StorageCore', () => {
describe('isImmichPath', () => {
it('should return true for APP_MEDIA_LOCATION path', () => {
const immichPath = '/photos';
expect(StorageCore.isImmichPath(immichPath)).toBe(true);
});
it('should return true for paths within the APP_MEDIA_LOCATION', () => {
const immichPath = '/photos/new/';
expect(StorageCore.isImmichPath(immichPath)).toBe(true);
});
it('should return false for paths outside the APP_MEDIA_LOCATION and same starts', () => {
const nonImmichPath = '/photos_new';
expect(StorageCore.isImmichPath(nonImmichPath)).toBe(false);
});
it('should return false for paths outside the APP_MEDIA_LOCATION', () => {
const nonImmichPath = '/some/other/path';
expect(StorageCore.isImmichPath(nonImmichPath)).toBe(false);
});
});
});

View File

@ -115,7 +115,13 @@ export class StorageCore {
} }
static isImmichPath(path: string) { static isImmichPath(path: string) {
return resolve(path).startsWith(resolve(APP_MEDIA_LOCATION)); const resolvedPath = resolve(path);
const resolvedAppMediaLocation = resolve(APP_MEDIA_LOCATION);
const normalizedPath = resolvedPath.endsWith('/') ? resolvedPath : resolvedPath + '/';
const normalizedAppMediaLocation = resolvedAppMediaLocation.endsWith('/')
? resolvedAppMediaLocation
: resolvedAppMediaLocation + '/';
return normalizedPath.startsWith(normalizedAppMediaLocation);
} }
static isGeneratedAsset(path: string) { static isGeneratedAsset(path: string) {