34 lines
1.4 KiB
Go
34 lines
1.4 KiB
Go
// 图片抓取网络约束回归:直接检查地址分类和 URL 格式,并实际拒绝回环目标,不访问公网图片。
|
|
package platform
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestCanvasImageFetchRejectsPrivateAndMetadataNetworks 覆盖私有/映射 IPv4/元数据/保留网段,确认公网样本分类可接受及非 HTTPS/带凭据 URL 被拒。
|
|
func TestCanvasImageFetchRejectsPrivateAndMetadataNetworks(t *testing.T) {
|
|
for _, raw := range []string{"127.0.0.1", "10.0.0.1", "169.254.169.254", "172.16.0.1", "192.168.1.1", "100.100.100.200", "168.63.129.16", "0.0.0.1", "198.18.0.1", "224.0.0.1", "255.255.255.255", "::1", "::ffff:127.0.0.1", "fe80::1", "fc00::1", "2001:db8::1"} {
|
|
if canvasPublicIP(netip.MustParseAddr(raw)) {
|
|
t.Errorf("private/reserved image host accepted: %s", raw)
|
|
}
|
|
}
|
|
for _, raw := range []string{"8.8.8.8", "1.1.1.1", "2606:4700:4700::1111"} {
|
|
if !canvasPublicIP(netip.MustParseAddr(raw)) {
|
|
t.Errorf("public host rejected: %s", raw)
|
|
}
|
|
}
|
|
for _, raw := range []string{"http://example.com/image.png", "file:///etc/passwd", "https://user:password@example.com/x"} {
|
|
if canvasImageURL(raw) {
|
|
t.Errorf("unsafe source accepted: %s", raw)
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
if _, err := fetchCanvasImage(ctx, "https://127.0.0.1/image.png"); err == nil {
|
|
t.Fatal("loopback image fetch permitted")
|
|
}
|
|
}
|