sky/internal/platform/storage/presign_test.go
root 6ed55254a0 chore: initial import of sky project
Go + PostgreSQL + Valkey backend (cmd/internal/migrations),
uni-app mobile frontend, PC web (web/), admin console (admin/),
Android WebView container (android_project/).
Build artifacts, node_modules, local env and debug screenshots excluded.
2026-11-09 22:22:44 +08:00

172 lines
4.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package storage
import (
"strings"
"testing"
"time"
)
// 预签名是安全边界:签错一个字节对象存储就会拒绝,
// 签漏一个约束(比如没把 Content-Type 绑进去)就能被绕过。
// 这些测试锁定算法输出的关键性质。
func testPresigner() *Presigner {
return &Presigner{
Endpoint: "https://s3.example.com",
Region: "us-east-1",
Bucket: "test-bucket",
AccessKey: "AKIAIOSFODNN7EXAMPLE",
SecretKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
}
}
func TestSignPutContainsRequiredParams(t *testing.T) {
p := testPresigner()
url, err := p.SignPut("2026/10/ab/file.png", "image/png", 1024, 15*time.Minute)
if err != nil {
t.Fatalf("签名失败: %v", err)
}
required := []string{
"X-Amz-Algorithm=AWS4-HMAC-SHA256",
"X-Amz-Credential=",
"X-Amz-Date=",
"X-Amz-Expires=900",
"X-Amz-SignedHeaders=",
"X-Amz-Signature=",
}
for _, r := range required {
if !strings.Contains(url, r) {
t.Errorf("签名 URL 缺少 %s\nURL: %s", r, url)
}
}
}
// 签名的核心安全属性:Content-Type 必须绑定进签名。
// 若没绑定,客户端可以声明 image/png 却上传任意字节。
func TestSignPutBindsContentType(t *testing.T) {
p := testPresigner()
url1, _ := p.SignPut("a/b.png", "image/png", 1024, 15*time.Minute)
url2, _ := p.SignPut("a/b.png", "image/jpeg", 1024, 15*time.Minute)
sig1 := extractSig(url1)
sig2 := extractSig(url2)
if sig1 == "" || sig2 == "" {
t.Fatal("未能提取签名")
}
if sig1 == sig2 {
t.Fatal("不同 Content-Type 产生了相同签名 —— Content-Type 未绑定进 canonical request")
}
}
// 同理,长度也必须绑定:否则可以声明 1KB 实传 10GB。
func TestSignPutBindsContentLength(t *testing.T) {
p := testPresigner()
url1, _ := p.SignPut("a/b.png", "image/png", 1024, 15*time.Minute)
url2, _ := p.SignPut("a/b.png", "image/png", 2048, 15*time.Minute)
if extractSig(url1) == extractSig(url2) {
t.Fatal("不同长度产生了相同签名 —— Content-Length 未绑定")
}
}
// 过期时间参与签名:篡改 Expires 应导致签名不匹配
func TestSignPutBindsExpiry(t *testing.T) {
p := testPresigner()
url1, _ := p.SignPut("a/b.png", "image/png", 1024, 15*time.Minute)
url2, _ := p.SignPut("a/b.png", "image/png", 1024, 60*time.Minute)
if extractSig(url1) == extractSig(url2) {
t.Fatal("不同过期时间产生了相同签名")
}
if !strings.Contains(url1, "X-Amz-Expires=900") {
t.Error("15 分钟未编码为 900 秒")
}
if !strings.Contains(url2, "X-Amz-Expires=3600") {
t.Error("60 分钟未编码为 3600 秒")
}
}
// 不同密钥必须产出不同签名 —— 否则密钥泄露检测形同虚设
func TestSignPutKeySensitive(t *testing.T) {
url1, _ := testPresigner().SignPut("a/b.png", "image/png", 1024, time.Minute)
p2 := testPresigner()
p2.SecretKey = "different-secret-key"
url2, _ := p2.SignPut("a/b.png", "image/png", 1024, time.Minute)
if extractSig(url1) == extractSig(url2) {
t.Fatal("不同密钥产生相同签名")
}
}
func TestSignGetWorks(t *testing.T) {
p := testPresigner()
url, err := p.SignGet("2026/10/ab/x.jpg", time.Hour)
if err != nil {
t.Fatalf("签名失败: %v", err)
}
if !strings.Contains(url, "X-Amz-Signature=") {
t.Error("GET 签名缺少 Signature")
}
if strings.Contains(url, "content-type") {
t.Error("GET 签名不应绑定 content-type")
}
}
// 路径中的特殊字符必须被正确编码,否则对象存储会把 key 解析错
func TestEscapePath(t *testing.T) {
cases := map[string]string{
"a/b/c.png": "a/b/c.png",
"a b/c.png": "a%20b/c.png",
"中文/图.png": "%E4%B8%AD%E6%96%87/%E5%9B%BE.png",
"a+b/c.png": "a%2Bb/c.png",
}
for in, want := range cases {
got := escapePath(in)
if got != want {
t.Errorf("escapePath(%q) = %q, want %q", in, got, want)
}
}
// 斜杠必须保留,否则目录结构会被破坏
if strings.Count(escapePath("a/b/c"), "/") != 2 {
t.Error("路径分隔符被转义了")
}
}
// ObjectKey 必须是内容寻址的:同内容同 key,不同内容不同 key
func TestObjectKeyContentAddressed(t *testing.T) {
ts := time.Date(2026, 10, 22, 0, 0, 0, 0, time.UTC)
hash := "eb41ad8186ddebf801dd8c64bd75e5b18c95d8bce648be10ae298f5fd97fe3a6"
k1 := ObjectKey(hash, "png", ts)
k2 := ObjectKey(hash, "png", ts)
if k1 != k2 {
t.Error("同一内容产生了不同 key")
}
if k1 != "2026/10/eb/"+hash+".png" {
t.Errorf("key 格式不符: %s", k1)
}
other := "aa41ad8186ddebf801dd8c64bd75e5b18c95d8bce648be10ae298f5fd97fe3a6"
if ObjectKey(other, "png", ts) == k1 {
t.Error("不同内容产生了相同 key")
}
// 扩展名大小写与前导点应被归一
if ObjectKey(hash, ".PNG", ts) != k1 {
t.Error("扩展名未归一化")
}
}
func extractSig(u string) string {
const marker = "X-Amz-Signature="
i := strings.Index(u, marker)
if i < 0 {
return ""
}
s := u[i+len(marker):]
if j := strings.IndexByte(s, '&'); j >= 0 {
s = s[:j]
}
return s
}