-
Notifications
You must be signed in to change notification settings - Fork 13.5k
fix: Prevent SSRF in import and app package URL fetches #40338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,60 @@ | ||
| import fs from 'fs'; | ||
| import http from 'http'; | ||
| import https from 'https'; | ||
|
|
||
| import { Import } from '@rocket.chat/core-services'; | ||
| import type { IUser } from '@rocket.chat/core-typings'; | ||
| import type { ServerMethods } from '@rocket.chat/ddp-client'; | ||
| import { serverFetch as fetch } from '@rocket.chat/server-fetch'; | ||
| import { Meteor } from 'meteor/meteor'; | ||
|
|
||
| import { Importers } from '..'; | ||
| import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; | ||
| import { settings } from '../../../settings/server'; | ||
| import { ProgressStep } from '../../lib/ImporterProgressStep'; | ||
| import { RocketChatImportFileInstance } from '../startup/store'; | ||
|
|
||
| function downloadHttpFile(fileUrl: string, writeStream: fs.WriteStream): void { | ||
| const protocol = fileUrl.startsWith('https') ? https : http; | ||
| protocol.get(fileUrl, (response) => { | ||
| response.pipe(writeStream); | ||
| const getPublicImportUrl = (fileUrl: string): URL => { | ||
| try { | ||
| const url = new URL(fileUrl); | ||
| if (url.protocol !== 'http:' && url.protocol !== 'https:') { | ||
| throw new Error('Invalid protocol'); | ||
| } | ||
|
|
||
| return url; | ||
| } catch { | ||
| throw new Meteor.Error('error-invalid-url', 'Import files must be downloaded from a valid HTTP or HTTPS URL.', 'downloadPublicImportFile'); | ||
| } | ||
| }; | ||
|
|
||
| async function downloadHttpFile(fileUrl: string, writeStream: ReturnType<typeof RocketChatImportFileInstance.createWriteStream>): Promise<void> { | ||
| const response = await fetch(fileUrl, { | ||
| ignoreSsrfValidation: false, | ||
| allowList: settings.get<string>('SSRF_Allowlist'), | ||
| }); | ||
| } | ||
|
|
||
| function copyLocalFile(filePath: fs.PathLike, writeStream: fs.WriteStream): void { | ||
| const readStream = fs.createReadStream(filePath); | ||
| readStream.pipe(writeStream); | ||
| if (response.status !== 200) { | ||
| throw new Meteor.Error('error-import-file-download-failed', 'Failed to download import file.', 'downloadPublicImportFile'); | ||
| } | ||
|
|
||
| const fileBuffer = Buffer.from(await response.arrayBuffer()); | ||
| await new Promise<void>((resolve, reject) => { | ||
| writeStream.once('error', reject); | ||
| writeStream.end(fileBuffer, resolve); | ||
| }); | ||
|
Comment on lines
+36
to
+40
|
||
| } | ||
|
|
||
| export const executeDownloadPublicImportFile = async (userId: IUser['_id'], fileUrl: string, importerKey: string): Promise<void> => { | ||
| const importer = Importers.get(importerKey); | ||
| const isUrl = fileUrl.startsWith('http'); | ||
| const publicImportUrl = getPublicImportUrl(fileUrl); | ||
| if (!importer) { | ||
| throw new Meteor.Error( | ||
| 'error-importer-not-defined', | ||
| `The importer (${importerKey}) has no import class defined.`, | ||
| 'downloadImportFile', | ||
| ); | ||
| } | ||
| // Check if it's a valid url or path before creating a new import record | ||
| if (!isUrl && !fs.existsSync(fileUrl)) { | ||
| throw new Meteor.Error('error-import-file-missing', fileUrl, 'downloadPublicImportFile'); | ||
| } | ||
|
|
||
| const operation = await Import.newOperation(userId, importer.name, importer.key); | ||
| const instance = new importer.importer(importer, operation); // eslint-disable-line new-cap | ||
|
|
||
| const oldFileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1).split('?')[0]; | ||
| const oldFileName = publicImportUrl.pathname.substring(publicImportUrl.pathname.lastIndexOf('/') + 1) || 'import-file'; | ||
| const date = new Date(); | ||
| const dateStr = `${date.getUTCFullYear()}${date.getUTCMonth()}${date.getUTCDate()}${date.getUTCHours()}${date.getUTCMinutes()}${date.getUTCSeconds()}`; | ||
| const newFileName = `${dateStr}_${userId}_${oldFileName}`; | ||
|
|
@@ -57,21 +69,16 @@ export const executeDownloadPublicImportFile = async (userId: IUser['_id'], file | |
| void instance.updateProgress(ProgressStep.ERROR); | ||
| }); | ||
|
|
||
| writeStream.on('end', () => { | ||
| writeStream.on('finish', () => { | ||
| void instance.updateProgress(ProgressStep.FILE_LOADED); | ||
| }); | ||
|
|
||
| if (isUrl) { | ||
| downloadHttpFile(fileUrl, writeStream); | ||
| } else { | ||
| // If the url is actually a folder path on the current machine, skip moving it to the file store | ||
| if (fs.statSync(fileUrl).isDirectory()) { | ||
| await instance.updateRecord({ file: fileUrl }); | ||
| await instance.updateProgress(ProgressStep.FILE_LOADED); | ||
| return; | ||
| } | ||
|
|
||
| copyLocalFile(fileUrl, writeStream); | ||
| try { | ||
| await downloadHttpFile(publicImportUrl.toString(), writeStream); | ||
| } catch (error) { | ||
| writeStream.destroy(); | ||
| await instance.updateProgress(ProgressStep.ERROR); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now reads the full remote import file into memory (
response.arrayBuffer()thenBuffer.from(...)) before writing it to storage, which is a regression from the previous streaming behavior and can exhaust Node heap on large imports. In production, a sufficiently large CSV/JSON export can cause high memory spikes or process crashes duringdownloadPublicImportFile, interrupting imports for all users. Keep the SSRF validation, but write from the response stream towriteStreamto preserve bounded memory usage.Useful? React with 👍 / 👎.