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
fix(backend/files): prevent OnlyOffice callback path traversal using generated temp files
johaven10d1dc0fe5a5
diff --git a/backend/src/applications/files/editors/only-office/only-office-manager.service.ts b/backend/src/applications/files/editors/only-office/only-office-manager.service.tsindex 1e11b8c1..5928cee0 100644--- a/backend/src/applications/files/editors/only-office/only-office-manager.service.ts+++ b/backend/src/applications/files/editors/only-office/only-office-manager.service.ts@@ -34,7 +34,6 @@ import {isPathExists,isPathIsDir,removeFiles,- uniqueFilePathFromDir,writeFromStream} from '../../utils/files'import {@@ -329,65 +328,70 @@ export class OnlyOfficeManager {/* url format:https://onlyoffice-server.com/cache/files/data/-33120641_7158/output.pptx/output.pptx?md5=duFHKC-5d47s-RRcYn3hAw&expires=1739400549&shardkey=-33120641&filename=output.pptx- */+ */const callbackDownloadUrl = this.parseDocumentServerDownloadUrl(url)const urlParams = callbackDownloadUrl.searchParams- // it is not the md5 of the file but the md5 generated by the combination of the elements of the url- const md5: string = urlParams.get('md5')- const tmpFilePath = await uniqueFilePathFromDir(path.join(os.tmpdir(), `${md5}-${urlParams.get('filename')}`))+ const tmpFilePath = path.join(os.tmpdir(), `sync-in-onlyoffice-${crypto.randomUUID()}`)- // convert the remote file to the local file with the current extension if these extensions aren't equal- const localExtension = path.extname(space.realPath).slice(1)- const remoteExtension = path.extname(urlParams.get('filename')).slice(1)+ try {+ // convert the remote file to the local file with the current extension if these extensions aren't equal+ const localExtension = path.extname(space.realPath).slice(1)+ const remoteExtension = path.extname(urlParams.get('filename') ?? '').slice(1)- let finalDownloadUrl = url- let finalParsedDownloadUrl = callbackDownloadUrl- if (localExtension !== remoteExtension && !ONLY_OFFICE_CONVERT_EXTENSIONS.ALLOW_AUTO.has(localExtension)) {- if (ONLY_OFFICE_CONVERT_EXTENSIONS.FROM.has(remoteExtension) && ONLY_OFFICE_CONVERT_EXTENSIONS.TO.has(localExtension)) {- finalDownloadUrl = await this.convertDocument(urlParams.get('shardkey'), url, remoteExtension, localExtension, space.url)- finalParsedDownloadUrl = this.parseDocumentServerDownloadUrl(finalDownloadUrl)- } else {- throw new Error(`document cannot be converted from ${remoteExtension} -> ${localExtension} : ${space.url}`)+ let finalDownloadUrl = url+ let finalParsedDownloadUrl = callbackDownloadUrl+ if (localExtension !== remoteExtension && !ONLY_OFFICE_CONVERT_EXTENSIONS.ALLOW_AUTO.has(localExtension)) {+ if (ONLY_OFFICE_CONVERT_EXTENSIONS.FROM.has(remoteExtension) && ONLY_OFFICE_CONVERT_EXTENSIONS.TO.has(localExtension)) {+ finalDownloadUrl = await this.convertDocument(urlParams.get('shardkey'), url, remoteExtension, localExtension, space.url)+ finalParsedDownloadUrl = this.parseDocumentServerDownloadUrl(finalDownloadUrl)+ } else {+ throw new Error(`document cannot be converted from ${remoteExtension} -> ${localExtension} : ${space.url}`)+ }}- }- // download file- let res: AxiosResponse- try {- res = await this.http.axiosRef({- method: HTTP_METHOD.GET,- url: finalDownloadUrl,- responseType: 'stream',- maxRedirects: 0,- httpsAgent: new https.Agent({ rejectUnauthorized: this.rejectUnauthorized })- })- await writeFromStream(tmpFilePath, res.data)- } catch (e) {- this.logger.error({- tag: this.saveDocument.name,- msg: `failed to download document from ${finalParsedDownloadUrl.origin}${finalParsedDownloadUrl.pathname} : ${space.url} - ${e.message}`- })- throw new Error(`unable to download document : ${e.message}`)- }+ // download file+ let res: AxiosResponse+ try {+ res = await this.http.axiosRef({+ method: HTTP_METHOD.GET,+ url: finalDownloadUrl,+ responseType: 'stream',+ maxRedirects: 0,+ httpsAgent: new https.Agent({ rejectUnauthorized: this.rejectUnauthorized })+ })+ await writeFromStream(tmpFilePath, res.data)+ } catch (e) {+ this.logger.error({+ tag: this.saveDocument.name,+ msg: `failed to download document from ${finalParsedDownloadUrl.origin}${finalParsedDownloadUrl.pathname} : ${space.url} - ${e.message}`+ })+ throw new Error(`unable to download document : ${e.message}`)+ }- // try to verify the downloaded size- const contentLength = Number(res.headers['content-length'])- if (!isNaN(contentLength) && contentLength !== 0) {- const tmpFileSize = await fileSize(tmpFilePath)- if (tmpFileSize !== contentLength) {- throw new Error(`document size differs (${tmpFileSize} != ${contentLength})`)+ // try to verify the downloaded size+ const contentLength = Number(res.headers['content-length'])+ if (!isNaN(contentLength) && contentLength !== 0) {+ const tmpFileSize = await fileSize(tmpFilePath)+ if (tmpFileSize !== contentLength) {+ throw new Error(`document size differs (${tmpFileSize} != ${contentLength})`)+ }+ } else if (contentLength === 0) {+ this.logger.warn({ tag: this.saveDocument.name, msg: `content length is 0 : ${space.url}` })+ }+ // copy contents to avoid inode changes (`file.id` in some cases)+ try {+ await copyFileContent(tmpFilePath, space.realPath)+ // emit file event+ FileEvent.emit('event', { user: user, space: space, action: ACTION.UPDATE, rPath: space.realPath, source: 'editor' })+ } catch (e) {+ throw new Error(`unable to save document : ${e.message}`)+ }+ } finally {+ try {+ await removeFiles(tmpFilePath)+ } catch (e) {+ this.logger.error({ tag: this.saveDocument.name, msg: `failed to remove temporary document ${tmpFilePath} : ${e.message}` })}- } else if (contentLength === 0) {- this.logger.warn({ tag: this.saveDocument.name, msg: `content length is 0 : ${space.url}` })- }- // copy contents to avoid inode changes (`file.id` in some cases)- try {- await copyFileContent(tmpFilePath, space.realPath)- // emit file event- FileEvent.emit('event', { user: user, space: space, action: ACTION.UPDATE, rPath: space.realPath, source: 'editor' })- await removeFiles(tmpFilePath)- } catch (e) {- throw new Error(`unable to save document : ${e.message}`)}}