Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
feat(backend:files): support trusted private IP downloads
johaven44261ea20e83
diff --git a/backend/src/applications/files/interfaces/download-file.interface.ts b/backend/src/applications/files/interfaces/download-file.interface.tsindex 8cb6487d..a094eb3f 100644--- a/backend/src/applications/files/interfaces/download-file.interface.ts+++ b/backend/src/applications/files/interfaces/download-file.interface.ts@@ -7,6 +7,7 @@ export interface DownloadFileContentInfo {}export interface DownloadFileOptions {+ allowPrivateIP?: booleanspace?: SpaceEnvgetContentInfo?: boolean}diff --git a/backend/src/applications/files/utils/download-file.spec.ts b/backend/src/applications/files/utils/download-file.spec.tsnew file mode 100644index 00000000..f499e6dd--- /dev/null+++ b/backend/src/applications/files/utils/download-file.spec.ts@@ -0,0 +1,90 @@+import { HttpService } from '@nestjs/axios'+import { HttpStatus } from '@nestjs/common'+import { Readable } from 'node:stream'+import { HTTP_METHOD } from '../../applications.constants'+import { FileError } from '../models/file-error'+import { writeFromStream } from './files'+import { downloadFile } from './download-file'++jest.mock('./files', () => ({+ writeFromStream: jest.fn()+}))++describe(downloadFile.name, () => {+ let http: { axiosRef: jest.Mock }++ const response = (remoteAddress: string, headers: Record<string, string> = {}) => ({+ headers,+ request: { socket: { remoteAddress } }+ })++ beforeEach(() => {+ http = { axiosRef: jest.fn() }+ ;(writeFromStream as jest.Mock).mockResolvedValue(undefined)+ })++ afterEach(() => {+ jest.clearAllMocks()+ })++ it('rejects private IPs on HEAD by default', async () => {+ http.axiosRef.mockResolvedValueOnce(response('127.0.0.1', { 'content-length': '12' }))++ await expect(downloadFile(http as unknown as HttpService, { url: 'https://example.test/file.txt' }, '/tmp/file.txt')).rejects.toEqual(+ new FileError(HttpStatus.FORBIDDEN, 'Access to internal IP addresses is forbidden')+ )++ expect(http.axiosRef).toHaveBeenCalledTimes(1)+ expect(http.axiosRef).toHaveBeenCalledWith({ method: HTTP_METHOD.HEAD, url: 'https://example.test/file.txt', maxRedirects: 1 })+ expect(writeFromStream).not.toHaveBeenCalled()+ })++ it('allows private IPs on HEAD when allowPrivateIP is enabled for content info', async () => {+ http.axiosRef.mockResolvedValueOnce(+ response('127.0.0.1', {+ 'content-length': '12',+ 'content-type': 'image/png',+ 'last-modified': 'Mon, 01 Jan 2024 00:00:00 GMT'+ })+ )++ const result = await downloadFile(http as unknown as HttpService, { url: 'https://example.test/avatar.png' }, '/tmp/avatar.png', {+ allowPrivateIP: true,+ getContentInfo: true+ })++ expect(result).toEqual({+ contentLength: 12,+ contentType: 'image/png',+ lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT'+ })+ expect(http.axiosRef).toHaveBeenCalledTimes(1)+ expect(writeFromStream).not.toHaveBeenCalled()+ })++ it('rejects private IPs on GET by default and closes the stream', async () => {+ const stream = Readable.from(['abc'])+ const destroySpy = jest.spyOn(stream, 'destroy')+ http.axiosRef+ .mockResolvedValueOnce(response('8.8.8.8', { 'content-length': '12' }))+ .mockResolvedValueOnce({ ...response('10.0.0.7'), data: stream })++ await expect(downloadFile(http as unknown as HttpService, { url: 'https://example.test/file.txt' }, '/tmp/file.txt')).rejects.toEqual(+ new FileError(HttpStatus.FORBIDDEN, 'Access to internal IP addresses is forbidden')+ )++ expect(destroySpy).toHaveBeenCalled()+ expect(writeFromStream).not.toHaveBeenCalled()+ })++ it('allows private IPs on GET when allowPrivateIP is enabled', async () => {+ const stream = Readable.from(['abc'])+ http.axiosRef+ .mockResolvedValueOnce(response('127.0.0.1', { 'content-length': '12' }))+ .mockResolvedValueOnce({ ...response('10.0.0.7'), data: stream })++ await downloadFile(http as unknown as HttpService, { url: 'https://example.test/file.txt' }, '/tmp/file.txt', { allowPrivateIP: true })++ expect(writeFromStream).toHaveBeenCalledWith('/tmp/file.txt', stream)+ })+})diff --git a/backend/src/applications/files/utils/download-file.ts b/backend/src/applications/files/utils/download-file.tsindex 9dd89215..5b00dbec 100644--- a/backend/src/applications/files/utils/download-file.ts+++ b/backend/src/applications/files/utils/download-file.ts@@ -40,13 +40,13 @@ export async function downloadFile(http: HttpService,downloadDto: DownloadFileDto,dstPath: string,- options: { space?: SpaceEnv; getContentInfo: true }+ options: { allowPrivateIP?: boolean; space?: SpaceEnv; getContentInfo: true }): Promise<DownloadFileContentInfo>export async function downloadFile(http: HttpService,downloadDto: DownloadFileDto,dstPath: string,- options?: { space?: SpaceEnv; getContentInfo?: false | undefined }+ options?: { allowPrivateIP?: boolean; space?: SpaceEnv; getContentInfo?: false | undefined }): Promise<void>export async function downloadFile(http: HttpService,@@ -56,7 +56,7 @@ export async function downloadFile(): Promise<void | DownloadFileContentInfo> {// dto must be validated by the callerconst headRes: AxiosResponse = await http.axiosRef({ method: HTTP_METHOD.HEAD, url: downloadDto.url, maxRedirects: 1 })- if (regExpPrivateIP.test(headRes.request.socket.remoteAddress)) {+ if (!options?.allowPrivateIP && regExpPrivateIP.test(headRes.request.socket.remoteAddress)) {// prevent SSRF attackthrow new FileError(HttpStatus.FORBIDDEN, errorRegexpPrivateIP)}@@ -87,7 +87,7 @@ export async function downloadFile(}const getRes = await http.axiosRef({ method: HTTP_METHOD.GET, url: downloadDto.url, responseType: 'stream', maxRedirects: 1 })- if (regExpPrivateIP.test(getRes.request.socket.remoteAddress)) {+ if (!options?.allowPrivateIP && regExpPrivateIP.test(getRes.request.socket.remoteAddress)) {// close requestgetRes.data?.destroy()// Prevent SSRF attacks and perform a DNS-rebinding check if a HEAD request has already been made