mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-01-08 17:51:57 +00:00
sts: Limit the size of the HTTPS reads
To avoid accidents/DoS when we are fetching a very very large policy, this patch limits the size of the reads to 10k, which should be more than enough for any reasonable policy as per the current draft.
This commit is contained in:
@@ -195,7 +195,9 @@ func httpGet(ctx context.Context, url string) ([]byte, error) {
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode == http.StatusOK {
|
||||||
return ioutil.ReadAll(resp.Body)
|
// Read but up to 10k; policies should be way smaller than that, and
|
||||||
|
// having a limit prevents abuse/accidents with very large replies.
|
||||||
|
return ioutil.ReadAll(&io.LimitedReader{resp.Body, 10 * 1024})
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("HTTP response status code: %v", resp.StatusCode)
|
return nil, fmt.Errorf("HTTP response status code: %v", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -157,6 +157,22 @@ func TestFetch(t *testing.T) {
|
|||||||
t.Logf("version99: got expected error: %v", err)
|
t.Logf("version99: got expected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPolicyTooBig(t *testing.T) {
|
||||||
|
// Construct a valid but very large JSON as a policy.
|
||||||
|
raw := `{"version": "STSv1", "mode": "enforce", "mx": [`
|
||||||
|
for i := 0; i < 2000; i++ {
|
||||||
|
raw += fmt.Sprintf("\"mx%d\", ", i)
|
||||||
|
}
|
||||||
|
raw += `"mxlast"], "max_age": 100}`
|
||||||
|
policyForDomain["toobig"] = raw
|
||||||
|
|
||||||
|
_, err := Fetch(context.Background(), "toobig")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("fetch worked, but should have failed")
|
||||||
|
}
|
||||||
|
t.Logf("got error as expected: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Tests for the policy cache.
|
// Tests for the policy cache.
|
||||||
|
|
||||||
func mustTempDir(t *testing.T) string {
|
func mustTempDir(t *testing.T) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user