從 local-tool 複製出獨立的「visionA Agent」桌面應用(A3 純橋樑: tunnel client + 配對 UI + 設定,不開 HTTP port、不做本機裝置/推論 UI)。 Bundle ID 與 local-tool 不同(com.innovedus.visiona-agent vs visiona-local), 雙 app 可共存。fork 後不主動 sync,需要時手動 cherry-pick。 Backend / Wails Go(AB1-AB13): - internal/tunnel:6 狀態機(Idle/Connecting/Connected/Reconnecting/Failed/Stopped) + Pair/Unpair/Reconnect/Disconnect binding + ClientHooks event - internal/auth:encrypted file token store(AES-GCM + scrypt + machineID fallback salt + 13 tests) - internal/config:YAML validation + atomic write + 11 tests - internal/log:ring buffer + ExportLog 升級 zip - visionA-backend /api/pairing/exchange:SessionTokenStore + 17 new tests - 三平台 build 驗證(macOS DMG 160 MB / Windows EXE / Linux AppImage) - end-to-end 5 milestone 全綠(pairing → tunnel → forward → reuse 防護 → tunnel drop failover) Frontend / Next.js(AF1-AF7,沿用 visionA-frontend 基礎): - AppShell + Header + TabNav(StatusView / PairView / SettingsView 三 tab) - ConnectionStatusBadge 5 種狀態 - TokenInput regex 驗證 + 7 種錯誤 + 0.5s auto-switch 到狀態頁 - 設定頁 4 區塊(含重新配對 AlertDialog) - agent-api.ts 封裝 Wails bindings(mock/real 雙實作)+ 90 tests Phase 0.7 review-driven fix(Round 2): - A1 Session fixation 防護(RotateSessionID) - A3 mock pairing 預設改 false(必須明確 opt-in)+ startup log - A4 Pair 失敗後 state 清理矩陣(exchange/Save/Start fail 各自終態) - A5 Pair/Unpair/Reconnect lifecycleMu + 50 goroutine race test - F1 重新配對次按鈕 / F2 PairView Esc cancel / F3 Wails BrowserOpenURL / F4 Settings draft 持久 + 未儲存 badge 驗證:agent backend go test -race -count=3 ./... 4 packages 全綠 / agent frontend pnpm test 119 tests 全綠 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package api
|
||
|
||
import (
|
||
"net/http"
|
||
"net/url"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// allowedHosts 定義 CORS 白名單的 hostname。
|
||
// 任何 port 都允許,scheme 只允許 http(本機不可能是 https)。
|
||
//
|
||
// M8-8(TDD v2/cors-security.md §3.1):
|
||
// v2 模式下 UI 改在使用者瀏覽器中跑,server 同時暴露給其他瀏覽器分頁,
|
||
// 必須限定 cross-origin 來源在本機 loopback,避免惡意網站透過 CORS 攻擊。
|
||
var allowedHosts = map[string]bool{
|
||
"127.0.0.1": true,
|
||
"localhost": true,
|
||
"[::1]": true,
|
||
"::1": true,
|
||
}
|
||
|
||
// isAllowedOrigin 判斷 Origin header 是否屬於白名單。
|
||
//
|
||
// 合法例:http://127.0.0.1:3721 / http://localhost:3721 / http://[::1]:3721
|
||
// 不合法例:https://127.0.0.1:3721 / http://evil.com / null / http://192.168.1.5:3721
|
||
//
|
||
// 注意:
|
||
// - 空字串視為非白名單(呼叫端會自行決定 same-origin 路徑)。
|
||
// - "null"(local file、某些 sandboxed iframe)一律拒絕。
|
||
// - 只允許 http scheme,本機不會有 https。
|
||
func isAllowedOrigin(origin string) bool {
|
||
if origin == "" || origin == "null" {
|
||
return false
|
||
}
|
||
u, err := url.Parse(origin)
|
||
if err != nil {
|
||
return false
|
||
}
|
||
if u.Scheme != "http" {
|
||
return false
|
||
}
|
||
host := strings.ToLower(u.Hostname())
|
||
return allowedHosts[host]
|
||
}
|
||
|
||
// CORSMiddleware 僅允許 127.0.0.1/localhost/::1 任意 port 的跨來源請求。
|
||
//
|
||
// 行為(M8-8 / TDD v2/cors-security.md §4.1):
|
||
//
|
||
// 1. Origin header 為空 → same-origin(瀏覽器 same-origin 不送 Origin)→ 直接放行;
|
||
// 若是 OPTIONS 預檢則回 204 即停(避免帶 ACA* 給沒人看的請求)。
|
||
// 2. Origin 在白名單 → 回完整 ACA* headers;OPTIONS → 204;其他方法 → 繼續執行 handler。
|
||
// 3. Origin 不在白名單:
|
||
// - state-changing 方法(POST/PUT/DELETE/PATCH/OPTIONS)→ 403 Forbidden,不回 ACA*。
|
||
// - 簡單讀取(GET/HEAD)→ 執行 handler 但不回 ACA*,瀏覽器 JS 讀不到 body。
|
||
//
|
||
// 為什麼 GET/HEAD 不直接擋:CORS 的設計就是讓 GET 可以執行(畢竟 `<img>`、`<script>` tag
|
||
// 也會送 GET),擋掉反而可能影響 same-origin 的 sub-resource。瀏覽器層的保護
|
||
// 是「不讓 JS 讀回應」,已足夠。對副作用操作我們強制走 POST + 不在白名單時 403。
|
||
func CORSMiddleware() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
origin := c.GetHeader("Origin")
|
||
method := c.Request.Method
|
||
|
||
// Same-origin 請求:瀏覽器 same-origin 不送 Origin,這條走最快路徑。
|
||
if origin == "" {
|
||
if method == http.MethodOptions {
|
||
c.AbortWithStatus(http.StatusNoContent)
|
||
return
|
||
}
|
||
c.Next()
|
||
return
|
||
}
|
||
|
||
if !isAllowedOrigin(origin) {
|
||
// 非白名單 Origin
|
||
// - state-changing 方法 → 403(嚴格擋)
|
||
// - GET/HEAD → 執行但不回 ACA*(瀏覽器層擋)
|
||
if method == http.MethodOptions ||
|
||
method == http.MethodPost ||
|
||
method == http.MethodPut ||
|
||
method == http.MethodDelete ||
|
||
method == http.MethodPatch {
|
||
c.AbortWithStatus(http.StatusForbidden)
|
||
return
|
||
}
|
||
c.Next()
|
||
return
|
||
}
|
||
|
||
// 白名單 Origin:回完整 ACA* headers
|
||
c.Header("Access-Control-Allow-Origin", origin)
|
||
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||
c.Header("Access-Control-Allow-Credentials", "true")
|
||
c.Header("Vary", "Origin")
|
||
|
||
if method == http.MethodOptions {
|
||
c.AbortWithStatus(http.StatusNoContent)
|
||
return
|
||
}
|
||
c.Next()
|
||
}
|
||
}
|