35 lines
630 B
Markdown
35 lines
630 B
Markdown
|
|
---
|
||
|
|
paths:
|
||
|
|
- "**/*.go"
|
||
|
|
- "**/go.mod"
|
||
|
|
- "**/go.sum"
|
||
|
|
---
|
||
|
|
# Go 安全性 (Security)
|
||
|
|
|
||
|
|
> 本檔案擴展了 [common/security.md](../common/security.md),包含 Go 特定內容。
|
||
|
|
|
||
|
|
## 金鑰管理 (Secret Management)
|
||
|
|
|
||
|
|
```go
|
||
|
|
apiKey := os.Getenv("OPENAI_API_KEY")
|
||
|
|
if apiKey == "" {
|
||
|
|
log.Fatal("OPENAI_API_KEY not configured")
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## 安全掃描 (Security Scanning)
|
||
|
|
|
||
|
|
- 使用 **gosec** 進行靜態安全分析:
|
||
|
|
```bash
|
||
|
|
gosec ./...
|
||
|
|
```
|
||
|
|
|
||
|
|
## 上下文 (Context) 與 逾時 (Timeouts)
|
||
|
|
|
||
|
|
始終使用 `context.Context` 進行逾時控制:
|
||
|
|
|
||
|
|
```go
|
||
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
```
|