[MEDIUM] CORS 配置过于宽松:子域名通配 + localhost 任意端口 #7

Open
opened 2026-04-30 03:45:06 +08:00 by lan · 0 comments
Owner

漏洞描述

internal/middleware/cors.go 中的 CORS 配置存在两个问题:

1. 子域名通配过于宽松

if strings.HasPrefix(origin, "https://") && strings.HasSuffix(origin, ".littlelan.cn") {
    return true
}

任何 *.littlelan.cn 子域名都被允许跨域访问并携带凭据。如果攻击者控制了任意子域名(或未来添加的不安全子域名),可读取所有 API 响应数据。

2. localhost 任意端口开放

if strings.HasPrefix(origin, "http://localhost") || strings.HasPrefix(origin, "http://127.0.0.1") {
    return true
}

localhost:8888localhost:9999 等任意端口都被允许。恶意本地应用可通过此方式获取带凭据的 API 访问权限。

修复建议

  1. 子域名匹配改为显式白名单(列出允许的子域名)
  2. localhost 限制为开发常用端口(3000, 5173)
  3. 生产环境可通过环境变量动态配置允许的来源列表

严重程度

MEDIUM — 攻击面取决于子域名控制权和本地恶意软件。

## 漏洞描述 `internal/middleware/cors.go` 中的 CORS 配置存在两个问题: ### 1. 子域名通配过于宽松 ```go if strings.HasPrefix(origin, "https://") && strings.HasSuffix(origin, ".littlelan.cn") { return true } ``` 任何 `*.littlelan.cn` 子域名都被允许跨域访问并携带凭据。如果攻击者控制了任意子域名(或未来添加的不安全子域名),可读取所有 API 响应数据。 ### 2. localhost 任意端口开放 ```go if strings.HasPrefix(origin, "http://localhost") || strings.HasPrefix(origin, "http://127.0.0.1") { return true } ``` `localhost:8888`、`localhost:9999` 等任意端口都被允许。恶意本地应用可通过此方式获取带凭据的 API 访问权限。 ## 修复建议 1. 子域名匹配改为显式白名单(列出允许的子域名) 2. localhost 限制为开发常用端口(3000, 5173) 3. 生产环境可通过环境变量动态配置允许的来源列表 ## 严重程度 **MEDIUM** — 攻击面取决于子域名控制权和本地恶意软件。
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: carrot_bbs/backend#7