forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): Do not allow arbitrary path traversal in the credential-tr…
…anslation endpoint (n8n-io#5522)
- Loading branch information
Showing
6 changed files
with
103 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Request } from 'express'; | ||
import { ICredentialTypes } from 'n8n-workflow'; | ||
import { join } from 'path'; | ||
import { access } from 'fs/promises'; | ||
import { Get, RestController } from '@/decorators'; | ||
import { BadRequestError, InternalServerError } from '@/ResponseHelper'; | ||
import { Config } from '@/config'; | ||
import { NODES_BASE_DIR } from '@/constants'; | ||
|
||
export const CREDENTIAL_TRANSLATIONS_DIR = 'n8n-nodes-base/dist/credentials/translations'; | ||
export const NODE_HEADERS_PATH = join(NODES_BASE_DIR, 'dist/nodes/headers'); | ||
|
||
export declare namespace TranslationRequest { | ||
export type Credential = Request<{}, {}, {}, { credentialType: string }>; | ||
} | ||
|
||
@RestController('/') | ||
export class TranslationController { | ||
constructor(private config: Config, private credentialTypes: ICredentialTypes) {} | ||
|
||
@Get('/credential-translation') | ||
async getCredentialTranslation(req: TranslationRequest.Credential) { | ||
const { credentialType } = req.query; | ||
|
||
if (!this.credentialTypes.recognizes(credentialType)) | ||
throw new BadRequestError(`Invalid Credential type: "${credentialType}"`); | ||
|
||
const defaultLocale = this.config.getEnv('defaultLocale'); | ||
const translationPath = join( | ||
CREDENTIAL_TRANSLATIONS_DIR, | ||
defaultLocale, | ||
`${credentialType}.json`, | ||
); | ||
|
||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return require(translationPath); | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
@Get('/node-translation-headers') | ||
async getNodeTranslationHeaders() { | ||
try { | ||
await access(`${NODE_HEADERS_PATH}.js`); | ||
} catch (_) { | ||
return; // no headers available | ||
} | ||
|
||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return require(NODE_HEADERS_PATH); | ||
} catch (error) { | ||
throw new InternalServerError('Failed to load headers file'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/cli/test/unit/controllers/translation.controller.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
import type { ICredentialTypes } from 'n8n-workflow'; | ||
import type { Config } from '@/config'; | ||
import { | ||
TranslationController, | ||
TranslationRequest, | ||
CREDENTIAL_TRANSLATIONS_DIR, | ||
} from '@/controllers/translation.controller'; | ||
import { BadRequestError } from '@/ResponseHelper'; | ||
|
||
describe('TranslationController', () => { | ||
const config = mock<Config>(); | ||
const credentialTypes = mock<ICredentialTypes>(); | ||
const controller = new TranslationController(config, credentialTypes); | ||
|
||
describe('getCredentialTranslation', () => { | ||
it('should throw 400 on invalid credential types', async () => { | ||
const credentialType = 'not-a-valid-credential-type'; | ||
const req = mock<TranslationRequest.Credential>({ query: { credentialType } }); | ||
credentialTypes.recognizes.calledWith(credentialType).mockReturnValue(false); | ||
|
||
expect(controller.getCredentialTranslation(req)).rejects.toThrowError( | ||
new BadRequestError(`Invalid Credential type: "${credentialType}"`), | ||
); | ||
}); | ||
|
||
it('should return translation json on valid credential types', async () => { | ||
const credentialType = 'credential-type'; | ||
const req = mock<TranslationRequest.Credential>({ query: { credentialType } }); | ||
config.getEnv.calledWith('defaultLocale').mockReturnValue('de'); | ||
credentialTypes.recognizes.calledWith(credentialType).mockReturnValue(true); | ||
const response = { translation: 'string' }; | ||
jest.mock(`${CREDENTIAL_TRANSLATIONS_DIR}/de/credential-type.json`, () => response, { | ||
virtual: true, | ||
}); | ||
|
||
expect(await controller.getCredentialTranslation(req)).toEqual(response); | ||
}); | ||
}); | ||
}); |