Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:files): harden remote downloads against SSRF, redirects, proxy bypasses and oversized streams

johaven22e773e5b826
diff --git a/backend/package.json b/backend/package.json
index 27e97379..241f914a 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -63,6 +63,7 @@
"fast-xml-parser": "^5.0.7",
"fs-extra": "^11.2.0",
"html-to-text": "^10.0.0",
+ "ipaddr.js": "^2.4.0",
"js-yaml": "^4.1.0",
"ldapts": "^8.0.36",
"mime-types": "^3.0.1",
diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts
index 067f4321..0dc2c4f2 100755
--- a/backend/src/app.module.ts
+++ b/backend/src/app.module.ts
@@ -34,8 +34,9 @@ import { SchedulerModule } from './infrastructure/scheduler/scheduler.module'
headers: {
'User-Agent': USER_AGENT
},
- timeout: 5000,
- maxRedirects: 5
+ proxy: false,
+ timeout: 6000,
+ maxRedirects: 0
})
],
providers: [AppService]
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.ts
index cc298859..d02742fe 100644
--- a/backend/src/applications/files/services/files-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-manager.service.spec.ts
@@ -2,6 +2,7 @@ import { HttpService } from '@nestjs/axios'
import { HttpStatus } from '@nestjs/common'
import { Test, TestingModule } from '@nestjs/testing'
import archiver from 'archiver'
+import { lookup } from 'node:dns/promises'
import fs from 'node:fs'
import path from 'node:path'
import { PassThrough, Readable } from 'node:stream'
@@ -20,6 +21,7 @@ import { DownloadFileDto } from '../dto/file-operations.dto'
import { FileEvent, FileTaskEvent } from '../events/file-events'
import { FileError } from '../models/file-error'
import { LockConflict } from '../models/file-lock-error'
+import { FILE_ERROR_MESSAGES } from '../utils/errors'
import { SendFile } from '../utils/send-file'
import * as unzipUtils from '../utils/unzip-file'
import * as filesUtils from '../utils/files'
@@ -36,10 +38,14 @@ jest.mock('tar', () => ({
__esModule: true,
extract: jest.fn()
}))
+jest.mock('node:dns/promises', () => ({
+ lookup: jest.fn()
+}))
describe(FilesManager.name, () => {
let service: FilesManager
let http: { axiosRef: jest.Mock }
+ const lookupMock = lookup as jest.Mock
let filesQueries: { moveFiles: jest.Mock; deleteFiles: jest.Mock }
let spacesManager: { spaceEnv: jest.Mock }
let contextManager: { headerOriginUrl: jest.Mock }
@@ -120,6 +126,7 @@ describe(FilesManager.name, () => {
beforeEach(async () => {
http = { axiosRef: jest.fn() }
+ lookupMock.mockResolvedValue([{ address: '8.8.8.8', family: 4 }])
filesQueries = {
moveFiles: jest.fn().mockResolvedValue(undefined),
deleteFiles: jest.fn().mockResolvedValue(undefined)
@@ -475,7 +482,7 @@ describe(FilesManager.name, () => {
}
await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(
- new FileError(HttpStatus.PAYLOAD_TOO_LARGE, 'File size limit exceeded')
+ new FileError(HttpStatus.PAYLOAD_TOO_LARGE, FILE_ERROR_MESSAGES.MAX_FILE_SIZE_EXCEEDED)
)
const tmpWritePath = (filesUtils.writeFromStream as jest.Mock).mock.calls[0][0] as string
@@ -504,7 +511,7 @@ describe(FilesManager.name, () => {
}
await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(
- new FileError(HttpStatus.PAYLOAD_TOO_LARGE, 'File size limit exceeded')
+ new FileError(HttpStatus.PAYLOAD_TOO_LARGE, FILE_ERROR_MESSAGES.MAX_FILE_SIZE_EXCEEDED)
)
expect(req.files).toHaveBeenCalled()
@@ -528,7 +535,7 @@ describe(FilesManager.name, () => {
}
await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(
- new FileError(HttpStatus.PAYLOAD_TOO_LARGE, 'File size limit exceeded')
+ new FileError(HttpStatus.PAYLOAD_TOO_LARGE, FILE_ERROR_MESSAGES.MAX_FILE_SIZE_EXCEEDED)
)
expect(filesUtils.writeFromStream).not.toHaveBeenCalled()
@@ -892,10 +899,33 @@ describe(FilesManager.name, () => {
expect(space.task.props.totalSize).toBe(55)
expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, FILE_OPERATION.DOWNLOAD, '/tmp/download.txt')
- expect(filesUtils.writeFromStream).toHaveBeenCalledWith('/tmp/download.txt', expect.anything())
+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith('/tmp/download.txt', expect.anything(), 0, 55)
expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
expect(fileEmitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/download.txt' })
})
+
+ it('should cleanup partial file and skip ADD event when download write fails', async () => {
+ const error = new FileError(HttpStatus.PAYLOAD_TOO_LARGE, FILE_ERROR_MESSAGES.MAX_FILE_SIZE_EXCEEDED)
+ const space = makeSpace()
+ ;(filesUtils.uniqueFilePathFromDir as jest.Mock).mockResolvedValueOnce('/tmp/download.txt')
+ ;(filesUtils.writeFromStream as jest.Mock).mockRejectedValueOnce(error)
+ http.axiosRef
+ .mockResolvedValueOnce({
+ headers: { 'content-length': '55' },
+ request: { socket: { remoteAddress: '8.8.8.8' } }
+ })
+ .mockResolvedValueOnce({
+ data: Readable.from(['abc']),
+ request: { socket: { remoteAddress: '8.8.8.8' } }
+ })
+ const fileEmitSpy = jest.spyOn(FileEvent, 'emit')
+
+ await expect(service.downloadFromUrl(user, space, { url: 'https://example.org/file.txt' })).rejects.toBe(error)
+
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith('/tmp/download.txt')
+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
+ expect(fileEmitSpy).not.toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/download.txt' })
+ })
})
describe('compress', () => {
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index c6054cbf..b1ee9b5e 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -55,13 +55,14 @@ import {
} from '../utils/files'
import { SendFile } from '../utils/send-file'
import { extractZip } from '../utils/unzip-file'
-import { downloadFile } from '../utils/download-file'
+import { DownloadFile } from '../utils/download-file'
import { FilesLockManager } from './files-lock-manager.service'
import { FilesQueries } from './files-queries.service'
import { FileEvent, FileTaskEvent } from '../events/file-events'
import { ACTION } from '../../../common/constants'
import { pipeline } from 'node:stream/promises'
-import { isMultipartFileTooLargeError, maxUploadSizeExceededError, uploadTmpFilePath } from '../utils/upload-file'
+import { isMultipartFileTooLargeError, uploadTmpFilePath } from '../utils/upload-file'
+import { FILE_ERROR_MESSAGES, maxFileSizeExceededError } from '../utils/errors'
@Injectable()
export class FilesManager {
@@ -272,7 +273,7 @@ export class FilesManager {
await writeFromStream(writePath, part.file)
// With throwFileSizeLimit disabled, multipart marks the file stream as truncated instead of rejecting.
if (part.file.truncated) {
- throw maxUploadSizeExceededError()
+ throw maxFileSizeExceededError()
}
if (tmpFile) {
// If the following move fails after these deletes, the previous resources remain recoverable from the trash.
@@ -296,7 +297,7 @@ export class FilesManager {
await removeFiles(dstFile)
}
if (isMultipartFileTooLargeError(e)) {
- throw maxUploadSizeExceededError()
+ throw maxFileSizeExceededError()
}
throw e
} finally {
@@ -317,7 +318,7 @@ export class FilesManager {
}
} catch (e) {
if (isMultipartFileTooLargeError(e)) {
- throw maxUploadSizeExceededError()
+ throw maxFileSizeExceededError()
}
throw e
}
@@ -448,8 +449,8 @@ export class FilesManager {
if (!isMove || (isMove && srcSpace.id !== dstSpace.id)) {
const size = isDir ? (await dirSize(srcSpace.realPath))[0] : await fileSize(srcSpace.realPath)
if (dstSpace.willExceedQuota(size)) {
- this.logger.warn({ tag: this.copyMove.name, msg: `storage quota will be exceeded for *${dstSpace.alias}* (${dstSpace.id})` })
- throw new FileError(HttpStatus.INSUFFICIENT_STORAGE, 'Storage quota will be exceeded')
+ this.logger.warn({ tag: this.copyMove.name, msg: `${FILE_ERROR_MESSAGES.STORAGE_QUOTA_EXCEEDED} for *${dstSpace.alias}* (${dstSpace.id})` })
+ throw new FileError(HttpStatus.INSUFFICIENT_STORAGE, FILE_ERROR_MESSAGES.STORAGE_QUOTA_EXCEEDED)
}
}
}
@@ -579,15 +580,16 @@ export class FilesManager {
throw new LockConflict(fileLock, 'Conflicting lock')
}
- // do
try {
- await downloadFile(this.http, downloadDto, rPath, { space: space })
+ await new DownloadFile(this.http).download(downloadDto, rPath, { space: space })
+ } catch (e) {
+ await removeFiles(rPath).catch((err: Error) => this.logger.error({ tag: this.downloadFromUrl.name, msg: `unable to remove ${rPath} : ${err}` }))
+ throw e
} finally {
// release lock
await this.filesLockManager.removeLock(fileLock.key)
- // emit file event
- FileEvent.emit('event', { user, space, action: ACTION.ADD, rPath: rPath })
}
+ FileEvent.emit('event', { user, space, action: ACTION.ADD, rPath: rPath })
}
async compress(user: UserModel, space: SpaceEnv, dto: CompressFileDto): Promise<void> {
diff --git a/backend/src/applications/files/utils/download-file.spec.ts b/backend/src/applications/files/utils/download-file.spec.ts
index f499e6dd..f8c82c63 100644
--- a/backend/src/applications/files/utils/download-file.spec.ts
+++ b/backend/src/applications/files/utils/download-file.spec.ts
@@ -1,25 +1,50 @@
+import { lookup } from 'node:dns/promises'
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 { FILE_ERROR_MESSAGES } from './errors'
import { writeFromStream } from './files'
-import { downloadFile } from './download-file'
+import { DownloadFile } from './download-file'
jest.mock('./files', () => ({
writeFromStream: jest.fn()
}))
+jest.mock('node:dns/promises', () => ({
+ lookup: jest.fn()
+}))
-describe(downloadFile.name, () => {
+describe(DownloadFile.name, () => {
let http: { axiosRef: jest.Mock }
+ const lookupMock = lookup as jest.Mock
+
+ const blockedRemoteAddresses = [
+ '10.0.0.1',
+ '100.64.0.0',
+ '127.0.0.1',
+ '169.254.169.254',
+ '192.168.1.1',
+ '192.0.2.1',
+ '::1',
+ 'fc00::1',
+ 'fe80::1',
+ '2001:db8::1',
+ '::ffff:127.0.0.1',
+ '999.1.1.1'
+ ]
+
+ const publicRemoteAddresses = ['8.8.8.8', '100.128.0.0', '172.32.0.0', '2001:4860:4860::8888', '::ffff:8.8.8.8']
- const response = (remoteAddress: string, headers: Record<string, string> = {}) => ({
+ const response = (remoteAddress: string | undefined, headers: Record<string, string> = {}, status = 200) => ({
+ status,
headers,
request: { socket: { remoteAddress } }
})
beforeEach(() => {
http = { axiosRef: jest.fn() }
+ lookupMock.mockResolvedValue([{ address: '8.8.8.8', family: 4 }])
;(writeFromStream as jest.Mock).mockResolvedValue(undefined)
})
@@ -27,15 +52,138 @@ describe(downloadFile.name, () => {
jest.clearAllMocks()
})
- it('rejects private IPs on HEAD by default', async () => {
- http.axiosRef.mockResolvedValueOnce(response('127.0.0.1', { 'content-length': '12' }))
+ it.each(blockedRemoteAddresses)('rejects blocked remote address "%s" on HEAD by default', async (remoteAddress) => {
+ http.axiosRef.mockResolvedValueOnce(response(remoteAddress, { 'content-length': '12' }))
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
+ expect(http.axiosRef).toHaveBeenCalledTimes(1)
+ expect(http.axiosRef).toHaveBeenCalledWith(
+ expect.objectContaining({ method: HTTP_METHOD.HEAD, url: 'https://example.test/file.txt', maxRedirects: 0 })
+ )
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('rejects blocked DNS resolutions before calling HEAD', async () => {
+ lookupMock.mockResolvedValueOnce([{ address: '127.0.0.1', family: 4 }])
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
+ expect(lookupMock).toHaveBeenCalledWith('example.test', { all: true, order: 'verbatim' })
+ expect(http.axiosRef).not.toHaveBeenCalled()
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('rejects mixed DNS resolutions when one address is blocked', async () => {
+ lookupMock.mockResolvedValueOnce([
+ { address: '8.8.8.8', family: 4 },
+ { address: '::1', family: 6 }
+ ])
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
+ expect(http.axiosRef).not.toHaveBeenCalled()
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('follows a safe redirect manually', async () => {
+ lookupMock.mockResolvedValueOnce([{ address: '8.8.8.8', family: 4 }]).mockResolvedValueOnce([{ address: '1.1.1.1', family: 4 }])
+ http.axiosRef
+ .mockResolvedValueOnce(response('8.8.8.8', { location: 'https://cdn.example.test/file.txt' }, 302))
+ .mockResolvedValueOnce(response('1.1.1.1', { 'content-length': '12', 'content-type': 'text/plain' }))
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt', { getContentInfo: true })
+ ).resolves.toEqual({
+ contentLength: 12,
+ contentType: 'text/plain',
+ lastModified: undefined
+ })
- 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).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({ method: HTTP_METHOD.HEAD, url: 'https://example.test/file.txt', maxRedirects: 0 })
)
+ expect(http.axiosRef).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({ method: HTTP_METHOD.HEAD, url: 'https://cdn.example.test/file.txt', maxRedirects: 0 })
+ )
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('rejects a redirect to a blocked DNS resolution before following it', async () => {
+ lookupMock.mockResolvedValueOnce([{ address: '8.8.8.8', family: 4 }]).mockResolvedValueOnce([{ address: '127.0.0.1', family: 4 }])
+ http.axiosRef.mockResolvedValueOnce(response('8.8.8.8', { location: 'https://internal.example.test/file.txt' }, 302))
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
+ expect(http.axiosRef).toHaveBeenCalledTimes(1)
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('rejects a missing remote address on HEAD by default', async () => {
+ http.axiosRef.mockResolvedValueOnce(response(undefined, { 'content-length': '12' }))
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
+ expect(http.axiosRef).toHaveBeenCalledTimes(1)
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it.each(publicRemoteAddresses)('allows public remote address "%s" on HEAD by default', async (remoteAddress) => {
+ http.axiosRef.mockResolvedValueOnce(response(remoteAddress, { 'content-length': '12', 'content-type': 'text/plain' }))
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt', { getContentInfo: true })
+ ).resolves.toEqual({
+ contentLength: 12,
+ contentType: 'text/plain',
+ lastModified: undefined
+ })
expect(http.axiosRef).toHaveBeenCalledTimes(1)
- expect(http.axiosRef).toHaveBeenCalledWith({ method: HTTP_METHOD.HEAD, url: 'https://example.test/file.txt', maxRedirects: 1 })
+ expect(http.axiosRef).toHaveBeenCalledWith(
+ expect.objectContaining({
+ httpAgent: expect.any(Object),
+ httpsAgent: expect.any(Object)
+ })
+ )
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('uses a guarded agent lookup for connection-time resolutions', async () => {
+ lookupMock.mockResolvedValueOnce([{ address: '8.8.8.8', family: 4 }]).mockResolvedValueOnce([{ address: '127.0.0.1', family: 4 }])
+ http.axiosRef.mockImplementationOnce(
+ (config: {
+ httpAgent: {
+ options: { lookup: (hostname: string, options: Record<string, unknown>, callback: (e: Error | null) => void) => void }
+ }
+ }) =>
+ new Promise((resolve, reject) => {
+ config.httpAgent.options.lookup('redirect.test', {}, (e: Error | null) => {
+ if (e) {
+ reject(e)
+ } else {
+ resolve(response('8.8.8.8', { 'content-length': '12', 'content-type': 'text/plain' }))
+ }
+ })
+ })
+ )
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt', { getContentInfo: true })
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
expect(writeFromStream).not.toHaveBeenCalled()
})
@@ -48,7 +196,7 @@ describe(downloadFile.name, () => {
})
)
- const result = await downloadFile(http as unknown as HttpService, { url: 'https://example.test/avatar.png' }, '/tmp/avatar.png', {
+ const result = await new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/avatar.png' }, '/tmp/avatar.png', {
allowPrivateIP: true,
getContentInfo: true
})
@@ -69,10 +217,48 @@ describe(downloadFile.name, () => {
.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')
- )
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP))
+
+ expect(destroySpy).toHaveBeenCalled()
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it.each(['', '-1', '1.5', 'Infinity', '9007199254740992', 'abc'])('rejects invalid content-length "%s"', async (contentLength) => {
+ http.axiosRef.mockResolvedValueOnce(response('8.8.8.8', { 'content-length': contentLength }))
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.BAD_REQUEST, FILE_ERROR_MESSAGES.DOWNLOAD_INVALID_CONTENT_LENGTH))
+
+ expect(http.axiosRef).toHaveBeenCalledTimes(1)
+ expect(writeFromStream).not.toHaveBeenCalled()
+ })
+
+ it('allows zero content-length and guards the written stream at zero bytes', async () => {
+ const stream = Readable.from([])
+ http.axiosRef
+ .mockResolvedValueOnce(response('8.8.8.8', { 'content-length': '0' }))
+ .mockResolvedValueOnce({ ...response('8.8.8.8'), data: stream })
+
+ await new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+
+ expect(writeFromStream).toHaveBeenCalledWith('/tmp/file.txt', stream, 0, 0)
+ })
+
+ it('rejects redirects on GET after the HEAD URL has been resolved', 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('8.8.8.8', { location: 'https://cdn.example.test/file.txt' }, 302), data: stream })
+
+ await expect(
+ new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt')
+ ).rejects.toEqual(new FileError(HttpStatus.BAD_REQUEST, FILE_ERROR_MESSAGES.DOWNLOAD_MAX_REDIRECTS_EXCEEDED))
+
+ expect(http.axiosRef).toHaveBeenCalledTimes(2)
expect(destroySpy).toHaveBeenCalled()
expect(writeFromStream).not.toHaveBeenCalled()
})
@@ -83,8 +269,22 @@ describe(downloadFile.name, () => {
.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 })
+ await new DownloadFile(http as unknown as HttpService).download({ url: 'https://example.test/file.txt' }, '/tmp/file.txt', {
+ allowPrivateIP: true
+ })
- expect(writeFromStream).toHaveBeenCalledWith('/tmp/file.txt', stream)
+ expect(writeFromStream).toHaveBeenCalledWith('/tmp/file.txt', stream, 0, 12)
+ expect(http.axiosRef).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({
+ method: HTTP_METHOD.GET,
+ url: 'https://example.test/file.txt',
+ responseType: 'stream',
+ decompress: false,
+ headers: { 'Accept-Encoding': 'identity' },
+ proxy: false,
+ maxRedirects: 0
+ })
+ )
})
})
diff --git a/backend/src/applications/files/utils/download-file.ts b/backend/src/applications/files/utils/download-file.ts
index 5b00dbec..463cd77f 100644
--- a/backend/src/applications/files/utils/download-file.ts
+++ b/backend/src/applications/files/utils/download-file.ts
@@ -1,97 +1,177 @@
+import { lookup } from 'node:dns/promises'
+import { Agent as HttpAgent } from 'node:http'
+import { Agent as HttpsAgent } from 'node:https'
+import type { LookupAddress } from 'node:dns'
+import type { LookupFunction } from 'node:net'
import type { HttpService } from '@nestjs/axios'
-import type { SpaceEnv } from '../../spaces/models/space-env.model'
-import type { AxiosResponse } from 'axios'
-import { HTTP_METHOD } from '../../applications.constants'
-import { FileError } from '../models/file-error'
import { HttpStatus } from '@nestjs/common'
-import { FileTaskEvent } from '../events/file-events'
+import { AxiosHeaders, type AxiosRequestConfig, type AxiosResponse } from 'axios'
+import ipaddr from 'ipaddr.js'
+import { HTTP_METHOD } from '../../applications.constants'
+import type { SpaceEnv } from '../../spaces/models/space-env.model'
import { FILE_OPERATION } from '../constants/operations'
-import { writeFromStream } from './files'
import type { DownloadFileDto } from '../dto/file-operations.dto'
+import { FileTaskEvent } from '../events/file-events'
import type { DownloadFileContentInfo, DownloadFileOptions } from '../interfaces/download-file.interface'
+import { FileError } from '../models/file-error'
+import { FILE_ERROR_MESSAGES } from './errors'
+import { writeFromStream } from './files'
+
+interface DownloadFileRequestOptions {
+ allowPrivateIP?: boolean
+ maxRedirects?: number
+}
+
+export class DownloadFile {
+ private static readonly dnsOptions = { all: true, order: 'verbatim' } as const
+ private static readonly maxRedirects = 1
+ private static readonly redirectStatus = new Set([301, 302, 303, 307, 308])
+ private readonly dnsCache = new Map<string, LookupAddress[]>()
+ private readonly safeAgents = {
+ httpAgent: new HttpAgent({ lookup: this.safeLookup.bind(this) }),
+ httpsAgent: new HttpsAgent({ lookup: this.safeLookup.bind(this) })
+ }
+
+ constructor(private readonly http: HttpService) {}
+
+ async download(
+ downloadDto: DownloadFileDto,
+ dstPath: string,
+ options: { allowPrivateIP?: boolean; space?: SpaceEnv; getContentInfo: true }
+ ): Promise<DownloadFileContentInfo>
+ async download(
+ downloadDto: DownloadFileDto,
+ dstPath: string,
+ options?: { allowPrivateIP?: boolean; space?: SpaceEnv; getContentInfo?: false | undefined }
+ ): Promise<void>
+ async download(downloadDto: DownloadFileDto, dstPath: string, options?: DownloadFileOptions): Promise<void | DownloadFileContentInfo> {
+ const { response: headRes, url } = await this.request(downloadDto.url, { method: HTTP_METHOD.HEAD }, { allowPrivateIP: options?.allowPrivateIP })
+
+ const headers = AxiosHeaders.from(headRes.headers)
+ const contentLength = this.contentLength(headers)
+ if (options?.getContentInfo) {
+ return {
+ contentLength,
+ contentType: `${headers.getContentType()}`,
+ lastModified: headers.get('last-modified') as string | undefined
+ } satisfies DownloadFileContentInfo
+ }
-const parts = [
- // IPv4 loopback (127.0.0.0/8)
- '127\\.(?:\\d{1,3}\\.){2}\\d{1,3}',
- // IPv4 link-local (169.254.0.0/16)
- '169\\.254\\.\\d{1,3}\\.\\d{1,3}',
- // IPv4 Carrier-grade NAT (100.64.0.0/10)
- '100\\.(?:6[4-9]|[7-9]\\d|1[01]\\d|12[0-7])\\.\\d{1,3}\\.\\d{1,3}',
- // IPv4 private (10.0.0.0/8)
- '10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}',
- // IPv4 private (192.168.0.0/16)
- '192\\.168\\.\\d{1,3}\\.\\d{1,3}',
- // IPv4 private (172.16.0.0/12)
- '172\\.(?:1[6-9]|2\\d|3[0-1])\\.\\d{1,3}\\.\\d{1,3}',
- // IPv4 & IPv6 loopback
- '::1',
- '::',
- '0.0.0.0',
- // IPv6 Unique Local Address (fc00::/7)
- 'f[cd][0-9a-f]{2}:[0-9a-f:]+',
- // IPv6 link-local (fe80::/10)
- 'fe[89ab][0-9a-f]{2}:[0-9a-f:]+'
-]
-
-const regExpPrivateIP = new RegExp(`^(?:${parts.join('|')})$`, 'i')
-const errorRegexpPrivateIP = 'Access to internal IP addresses is forbidden'
-
-export async function downloadFile(
- http: HttpService,
- downloadDto: DownloadFileDto,
- dstPath: string,
- options: { allowPrivateIP?: boolean; space?: SpaceEnv; getContentInfo: true }
-): Promise<DownloadFileContentInfo>
-export async function downloadFile(
- http: HttpService,
- downloadDto: DownloadFileDto,
- dstPath: string,
- options?: { allowPrivateIP?: boolean; space?: SpaceEnv; getContentInfo?: false | undefined }
-): Promise<void>
-export async function downloadFile(
- http: HttpService,
- downloadDto: DownloadFileDto,
- dstPath: string,
- options?: DownloadFileOptions
-): Promise<void | DownloadFileContentInfo> {
- // dto must be validated by the caller
- const headRes: AxiosResponse = await http.axiosRef({ method: HTTP_METHOD.HEAD, url: downloadDto.url, maxRedirects: 1 })
- if (!options?.allowPrivateIP && regExpPrivateIP.test(headRes.request.socket.remoteAddress)) {
- // prevent SSRF attack
- throw new FileError(HttpStatus.FORBIDDEN, errorRegexpPrivateIP)
+ if (contentLength === null) throw new FileError(HttpStatus.BAD_REQUEST, FILE_ERROR_MESSAGES.DOWNLOAD_INVALID_CONTENT_LENGTH)
+ this.prepareSpace(options?.space, contentLength, dstPath)
+
+ // The HEAD request resolved redirects; the GET must target that final URL directly.
+ const { response: getRes } = await this.request(
+ url,
+ { method: HTTP_METHOD.GET, responseType: 'stream', decompress: false, headers: { 'Accept-Encoding': 'identity' } },
+ { allowPrivateIP: options?.allowPrivateIP, maxRedirects: 0 }
+ )
+ await writeFromStream(dstPath, getRes.data, 0, contentLength)
}
- // attempt to retrieve the Content-Length header
- const contentLength = 'content-length' in headRes.headers ? Number(headRes.headers['content-length']) || null : null
- if (options?.getContentInfo) {
- return {
- contentLength: contentLength,
- contentType: `${headRes.headers['content-type']}`,
- lastModified: headRes.headers['last-modified'] as string | undefined
- } satisfies DownloadFileContentInfo
+ private safeLookup(hostname: string, options: Parameters<LookupFunction>[1], cb: Parameters<LookupFunction>[2]): void {
+ this.resolvePublic(hostname)
+ .then((addresses) => {
+ const family = options.family === 4 || options.family === 'IPv4' ? 4 : options.family === 6 || options.family === 'IPv6' ? 6 : null
+ const matches = family ? addresses.filter((a) => a.family === family) : addresses
+ if (!matches.length) return cb(new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP), '', 0)
+ if (options.all) {
+ cb(null, matches)
+ } else {
+ cb(null, matches[0].address, matches[0].family)
+ }
+ })
+ .catch((e: Error) => cb(e as FileError, '', 0))
}
- if (!contentLength) {
- throw new FileError(HttpStatus.BAD_REQUEST, 'Missing "content-length" header')
+ private async request(
+ url: string,
+ config: AxiosRequestConfig,
+ options: DownloadFileRequestOptions = {}
+ ): Promise<{ response: AxiosResponse; url: string }> {
+ let currentUrl = url
+ for (let redirects = 0; ; redirects++) {
+ if (!options.allowPrivateIP) await this.resolvePublic(new URL(currentUrl).hostname)
+ const response: AxiosResponse = await this.http.axiosRef({
+ ...config,
+ url: currentUrl,
+ proxy: false,
+ maxRedirects: 0,
+ validateStatus: (status) => status >= 200 && status < 400,
+ ...(options.allowPrivateIP ? {} : this.safeAgents)
+ })
+ try {
+ this.checkRemote(response, options.allowPrivateIP)
+ } catch (e) {
+ response.data?.destroy?.()
+ throw e
+ }
+ const nextUrl = this.redirectUrl(response, currentUrl)
+ if (!nextUrl) return { response, url: currentUrl }
+
+ response.data?.destroy?.()
+ // Redirects are followed manually to re-run DNS and remote address checks on each hop.
+ if (redirects >= (options.maxRedirects ?? DownloadFile.maxRedirects)) {
+ throw new FileError(HttpStatus.BAD_REQUEST, FILE_ERROR_MESSAGES.DOWNLOAD_MAX_REDIRECTS_EXCEEDED)
+ }
+ currentUrl = nextUrl
+ }
}
- if (options?.space) {
- if (options.space.willExceedQuota(contentLength)) {
- throw new FileError(HttpStatus.INSUFFICIENT_STORAGE, 'Storage quota will be exceeded')
+ private redirectUrl(response: AxiosResponse, currentUrl: string): string | null {
+ if (!DownloadFile.redirectStatus.has(response.status)) return null
+ const location = response.headers.location as string | undefined
+ if (!location) throw new FileError(HttpStatus.BAD_REQUEST, FILE_ERROR_MESSAGES.DOWNLOAD_MISSING_REDIRECT_LOCATION)
+
+ const url = new URL(location, currentUrl)
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
+ throw new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_UNSAFE_REDIRECT_LOCATION)
}
- // tasking
- if (options.space.task?.cacheKey) {
- options.space.task.props.totalSize = contentLength
- FileTaskEvent.emit('startWatch', options.space, FILE_OPERATION.DOWNLOAD, dstPath)
+ return url.toString()
+ }
+
+ private async resolvePublic(hostname: string): Promise<LookupAddress[]> {
+ const key = this.normalizeHostname(hostname).toLowerCase()
+ // Keep DNS answers stable within one download attempt and avoid a second resolution drift.
+ const cached = this.dnsCache.get(key)
+ if (cached) return cached
+
+ const addresses = await lookup(key, DownloadFile.dnsOptions)
+ if (!addresses.length || addresses.some((a) => this.isBlocked(a.address))) {
+ throw new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP)
+ }
+ this.dnsCache.set(key, addresses)
+ return addresses
+ }
+
+ private normalizeHostname(hostname: string): string {
+ return hostname.startsWith('[') && hostname.endsWith(']') ? hostname.slice(1, -1) : hostname
+ }
+
+ private checkRemote(response: AxiosResponse, allowPrivateIP?: boolean): void {
+ if (!allowPrivateIP && this.isBlocked(response.request?.socket?.remoteAddress)) {
+ throw new FileError(HttpStatus.FORBIDDEN, FILE_ERROR_MESSAGES.DOWNLOAD_PRIVATE_IP)
}
}
- const getRes = await http.axiosRef({ method: HTTP_METHOD.GET, url: downloadDto.url, responseType: 'stream', maxRedirects: 1 })
- if (!options?.allowPrivateIP && regExpPrivateIP.test(getRes.request.socket.remoteAddress)) {
- // close request
- getRes.data?.destroy()
- // Prevent SSRF attacks and perform a DNS-rebinding check if a HEAD request has already been made
- throw new FileError(HttpStatus.FORBIDDEN, errorRegexpPrivateIP)
+ private isBlocked(address: string | undefined): boolean {
+ return !address || !ipaddr.isValid(address) || ipaddr.process(address).range() !== 'unicast'
+ }
+
+ private contentLength(headers: AxiosHeaders): number | null {
+ const value = headers.getContentLength(/^\d+$/)?.[0]
+ const contentLength = Number(value)
+ return Number.isSafeInteger(contentLength) ? contentLength : null
+ }
+
+ private prepareSpace(space: SpaceEnv | undefined, contentLength: number, dstPath: string): void {
+ if (!space) return
+ if (space.willExceedQuota(contentLength)) {
+ throw new FileError(HttpStatus.INSUFFICIENT_STORAGE, FILE_ERROR_MESSAGES.STORAGE_QUOTA_EXCEEDED)
+ }
+ if (space.task?.cacheKey) {
+ space.task.props.totalSize = contentLength
+ FileTaskEvent.emit('startWatch', space, FILE_OPERATION.DOWNLOAD, dstPath)
+ }
}
- await writeFromStream(dstPath, getRes.data)
}
diff --git a/backend/src/applications/files/utils/errors.ts b/backend/src/applications/files/utils/errors.ts
new file mode 100644
index 00000000..220106a7
--- /dev/null
+++ b/backend/src/applications/files/utils/errors.ts
@@ -0,0 +1,16 @@
+import { FileError } from '../models/file-error'
+import { HttpStatus } from '@nestjs/common'
+
+export const FILE_ERROR_MESSAGES = {
+ DOWNLOAD_PRIVATE_IP: 'Access to internal IP addresses is forbidden',
+ DOWNLOAD_INVALID_CONTENT_LENGTH: 'Missing or invalid "content-length" header',
+ DOWNLOAD_MAX_REDIRECTS_EXCEEDED: 'Maximum redirects exceeded',
+ DOWNLOAD_MISSING_REDIRECT_LOCATION: 'Missing redirect location',
+ DOWNLOAD_UNSAFE_REDIRECT_LOCATION: 'Unsafe redirect location',
+ MAX_FILE_SIZE_EXCEEDED: 'File size limit exceeded',
+ STORAGE_QUOTA_EXCEEDED: 'Storage quota will be exceeded'
+} as const
+
+export function maxFileSizeExceededError(): FileError {
+ return new FileError(HttpStatus.PAYLOAD_TOO_LARGE, FILE_ERROR_MESSAGES.MAX_FILE_SIZE_EXCEEDED)
+}
diff --git a/backend/src/applications/files/utils/files.spec.ts b/backend/src/applications/files/utils/files.spec.ts
new file mode 100644
index 00000000..cd5d2227
--- /dev/null
+++ b/backend/src/applications/files/utils/files.spec.ts
@@ -0,0 +1,47 @@
+import { HttpStatus } from '@nestjs/common'
+import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+import { Readable } from 'node:stream'
+import { FileError } from '../models/file-error'
+import { FILE_ERROR_MESSAGES } from './errors'
+import { writeFromStream } from './files'
+
+describe(writeFromStream.name, () => {
+ let tmpDir: string
+
+ beforeEach(async () => {
+ tmpDir = await mkdtemp(path.join(os.tmpdir(), 'write-from-stream-'))
+ })
+
+ afterEach(async () => {
+ await rm(tmpDir, { recursive: true, force: true })
+ })
+
+ it('writes a stream matching the max size', async () => {
+ const filePath = path.join(tmpDir, 'file.txt')
+
+ await writeFromStream(filePath, Readable.from([Buffer.from('abc')]), 0, 3)
+
+ await expect(readFile(filePath, 'utf8')).resolves.toBe('abc')
+ })
+
+ it('rejects a stream exceeding the max size', async () => {
+ const filePath = path.join(tmpDir, 'file.txt')
+
+ await expect(writeFromStream(filePath, Readable.from([Buffer.from('abcd')]), 0, 3)).rejects.toMatchObject({
+ httpCode: HttpStatus.PAYLOAD_TOO_LARGE,
+ message: FILE_ERROR_MESSAGES.MAX_FILE_SIZE_EXCEEDED,
+ name: FileError.name
+ })
+ })
+
+ it('accounts for the existing start offset', async () => {
+ const filePath = path.join(tmpDir, 'file.txt')
+ await writeFile(filePath, 'abc')
+
+ await writeFromStream(filePath, Readable.from([Buffer.from('de')]), 3, 5)
+
+ await expect(readFile(filePath, 'utf8')).resolves.toBe('abcde')
+ })
+})
diff --git a/backend/src/applications/files/utils/files.ts b/backend/src/applications/files/utils/files.ts
index 841603e7..f443b784 100644
--- a/backend/src/applications/files/utils/files.ts
+++ b/backend/src/applications/files/utils/files.ts
@@ -6,7 +6,7 @@ import crypto from 'node:crypto'
import { createReadStream, createWriteStream, Dirent, statSync } from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
-import { Readable } from 'node:stream'
+import { Readable, Transform } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { formatDateISOString } from '../../../common/functions'
import { currentTimeStamp, isValidFileName, regExpPreventPathTraversal } from '../../../common/shared'
@@ -14,6 +14,7 @@ import { DEFAULT_CHECKSUM_ALGORITHM, DEFAULT_HIGH_WATER_MARK, EXTRA_MIMES_TYPE }
import type { FileDBProps } from '../interfaces/file-db-props.interface'
import type { FileProps } from '../interfaces/file-props.interface'
import { FileError } from '../models/file-error'
+import { maxFileSizeExceededError } from './errors'
export function sanitizePath(fPath: string): string {
return path.normalize(fPath).replace(regExpPreventPathTraversal, '')
@@ -178,9 +179,23 @@ export async function checksumFile(filePath: string, alg: string): Promise<strin
return hash.digest('hex')
}
-export function writeFromStream(rPath: string, stream: Readable, start: number = 0): Promise<void> {
+export function writeFromStream(rPath: string, stream: Readable, start: number = 0, maxSize?: number): Promise<void> {
const dst: WriteStream = createWriteStream(rPath, { flags: start ? 'a' : 'w', start: start, highWaterMark: DEFAULT_HIGH_WATER_MARK })
- return pipeline(stream, dst)
+ if (maxSize === undefined) {
+ return pipeline(stream, dst)
+ }
+ let received = start
+ const limitSize = new Transform({
+ transform(chunk, _encoding, callback) {
+ received += chunk.length
+ if (received > maxSize) {
+ callback(maxFileSizeExceededError())
+ return
+ }
+ callback(null, chunk)
+ }
+ })
+ return pipeline(stream, limitSize, dst)
}
export async function writeFromStreamAndChecksum(rPath: string, stream: Readable, hasRange: number, alg: string): Promise<string> {
diff --git a/backend/src/applications/files/utils/upload-file.ts b/backend/src/applications/files/utils/upload-file.ts
index 46f80c07..199a0eb0 100644
--- a/backend/src/applications/files/utils/upload-file.ts
+++ b/backend/src/applications/files/utils/upload-file.ts
@@ -1,21 +1,14 @@
import path from 'node:path'
import { randomUUID } from 'node:crypto'
-import { FileError } from '../models/file-error'
-import { HttpStatus } from '@nestjs/common'
import { fileName } from './files'
const FASTIFY_MULTIPART_FILE_TOO_LARGE_CODE = 'FST_REQ_FILE_TOO_LARGE' as const
-const MAX_UPLOAD_SIZE_EXCEEDED = 'File size limit exceeded' as const
export function isMultipartFileTooLargeError(e: any): boolean {
// Other multipart limits also return 413; only this code means the file-size limit was reached.
return e?.code === FASTIFY_MULTIPART_FILE_TOO_LARGE_CODE
}
-export function maxUploadSizeExceededError(): FileError {
- return new FileError(HttpStatus.PAYLOAD_TOO_LARGE, MAX_UPLOAD_SIZE_EXCEEDED)
-}
-
export function uploadTmpFilePath(tmpPath: string, partFileName: string): string {
return path.join(tmpPath, `${randomUUID()}-upload-${fileName(partFileName) || 'file'}`)
}
diff --git a/backend/src/applications/spaces/guards/space.guard.ts b/backend/src/applications/spaces/guards/space.guard.ts
index 235003f8..4904d18e 100644
--- a/backend/src/applications/spaces/guards/space.guard.ts
+++ b/backend/src/applications/spaces/guards/space.guard.ts
@@ -17,6 +17,7 @@ import { SpaceEnv } from '../models/space-env.model'
import { SpacesManager } from '../services/spaces-manager.service'
import { canAccessToSpaceUrl, haveSpaceEnvPermissions } from '../utils/permissions'
import { PATH_TO_SPACE_SEGMENTS } from '../utils/routes'
+import { FILE_ERROR_MESSAGES } from '../../files/utils/errors'
@Injectable()
export class SpaceGuard implements CanActivate {
@@ -51,7 +52,7 @@ export class SpaceGuard implements CanActivate {
} else if (req.space.storageQuota) {
const contentLength = parseInt(req.headers['content-length'] || '0', 10) || 0
if (req.space.willExceedQuota(contentLength)) {
- throw new HttpException('Storage quota will be exceeded', HttpStatus.INSUFFICIENT_STORAGE)
+ throw new HttpException(FILE_ERROR_MESSAGES.STORAGE_QUOTA_EXCEEDED, HttpStatus.INSUFFICIENT_STORAGE)
}
}
}
diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts
index 9f9d0725..c33cfb77 100644
--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts
+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts
@@ -16,7 +16,7 @@ import { AdminUsersManager } from '../../../applications/users/services/admin-us
import { UsersManager } from '../../../applications/users/services/users-manager.service'
import * as avatarUtils from '../../../applications/users/utils/avatar'
import * as filesUtils from '../../../applications/files/utils/files'
-import * as downloadFileUtils from '../../../applications/files/utils/download-file'
+import { DownloadFile } from '../../../applications/files/utils/download-file'
import * as imageUtils from '../../../common/image'
import { DEFAULT_STORAGE_QUOTA_FIELD } from '../auth-providers.constants'
import { OAuthCookie } from './auth-oidc.constants'
@@ -381,7 +381,7 @@ describe(AuthProviderOIDC.name, () => {
const userInfo = (picture = 'https://cdn.example.test/avatar.jpg') => ({ picture }) as any
it('returns when picture url is invalid', async () => {
- const downloadSpy = jest.spyOn(downloadFileUtils, 'downloadFile')
+ const downloadSpy = jest.spyOn(DownloadFile.prototype, 'download')
await (service as any).updatePictureUrl(oidcUser, userInfo('not-a-url'))
@@ -389,7 +389,7 @@ describe(AuthProviderOIDC.name, () => {
})
it('stops when content type is not an image', async () => {
- const downloadSpy = jest.spyOn(downloadFileUtils, 'downloadFile').mockResolvedValueOnce({
+ const downloadSpy = jest.spyOn(DownloadFile.prototype, 'download').mockResolvedValueOnce({
contentType: 'text/plain',
contentLength: 123,
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT'
@@ -403,7 +403,7 @@ describe(AuthProviderOIDC.name, () => {
})
it('skips update when avatar metadata is unchanged', async () => {
- const downloadSpy = jest.spyOn(downloadFileUtils, 'downloadFile').mockResolvedValueOnce({
+ const downloadSpy = jest.spyOn(DownloadFile.prototype, 'download').mockResolvedValueOnce({
contentType: 'image/png',
contentLength: 128,
lastModified: 'Mon, 01 Jan 2024 00:00:00 GMT'
@@ -419,7 +419,7 @@ describe(AuthProviderOIDC.name, () => {
it('downloads and converts avatar when checks pass', async () => {
const downloadSpy = jest
- .spyOn(downloadFileUtils, 'downloadFile')
+ .spyOn(DownloadFile.prototype, 'download')
.mockResolvedValueOnce({
contentType: 'image/png',
contentLength: 128,
@@ -441,7 +441,7 @@ describe(AuthProviderOIDC.name, () => {
it('stops after download when avatar size exceeds limit', async () => {
const downloadSpy = jest
- .spyOn(downloadFileUtils, 'downloadFile')
+ .spyOn(DownloadFile.prototype, 'download')
.mockResolvedValueOnce({
contentType: 'image/png',
contentLength: 128,
diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts
index 9ff344c3..3db0afd3 100644
--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts
+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts
@@ -44,7 +44,7 @@ import type { AuthProviderOIDCConfig } from './auth-oidc.config'
import { OAuthCookie, OAuthCookieSettings, OAuthTokenEndpoint } from './auth-oidc.constants'
import { HttpService } from '@nestjs/axios'
import { DownloadFileDto } from '../../../applications/files/dto/file-operations.dto'
-import { downloadFile } from '../../../applications/files/utils/download-file'
+import { DownloadFile } from '../../../applications/files/utils/download-file'
import { convertTempImageToPng, imgMimeTypePrefix } from '../../../common/image'
import { fileSize } from '../../../applications/files/utils/files'
@@ -433,10 +433,11 @@ export class AuthProviderOIDC implements AuthProvider {
// checks
let pictureContentLength: number | undefined
let pictureLastModified: string | undefined
+ const downloader = new DownloadFile(this.http)
try {
const tmpPicturePath = path.join(user.tmpPath, USER_AVATAR_FILE_NAME)
// retrieve headers
- const { contentType, contentLength, lastModified } = await downloadFile(this.http, downloadDto, tmpPicturePath, {
+ const { contentType, contentLength, lastModified } = await downloader.download(downloadDto, tmpPicturePath, {
allowPrivateIP: true, // trust the url source
getContentInfo: true
})
@@ -464,7 +465,7 @@ export class AuthProviderOIDC implements AuthProvider {
// download avatar (trust the url source)
const userAvatarTmpPath = path.join(user.tmpPath, USER_AVATAR_FILE_NAME)
try {
- await downloadFile(this.http, downloadDto, userAvatarTmpPath, { allowPrivateIP: true })
+ await downloader.download(downloadDto, userAvatarTmpPath, { allowPrivateIP: true })
} catch (e) {
this.logger.warn({ tag: this.updatePictureUrl.name, msg: `download failed: ${e}` })
return
diff --git a/package-lock.json b/package-lock.json
index b2ede193..f77b4ea6 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -72,6 +72,7 @@
"fast-xml-parser": "^5.0.7",
"fs-extra": "^11.2.0",
"html-to-text": "^10.0.0",
+ "ipaddr.js": "^2.4.0",
"js-yaml": "^4.1.0",
"ldapts": "^8.0.36",
"mime-types": "^3.0.1",