Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

refactor(storage): 优化OSS存储初始化与URL格式,提升安全性和兼容性

bingxiang.chengd11e043142e8
diff --git a/docreader/parser/storage.py b/docreader/parser/storage.py
index b44d51d2f..6e97838b2 100644
--- a/docreader/parser/storage.py
+++ b/docreader/parser/storage.py
@@ -245,6 +245,9 @@ class OssStorage(Storage):
self.bucket, self.endpoint, self.prefix = self._init_oss_bucket()
def _init_oss_bucket(self):
+ if oss2 is None:
+ logger.error("oss2 package not installed. Install with: pip install oss2")
+ return None, None, None
try:
sc = self.storage_config
access_key = _cfg(sc, "access_key", "OSS_ACCESS_KEY")
@@ -268,12 +271,15 @@ class OssStorage(Storage):
auth = oss2.Auth(access_key, secret_key)
bucket = oss2.Bucket(auth, endpoint_raw, bucket_name)
- # Verify bucket exists, create if missing
+ # Verify bucket exists; do NOT auto-create to avoid unintended public-read buckets
try:
bucket.get_bucket_info()
except oss2.exceptions.NoSuchBucket:
- logger.info("OSS bucket %s not found, creating...", bucket_name)
- bucket.create_bucket(oss2.models.BUCKET_ACL_PUBLIC_READ)
+ logger.error(
+ "OSS bucket %s does not exist. Please create it manually in the console.",
+ bucket_name,
+ )
+ return None, None, None
prefix = prefix_raw.strip().strip("/") if prefix_raw else ""
return bucket, endpoint_raw, prefix
@@ -283,7 +289,9 @@ class OssStorage(Storage):
return None, None, None
def _get_download_url(self, object_key: str) -> str:
- return f"{self.endpoint}/{self.bucket.bucket_name}/{object_key}"
+ # Use virtual-hosted style URL: https://{bucket}.{endpoint_without_scheme}/{key}
+ endpoint_no_scheme = self.endpoint.removeprefix("https://").removeprefix("http://")
+ return f"https://{self.bucket.bucket_name}.{endpoint_no_scheme}/{object_key}"
def upload_file(self, file_path: str) -> str:
try:
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index 5562ac465..dbaeeae4b 100755
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -659,6 +659,7 @@ export default {
engineLocal: 'Local',
engineCos: 'Tencent Cloud COS',
engineTos: 'Volcengine TOS',
+ engineOss: 'Alibaba Cloud OSS',
engineS3: 'AWS S3',
localTitle: 'Local Storage',
localDesc: 'Store files on the server local filesystem, suitable for single-node deployment only.',
@@ -692,6 +693,10 @@ export default {
s3Desc: 'AWS S3 and S3-compatible object storage services, suitable for public cloud deployment.',
s3AccessKeyPlaceholder: 'AWS Access Key',
s3SecretKeyPlaceholder: 'AWS Secret Key',
+ ossTitle: 'Alibaba Cloud OSS',
+ ossDesc: 'Alibaba Cloud Object Storage Service (OSS), suitable for public cloud deployment.',
+ ossAccessKeyPlaceholder: 'Alibaba Cloud Access Key',
+ ossSecretKeyPlaceholder: 'Alibaba Cloud Secret Key',
console: 'Console',
docs: 'Docs',
testConnection: 'Test Connection',
@@ -3117,6 +3122,8 @@ export default {
engineTosDesc: 'Volcengine object storage, for public cloud deployment',
engineS3: 'AWS S3',
engineS3Desc: 'AWS S3 and compatible storage, for public cloud deployment',
+ engineOss: 'Alibaba Cloud OSS',
+ engineOssDesc: 'Alibaba Cloud Object Storage Service, for public cloud deployment',
},
parser: {
title: 'Parser Engine',
diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts
index 57200733b..493a6a05b 100755
--- a/frontend/src/i18n/locales/ko-KR.ts
+++ b/frontend/src/i18n/locales/ko-KR.ts
@@ -516,6 +516,7 @@ export default {
engineLocal: 'Local(로컬)',
engineCos: 'Tencent Cloud COS',
engineTos: 'Volcengine TOS',
+ engineOss: 'Alibaba Cloud OSS',
engineS3: 'AWS S3',
localTitle: 'Local(로컬 스토리지)',
localDesc: '서버 로컬 파일시스템에 파일을 저장합니다. 단일 노드 배포에만 적합합니다.',
@@ -549,6 +550,10 @@ export default {
s3Desc: 'AWS S3 및 호환 오브젝트 스토리지 서비스, 퍼블릭 클라우드 배포에 적합합니다.',
s3AccessKeyPlaceholder: 'AWS Access Key',
s3SecretKeyPlaceholder: 'AWS Secret Key',
+ ossTitle: 'Alibaba Cloud OSS',
+ ossDesc: 'Alibaba Cloud 오브젝트 스토리지 서비스(OSS), 퍼블릭 클라우드 배포에 적합합니다.',
+ ossAccessKeyPlaceholder: 'Alibaba Cloud Access Key',
+ ossSecretKeyPlaceholder: 'Alibaba Cloud Secret Key',
console: '콘솔',
docs: '문서',
testConnection: '연결 테스트',
@@ -3172,6 +3177,8 @@ export default {
engineTosDesc: 'Volcengine 오브젝트 스토리지, 퍼블릭 클라우드 배포에 적합',
engineS3: 'AWS S3',
engineS3Desc: 'AWS S3 및 호환 스토리지, 퍼블릭 클라우드 배포에 적합',
+ engineOss: 'Alibaba Cloud OSS',
+ engineOssDesc: 'Alibaba Cloud 오브젝트 스토리지, 퍼블릭 클라우드 배포에 적합',
},
parser: {
title: '파서 엔진',
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index ab81a6c3e..253c361c1 100755
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -640,6 +640,7 @@ export default {
engineLocal: 'Локальное',
engineCos: 'Tencent Cloud COS',
engineTos: 'Volcengine TOS',
+ engineOss: 'Alibaba Cloud OSS',
engineS3: 'AWS S3',
localTitle: 'Локальное хранилище',
localDesc: 'Хранение файлов в локальной файловой системе сервера. Подходит только для однонодового развёртывания.',
@@ -673,6 +674,10 @@ export default {
s3Desc: 'AWS S3 и совместимые сервисы объектного хранилища для публичного облака.',
s3AccessKeyPlaceholder: 'AWS Access Key',
s3SecretKeyPlaceholder: 'AWS Secret Key',
+ ossTitle: 'Alibaba Cloud OSS',
+ ossDesc: 'Объектное хранилище Alibaba Cloud (OSS), подходит для публичного облака.',
+ ossAccessKeyPlaceholder: 'Alibaba Cloud Access Key',
+ ossSecretKeyPlaceholder: 'Alibaba Cloud Secret Key',
console: 'Консоль',
docs: 'Документация',
testConnection: 'Тест подключения',
@@ -2787,6 +2792,8 @@ export default {
engineTosDesc: 'Объектное хранилище Volcengine, для публичного облака',
engineS3: 'AWS S3',
engineS3Desc: 'AWS S3 и совместимые хранилища, для публичного облака',
+ engineOss: 'Alibaba Cloud OSS',
+ engineOssDesc: 'Объектное хранилище Alibaba Cloud, для публичного облака',
},
parser: {
title: 'Парсер',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index 846b9a361..e31b058c4 100755
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -513,6 +513,7 @@ export default {
engineLocal: "Local(本地)",
engineCos: "腾讯云 COS",
engineTos: "火山引擎 TOS",
+ engineOss: "阿里云 OSS",
engineS3: "AWS S3",
localTitle: "Local(本地存储)",
localDesc: "使用服务器本地文件系统存储文件,仅适合单机部署。",
@@ -546,6 +547,10 @@ export default {
s3Desc: "AWS S3 及兼容的对象存储服务,适合公有云部署。",
s3AccessKeyPlaceholder: "AWS Access Key",
s3SecretKeyPlaceholder: "AWS Secret Key",
+ ossTitle: "阿里云 OSS",
+ ossDesc: "阿里云对象存储服务(OSS),适合公有云部署。",
+ ossAccessKeyPlaceholder: "阿里云 Access Key",
+ ossSecretKeyPlaceholder: "阿里云 Secret Key",
console: "控制台",
docs: "文档",
testConnection: "测试连接",
@@ -3115,6 +3120,8 @@ export default {
engineTosDesc: "火山引擎对象存储,适合公有云部署",
engineS3: "AWS S3",
engineS3Desc: "AWS S3 及兼容存储,适合公有云部署",
+ engineOss: "阿里云 OSS",
+ engineOssDesc: "阿里云对象存储,适合公有云部署",
},
parser: {
title: "解析引擎",
diff --git a/frontend/src/views/settings/StorageEngineSettings.vue b/frontend/src/views/settings/StorageEngineSettings.vue
index 97ece2df4..694a5f45f 100644
--- a/frontend/src/views/settings/StorageEngineSettings.vue
+++ b/frontend/src/views/settings/StorageEngineSettings.vue
@@ -34,7 +34,7 @@
<t-option value="cos" :label="$t('settings.storage.engineCos')" />
<t-option value="tos" :label="$t('settings.storage.engineTos')" />
<t-option value="s3" label="AWS S3" />
- <t-option value="oss" label="阿里云 OSS" />
+ <t-option value="oss" :label="$t('settings.storage.engineOss')" />
</t-select>
</div>
</div>
@@ -428,13 +428,13 @@
<div class="engine-header">
<div class="engine-header-info">
<div class="engine-title-row">
- <h3>阿里云 OSS</h3>
+ <h3>{{ $t('settings.storage.ossTitle') }}</h3>
<t-tag theme="success" variant="light" size="small">{{ $t('settings.storage.configurable') }}</t-tag>
</div>
<p>
- 阿里云对象存储服务
- <a class="engine-link" href="https://oss.console.aliyun.com/" target="_blank" rel="noopener">控制台</a>
- <a class="engine-link" href="https://help.aliyun.com/zh/oss/" target="_blank" rel="noopener">文档</a>
+ {{ $t('settings.storage.ossDesc') }}
+ <a class="engine-link" href="https://oss.console.aliyun.com/" target="_blank" rel="noopener">{{ $t('settings.storage.console') }} ↗</a>
+ <a class="engine-link" href="https://help.aliyun.com/zh/oss/" target="_blank" rel="noopener">{{ $t('settings.storage.docs') }} ↗</a>
</p>
</div>
</div>
@@ -459,7 +459,7 @@
<label>Access Key</label>
<t-input
v-model="config.oss.access_key"
- :placeholder="$t('settings.storage.tosAccessKeyPlaceholder')"
+ :placeholder="$t('settings.storage.ossAccessKeyPlaceholder')"
clearable
/>
</div>
@@ -468,7 +468,7 @@
<t-input
v-model="config.oss.secret_key"
type="password"
- :placeholder="$t('settings.storage.tosSecretKeyPlaceholder')"
+ :placeholder="$t('settings.storage.ossSecretKeyPlaceholder')"
clearable
/>
</div>
@@ -757,6 +757,7 @@ function buildPayload(): StorageEngineConfig {
secret_key: (config.value.oss?.secret_key || '').trim(),
bucket_name: (config.value.oss?.bucket_name || '').trim(),
path_prefix: (config.value.oss?.path_prefix || '').trim(),
+ // Temp bucket fields: not exposed in UI; server manages these independently
use_temp_bucket: config.value.oss?.use_temp_bucket ?? false,
temp_bucket_name: (config.value.oss?.temp_bucket_name || '').trim(),
temp_region: (config.value.oss?.temp_region || '').trim(),
@@ -998,7 +999,7 @@ onMounted(loadAll)
label {
font-size: 13px;
font-weight: 500;
- color: var(--td-text-color-secondary)555;
+ color: var(--td-text-color-secondary);
}
&--inline {
diff --git a/internal/handler/system.go b/internal/handler/system.go
index f23bb4bde..51590ecbe 100644
--- a/internal/handler/system.go
+++ b/internal/handler/system.go
@@ -629,6 +629,9 @@ func hasGetObjectAction(action interface{}) bool {
// cosFieldPattern validates COS region and bucket name format to prevent URL injection.
var cosFieldPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$`)
+// ossFieldPattern validates OSS region and bucket name format to prevent URL injection.
+var ossFieldPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$`)
+
// sanitizeStorageCheckError converts a raw storage connectivity error into a safe
// user-facing message that does not leak internal network details (hostnames, IPs, ports).
func sanitizeStorageCheckError(err error) string {
@@ -964,11 +967,21 @@ func (h *SystemHandler) checkOSS(c *gin.Context, ctx context.Context, cfg *types
return
}
- if blocked, reason := isBlockedStorageEndpoint(endpoint); blocked {
+ // Strip URL scheme before SSRF check — OSS endpoint may include http:// or https://
+ ssrfEndpoint := strings.TrimPrefix(strings.TrimPrefix(endpoint, "https://"), "http://")
+ if blocked, reason := isBlockedStorageEndpoint(ssrfEndpoint); blocked {
logger.Warnf(ctx, "Storage check: OSS endpoint blocked by SSRF protection", "endpoint", endpoint)
c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: reason}})
return
}
+ if !ossFieldPattern.MatchString(cfg.Region) {
+ c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: "Region 格式不正确,仅允许字母、数字、点、连字符"}})
+ return
+ }
+ if !ossFieldPattern.MatchString(cfg.BucketName) {
+ c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: "Bucket 名称格式不正确,仅允许字母、数字、点、连字符"}})
+ return
+ }
err := file.CheckOssConnectivity(ctx, endpoint, cfg.Region, accessKey, secretKey, cfg.BucketName)
if err != nil {