fix(cors): add proper CORS configuration for content routes#3192
fix(cors): add proper CORS configuration for content routes#3192luke1879012 wants to merge 1 commit intocloudreve:masterfrom
Conversation
| AllowOrigins: []string{"*"}, | ||
| AllowMethods: []string{"GET", "HEAD", "OPTIONS"}, | ||
| AllowHeaders: []string{"Range", "If-Range", "Authorization", "Content-Type"}, | ||
| ExposeHeaders: []string{"Content-Range", "Accept-Ranges", "Content-Length", "Content-Disposition"}, |
There was a problem hiding this comment.
Let's add more exposed headers: Content-Disposition, ETag
There was a problem hiding this comment.
English:
When can it be repaired? Although I can currently access it through IP, the data is inconsistent, which makes my OCD very uncomfortable.
中文:
请问什么时候可以修复呢?虽然目前可以用ip访问,但是数据不一致,导致我的强迫症很难受。
There was a problem hiding this comment.
Should we use the setting item from conf.ini rather then hard coding?
There was a problem hiding this comment.
English: When can it be repaired? Although I can currently access it through IP, the data is inconsistent, which makes my OCD very uncomfortable. 中文: 请问什么时候可以修复呢?虽然目前可以用ip访问,但是数据不一致,导致我的强迫症很难受。
As a temporary fix, if you are using a reverse proxy/CDN for Cloudreve, you may uodate their configuration to add these CORS headers. Fix in 4.12.0.
There was a problem hiding this comment.
Should we use the setting item from
conf.inirather then hard coding?
you are right
English Version
Title: Fix incomplete CORS configuration for /file/content endpoint
Description
Problem:
When accessing video or file content via the /api/v4/file/content endpoint from a cross-origin context (e.g., embedded in Obsidian notes or other third-party applications), the browser's preflight (OPTIONS) request fails. This is because the CORS middleware for this endpoint only sets AllowOrigins: ["*"] but lacks other essential CORS headers such as AllowMethods, AllowHeaders, and ExposeHeaders.
Example Scenario:
When embedding Cloudreve-hosted videos in Obsidian notes:
Root Cause:
The contentCors configuration in
routers/router.go
(around line 601) was incomplete:
Solution:
Added complete CORS configuration including:
中文版本
标题:修复 /file/content 端点的 CORS 配置不完整问题
描述
问题现象:
当从跨域环境(如嵌入在 Obsidian 笔记或其他第三方应用中)访问 /api/v4/file/content 端点的视频或文件内容时,浏览器的预检请求(OPTIONS)会失败。这是因为该端点的 CORS 中间件只设置了 AllowOrigins: ["*"],但缺少其他必要的 CORS 头。
问题复现示例:
在 Obsidian 笔记中嵌入 Cloudreve 托管的视频时:
第一个示例失败是因为浏览器发送了预检 OPTIONS 请求,但没有收到正确的 CORS 响应头。第二个示例能正常工作是因为直接 IP 访问在某些情况下不受跨域限制。
根本原因:
routers/router.go
文件(约第 601 行)中的 contentCors 配置不完整:
解决方案:
添加完整的 CORS 配置,包括:
修复后,以上两种方式均可正常播放视频。