chore: fix form string abuse (#38106)

This commit is contained in:
wxiaoguang
2026-06-15 02:26:22 +08:00
committed by GitHub
parent 3417bc8979
commit 47d48eb208
8 changed files with 25 additions and 47 deletions

View File

@@ -63,20 +63,24 @@ func (s *Sitemap) Add(u URL) {
// WriteTo writes the sitemap to a response
func (s *Sitemap) WriteTo(w io.Writer) (int64, error) {
if l := len(s.URLs); l > urlsLimit {
return 0, fmt.Errorf("The sitemap contains %d URLs, but only %d are allowed", l, urlsLimit)
return 0, fmt.Errorf("sitemap contains %d URLs, but only %d are allowed", l, urlsLimit)
}
if l := len(s.Sitemaps); l > urlsLimit {
return 0, fmt.Errorf("The sitemap contains %d sub-sitemaps, but only %d are allowed", l, urlsLimit)
return 0, fmt.Errorf("sitemap contains %d sub-sitemaps, but only %d are allowed", l, urlsLimit)
}
buf := bytes.NewBufferString(xml.Header)
if err := xml.NewEncoder(buf).Encode(s); err != nil {
encoder := xml.NewEncoder(buf)
defer encoder.Close()
if err := encoder.Encode(s); err != nil {
return 0, err
}
_ = encoder.Flush()
if err := buf.WriteByte('\n'); err != nil {
return 0, err
}
// FIXME: such limit is not right, the content has been written, it would have already caused OOM
if buf.Len() > sitemapFileLimit {
return 0, fmt.Errorf("The sitemap has %d bytes, but only %d are allowed", buf.Len(), sitemapFileLimit)
return 0, fmt.Errorf("sitemap has %d bytes, but only %d are allowed", buf.Len(), sitemapFileLimit)
}
return buf.WriteTo(w)
}

View File

@@ -61,14 +61,14 @@ func TestNewSitemap(t *testing.T) {
{
name: "too many urls",
urls: make([]URL, 50001),
wantErr: "The sitemap contains 50001 URLs, but only 50000 are allowed",
wantErr: "sitemap contains 50001 URLs, but only 50000 are allowed",
},
{
name: "too big file",
urls: []URL{
{URL: strings.Repeat("b", 50*1024*1024+1)},
},
wantErr: "The sitemap has 52428932 bytes, but only 52428800 are allowed",
wantErr: "sitemap has 52428932 bytes, but only 52428800 are allowed",
},
}
for _, tt := range tests {
@@ -137,14 +137,14 @@ func TestNewSitemapIndex(t *testing.T) {
{
name: "too many sitemaps",
urls: make([]URL, 50001),
wantErr: "The sitemap contains 50001 sub-sitemaps, but only 50000 are allowed",
wantErr: "sitemap contains 50001 sub-sitemaps, but only 50000 are allowed",
},
{
name: "too big file",
urls: []URL{
{URL: strings.Repeat("b", 50*1024*1024+1)},
},
wantErr: "The sitemap has 52428952 bytes, but only 52428800 are allowed",
wantErr: "sitemap has 52428952 bytes, but only 52428800 are allowed",
},
}
for _, tt := range tests {