mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 04:04:06 -08:00
refactor: Simplify resolveIcon checks and add tests (#11165)
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
This commit is contained in:
parent
ad4cb0ea0c
commit
d4afb0f38b
|
@ -0,0 +1,37 @@
|
||||||
|
import { mock } from 'jest-mock-extended';
|
||||||
|
import type { DirectoryLoader } from 'n8n-core';
|
||||||
|
|
||||||
|
import { LoadNodesAndCredentials } from '../load-nodes-and-credentials';
|
||||||
|
|
||||||
|
describe('LoadNodesAndCredentials', () => {
|
||||||
|
describe('resolveIcon', () => {
|
||||||
|
let instance: LoadNodesAndCredentials;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
instance = new LoadNodesAndCredentials(mock(), mock(), mock());
|
||||||
|
instance.loaders.package1 = mock<DirectoryLoader>({
|
||||||
|
directory: '/icons/package1',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if the loader for the package is not found', () => {
|
||||||
|
const result = instance.resolveIcon('unknownPackage', '/icons/unknownPackage/icon.png');
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if the resolved file path is outside the loader directory', () => {
|
||||||
|
const result = instance.resolveIcon('package1', '/some/other/path/icon.png');
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the file path if the file is within the loader directory', () => {
|
||||||
|
const result = instance.resolveIcon('package1', '/icons/package1/icon.png');
|
||||||
|
expect(result).toBe('/icons/package1/icon.png');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if the URL is outside the package directory', () => {
|
||||||
|
const result = instance.resolveIcon('package1', '/icons/package1/../../../etc/passwd');
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -29,6 +29,7 @@ import {
|
||||||
inE2ETests,
|
inE2ETests,
|
||||||
} from '@/constants';
|
} from '@/constants';
|
||||||
import { Logger } from '@/logging/logger.service';
|
import { Logger } from '@/logging/logger.service';
|
||||||
|
import { isContainedWithin } from '@/utils/path-util';
|
||||||
|
|
||||||
interface LoadedNodesAndCredentials {
|
interface LoadedNodesAndCredentials {
|
||||||
nodes: INodeTypeData;
|
nodes: INodeTypeData;
|
||||||
|
@ -155,14 +156,13 @@ export class LoadNodesAndCredentials {
|
||||||
|
|
||||||
resolveIcon(packageName: string, url: string): string | undefined {
|
resolveIcon(packageName: string, url: string): string | undefined {
|
||||||
const loader = this.loaders[packageName];
|
const loader = this.loaders[packageName];
|
||||||
if (loader) {
|
if (!loader) {
|
||||||
const pathPrefix = `/icons/${packageName}/`;
|
return undefined;
|
||||||
const filePath = path.resolve(loader.directory, url.substring(pathPrefix.length));
|
|
||||||
if (!path.relative(loader.directory, filePath).includes('..')) {
|
|
||||||
return filePath;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return undefined;
|
const pathPrefix = `/icons/${packageName}/`;
|
||||||
|
const filePath = path.resolve(loader.directory, url.substring(pathPrefix.length));
|
||||||
|
|
||||||
|
return isContainedWithin(loader.directory, filePath) ? filePath : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
getCustomDirectories(): string[] {
|
getCustomDirectories(): string[] {
|
||||||
|
|
Loading…
Reference in a new issue