diff --git a/apps/oss/tests/test_file_retrieval_auth.py b/apps/oss/tests/test_file_retrieval_auth.py
new file mode 100644
index 00000000000..555aa79aebf
--- /dev/null
+++ b/apps/oss/tests/test_file_retrieval_auth.py
@@ -0,0 +1,51 @@
+from unittest.mock import patch
+
+from django.http import HttpResponse
+from django.test import SimpleTestCase
+from rest_framework.test import APIRequestFactory
+
+from oss.views import FileRetrievalView
+from oss.views.file import FILE_AUTH_COOKIE
+
+
+class FileRetrievalAuthTestCase(SimpleTestCase):
+ """文件读取接口的凭证来源:请求头优先,cookie 兜底。"""
+
+ def setUp(self):
+ self.factory = APIRequestFactory()
+
+ def retrieve(self, request):
+ with patch("oss.views.file.FileSerializer.Operate") as operate:
+ operate.return_value.get.return_value = HttpResponse()
+ FileRetrievalView.as_view()(request, file_id="file-id")
+ return operate.return_value.get.call_args.kwargs["mk_file_auth"]
+
+ def test_bearer_header_is_used_when_cookie_is_missing(self):
+ request = self.factory.get("/chat/oss/file/file-id", HTTP_AUTHORIZATION="Bearer header-token")
+
+ self.assertEqual(self.retrieve(request), "header-token")
+
+ def test_bearer_header_takes_precedence_over_cookie(self):
+ self.factory.cookies[FILE_AUTH_COOKIE] = "cookie-token"
+ request = self.factory.get("/chat/oss/file/file-id", HTTP_AUTHORIZATION="bearer header-token")
+
+ self.assertEqual(self.retrieve(request), "header-token")
+
+ def test_cookie_is_used_without_bearer_header(self):
+ self.factory.cookies[FILE_AUTH_COOKIE] = "cookie-token"
+ request = self.factory.get("/chat/oss/file/file-id")
+
+ self.assertEqual(self.retrieve(request), "cookie-token")
+
+ def test_other_authorization_schemes_fall_back_to_cookie(self):
+ self.factory.cookies[FILE_AUTH_COOKIE] = "cookie-token"
+ for authorization in ("Basic dXNlcjpwYXNz", "Bearer ", "Bearer"):
+ with self.subTest(authorization=authorization):
+ request = self.factory.get("/chat/oss/file/file-id", HTTP_AUTHORIZATION=authorization)
+
+ self.assertEqual(self.retrieve(request), "cookie-token")
+
+ def test_missing_credentials_pass_none(self):
+ request = self.factory.get("/chat/oss/file/file-id")
+
+ self.assertIsNone(self.retrieve(request))
diff --git a/apps/oss/views/file.py b/apps/oss/views/file.py
index fa20f7a11df..afe78c141a6 100644
--- a/apps/oss/views/file.py
+++ b/apps/oss/views/file.py
@@ -21,6 +21,20 @@
from oss.serializers.file import FileSerializer, get_url_content
+FILE_AUTH_COOKIE = "mk_file_auth"
+
+
+def get_file_auth(request: Request):
+ """
+ 读取文件凭证:优先使用 Authorization 请求头中的 Bearer token,没有时使用 cookie。
+ 对话页以 iframe 嵌入第三方页面时,浏览器可能不发送跨站 cookie,前端改用请求头携带同一个 token。
+ """
+ scheme, _separator, token = request.headers.get("Authorization", "").partition(" ")
+ if scheme.lower() == "bearer" and token.strip():
+ return token.strip()
+ return request.COOKIES.get(FILE_AUTH_COOKIE)
+
+
class FileRetrievalView(APIView):
@extend_schema(
methods=["GET"],
@@ -41,7 +55,7 @@ def get(self, request: Request, file_id: str):
"http_range": request.headers.get("Range", ""),
}
).get(
- mk_file_auth=request.COOKIES.get("mk_file_auth"),
+ mk_file_auth=get_file_auth(request),
thumbnail=request.query_params.get("variant") == "thumbnail",
)
diff --git a/ui/src/conversation-panel/components/content/items/file.vue b/ui/src/conversation-panel/components/content/items/file.vue
index aefbd55d02a..e736cdcaf16 100644
--- a/ui/src/conversation-panel/components/content/items/file.vue
+++ b/ui/src/conversation-panel/components/content/items/file.vue
@@ -6,8 +6,9 @@ import MkAudio from '@/components/global/mk-media-preview/MkAudio.vue'
import MkVideo from '@/components/global/mk-media-preview/MkVideo.vue'
import FileUploadCard from '@/components/mk-drag-upload/FileUploadCard.vue'
import { getFilePreviewType } from '../../../right-sidebar/file-preview/index'
+import { downloadChatFile } from '../../../right-sidebar/file-preview/loader'
import { useRightSidebarStoreOptional } from '../../../right-sidebar/index'
-import { downloadByURL } from '@/utils/common'
+import { useFileRequestHeaders } from '../../../view/index'
import { getFileExtension, isAudio, isImage, isVideo } from '@/utils/icon'
import { formatFileSize } from '@/utils/number'
@@ -20,6 +21,7 @@ const props = defineProps<{
const { t } = useI18n({ useScope: 'global' })
// 对话视图内可在右侧预览,工作流执行详情等无右侧栏时直接下载
const rightSidebar = useRightSidebarStoreOptional()
+const getFileRequestHeaders = useFileRequestHeaders()
type FileKind = 'image' | 'audio' | 'video' | 'file'
@@ -48,7 +50,7 @@ const imageSources = computed(() => imageFiles.value.map((file) => file.src))
function openFile(file: ChatFile) {
if (!file.url) return
if (rightSidebar && getFilePreviewType(file.name)) rightSidebar.showFilePreview({ name: file.name, url: file.url })
- else downloadByURL(file.url, file.name)
+ else void downloadChatFile(file.url, file.name, getFileRequestHeaders())
}
function getFileDescription(file: ChatFile) {
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/index.vue
index 30de8418df3..2f47a1fd50b 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/index.vue
@@ -3,7 +3,7 @@ import { computed, onBeforeUnmount, onMounted, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import type { FilePreviewType } from './index'
import type { Component } from 'vue'
-import { useViewType } from '@/conversation-panel/view/index'
+import { useFileRequestHeaders, useViewType } from '@/conversation-panel/view/index'
import { useRightSidebarStore } from '../index'
import { createFullscreenToggle } from '../knowledge-source/document-preview/index'
import CsvItem from './items/csv/index.vue'
@@ -14,12 +14,13 @@ import PdfItem from './items/pdf/index.vue'
import PptItem from './items/ppt/index.vue'
import TextItem from './items/text/index.vue'
import WordItem from './items/word/index.vue'
-import { downloadByURL } from '@/utils/common'
+import { downloadChatFile } from './loader'
defineOptions({ name: 'FilePreview' })
const { t } = useI18n({ useScope: 'global' })
const viewType = useViewType()
+const getFileRequestHeaders = useFileRequestHeaders()
const { filePreview, closeRightSide } = useRightSidebarStore()
const { previewFile, previewType } = filePreview
const fullscreen = createFullscreenToggle()
@@ -42,7 +43,7 @@ const previewRenderer = computed(() => (previewType.value ? previewRenderers[pre
// 下载当前预览的原文件
const downloadFile = () => {
const file = previewFile.value
- if (file?.url) downloadByURL(file.url, file.name)
+ if (file?.url) void downloadChatFile(file.url, file.name, getFileRequestHeaders())
}
// 全屏只作用于预览区域
@@ -69,8 +70,8 @@ onBeforeUnmount(() => fullscreen.unbind())
-
-
+
+
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/csv/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/csv/index.vue
index 6de285821d8..a612cb737cb 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/csv/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/csv/index.vue
@@ -1,7 +1,7 @@
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/excel/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/excel/index.vue
index 56d215f5e70..6ac9e9dccc0 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/excel/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/excel/index.vue
@@ -2,7 +2,7 @@
import { ref, watch } from 'vue'
import type { PreviewFile } from '../../index'
import SheetTable from '../../components/sheet-table/index.vue'
-import { fetchPreviewFile, usePreviewLoader } from '../../loader'
+import { usePreviewLoader } from '../../loader'
import FilePreviewStatus from '../../status.vue'
defineOptions({ name: 'FilePreviewExcel' })
@@ -13,9 +13,9 @@ const {
data: sheets,
loading,
error,
-} = usePreviewLoader(async (signal) => {
+} = usePreviewLoader(async (fetchFile) => {
// xlsx 解析模块(exceljs、图表)首次预览时才加载
- const [response, { loadXlsxSheets }] = await Promise.all([fetchPreviewFile(props.file.url, signal), import('./xlsx')])
+ const [response, { loadXlsxSheets }] = await Promise.all([fetchFile(props.file.url), import('./xlsx')])
return loadXlsxSheets(await response.arrayBuffer())
})
const activeSheet = ref('')
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/html/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/html/index.vue
index baf5e757cef..51a9b49e5bb 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/html/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/html/index.vue
@@ -1,13 +1,13 @@
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/markdown/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/markdown/index.vue
index 6ea9afbe796..61175e8ee84 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/markdown/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/markdown/index.vue
@@ -1,13 +1,13 @@
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/pdf/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/pdf/index.vue
index ab66fc45cd5..97b8ec86b22 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/pdf/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/pdf/index.vue
@@ -1,6 +1,7 @@
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/index.vue
index b8c5b32e1e5..112a158d9e1 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/index.vue
@@ -1,6 +1,7 @@
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/ppt.ts b/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/ppt.ts
index 7cc60bae655..a5161d53335 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/ppt.ts
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/ppt/ppt.ts
@@ -32,8 +32,8 @@ export function createPptRenderer() {
viewer = null
}
- /** target 为放置幻灯片的容器,scrollTarget 为其滚动容器,用于按可见区域挂载幻灯片。 */
- async function render(target: HTMLElement, scrollTarget: HTMLElement, url: string) {
+ /** target 为放置幻灯片的容器,scrollTarget 为其滚动容器,用于按可见区域挂载幻灯片;headers 随文件请求发送。 */
+ async function render(target: HTMLElement, scrollTarget: HTMLElement, url: string, headers?: Record) {
const current = ++token
release()
target.replaceChildren()
@@ -43,7 +43,7 @@ export function createPptRenderer() {
const controller = new AbortController()
abortController = controller
try {
- const response = await fetch(url, { signal: controller.signal })
+ const response = await fetch(url, { headers, signal: controller.signal })
if (!response.ok) throw new Error(String(response.status))
const [data, { PptxViewer, RECOMMENDED_ZIP_LIMITS }] = await Promise.all([response.arrayBuffer(), loadPptxRenderer()])
if (current !== token) return
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/text/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/text/index.vue
index 34dddc0c4ba..d583f2b362f 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/text/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/text/index.vue
@@ -1,13 +1,13 @@
diff --git a/ui/src/conversation-panel/right-sidebar/file-preview/items/word/index.vue b/ui/src/conversation-panel/right-sidebar/file-preview/items/word/index.vue
index 65387b69e53..55dac514a22 100644
--- a/ui/src/conversation-panel/right-sidebar/file-preview/items/word/index.vue
+++ b/ui/src/conversation-panel/right-sidebar/file-preview/items/word/index.vue
@@ -1,7 +1,7 @@