mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-20 14:59:36 +00:00
fastcgi: fail the request when a param name cannot fit in a record
A name longer than maxWrite-8 bytes made the value truncation length negative and panicked when slicing. Return an error instead of forwarding a partial set of params.
This commit is contained in:
parent
c168dbc4ca
commit
aace7615fb
2 changed files with 149 additions and 0 deletions
|
|
@ -17,6 +17,7 @@ package fastcgi
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// streamWriter abstracts out the separation of a stream into discrete records.
|
||||
|
|
@ -81,6 +82,11 @@ func (w *streamWriter) writePairs(pairs map[string]string) error {
|
|||
// init headers
|
||||
w.buf.Write(b)
|
||||
for k, v := range pairs {
|
||||
// a name that cannot fit in a record, even with an empty value,
|
||||
// cannot be sent; fail rather than forward a partial set of params
|
||||
if len(k) > maxWrite-8 {
|
||||
return fmt.Errorf("fastcgi param name too long: %d bytes, max %d", len(k), maxWrite-8)
|
||||
}
|
||||
m := 8 + len(k) + len(v)
|
||||
if m > maxWrite {
|
||||
// param data size exceed 65535 bytes"
|
||||
|
|
|
|||
143
modules/caddyhttp/reverseproxy/fastcgi/writer_test.go
Normal file
143
modules/caddyhttp/reverseproxy/fastcgi/writer_test.go
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
// Copyright 2015 Matthew Holt and The Caddy Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package fastcgi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"net"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWritePairsOversized(t *testing.T) {
|
||||
longestName := strings.Repeat("N", maxWrite-8)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
pairs map[string]string
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "longest name that fits is kept with an empty value",
|
||||
pairs: map[string]string{longestName: "x"},
|
||||
want: map[string]string{longestName: ""},
|
||||
},
|
||||
{
|
||||
name: "long value is truncated",
|
||||
pairs: map[string]string{"K": strings.Repeat("v", maxWrite)},
|
||||
want: map[string]string{"K": strings.Repeat("v", maxWrite-9)},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
data, err := writePairsToPipe(tt.pairs)
|
||||
if err != nil {
|
||||
t.Fatalf("writePairs: %v", err)
|
||||
}
|
||||
|
||||
got := decodeParams(t, data)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("got %d params, want %d", len(got), len(tt.want))
|
||||
for k, v := range got {
|
||||
if tt.want[k] != v {
|
||||
t.Errorf("param with %d-byte name has %d-byte value, want %d bytes", len(k), len(v), len(tt.want[k]))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWritePairsNameTooLong(t *testing.T) {
|
||||
for _, n := range []int{maxWrite - 7, maxWrite + 1} {
|
||||
pairs := map[string]string{"SCRIPT_NAME": "/index.php", strings.Repeat("N", n): "x"}
|
||||
if _, err := writePairsToPipe(pairs); err == nil {
|
||||
t.Errorf("%d-byte name: expected an error, got none", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// writePairsToPipe writes pairs as Params records and returns the bytes
|
||||
// that were sent, along with the error returned by writePairs.
|
||||
func writePairsToPipe(pairs map[string]string) ([]byte, error) {
|
||||
clientConn, serverConn := net.Pipe()
|
||||
defer serverConn.Close()
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
defer clientConn.Close()
|
||||
w := &streamWriter{c: &client{rwc: clientConn, reqID: 1}, buf: new(bytes.Buffer), recType: Params}
|
||||
errCh <- w.writePairs(pairs)
|
||||
}()
|
||||
|
||||
data, readErr := io.ReadAll(serverConn)
|
||||
if err := <-errCh; err != nil {
|
||||
return data, err
|
||||
}
|
||||
return data, readErr
|
||||
}
|
||||
|
||||
// decodeParams reads the name-value pairs out of a stream of Params records.
|
||||
func decodeParams(t *testing.T, data []byte) map[string]string {
|
||||
t.Helper()
|
||||
|
||||
var stream []byte
|
||||
for len(data) > 0 {
|
||||
if len(data) < 8 {
|
||||
t.Fatalf("short record header: %d bytes", len(data))
|
||||
}
|
||||
if data[1] != Params {
|
||||
t.Fatalf("record type %d, want %d", data[1], Params)
|
||||
}
|
||||
contentLen := int(binary.BigEndian.Uint16(data[4:6]))
|
||||
end := 8 + contentLen + int(data[6])
|
||||
if len(data) < end {
|
||||
t.Fatalf("short record: %d bytes, want %d", len(data), end)
|
||||
}
|
||||
stream = append(stream, data[8:8+contentLen]...)
|
||||
data = data[end:]
|
||||
}
|
||||
|
||||
pairs := make(map[string]string)
|
||||
for len(stream) > 0 {
|
||||
var nameLen, valueLen int
|
||||
nameLen, stream = decodeSize(t, stream)
|
||||
valueLen, stream = decodeSize(t, stream)
|
||||
if len(stream) < nameLen+valueLen {
|
||||
t.Fatalf("short pair: %d bytes, want %d", len(stream), nameLen+valueLen)
|
||||
}
|
||||
pairs[string(stream[:nameLen])] = string(stream[nameLen : nameLen+valueLen])
|
||||
stream = stream[nameLen+valueLen:]
|
||||
}
|
||||
return pairs
|
||||
}
|
||||
|
||||
func decodeSize(t *testing.T, b []byte) (int, []byte) {
|
||||
t.Helper()
|
||||
if len(b) == 0 {
|
||||
t.Fatal("missing pair length")
|
||||
}
|
||||
if b[0]>>7 == 0 {
|
||||
return int(b[0]), b[1:]
|
||||
}
|
||||
if len(b) < 4 {
|
||||
t.Fatalf("short pair length: %d bytes", len(b))
|
||||
}
|
||||
return int(binary.BigEndian.Uint32(b) &^ (1 << 31)), b[4:]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue