1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-09 18:25:58 +01:00

Use MaxBytesReader in whip.

This commit is contained in:
Juliusz Chroboczek 2023-12-09 15:51:35 +01:00
parent a283692584
commit 1f3b349ea2
2 changed files with 12 additions and 15 deletions

View file

@ -133,11 +133,17 @@ func httpError(w http.ResponseWriter, err error) {
return return
} }
if os.IsPermission(err) { if os.IsPermission(err) {
http.Error(w, "403 forbidden", http.StatusForbidden) http.Error(w, "Forbidden", http.StatusForbidden)
return
}
var mberr *http.MaxBytesError
if errors.As(err, &mberr) {
http.Error(w, "Request body too large",
http.StatusRequestEntityTooLarge)
return return
} }
log.Printf("HTTP server error: %v", err) log.Printf("HTTP server error: %v", err)
http.Error(w, "500 Internal Server Error", http.Error(w, "Internal server error",
http.StatusInternalServerError) http.StatusInternalServerError)
} }

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -44,16 +43,6 @@ func newId() string {
return base64.RawURLEncoding.EncodeToString(b) return base64.RawURLEncoding.EncodeToString(b)
} }
const sdpLimit = 1024 * 1024
func readLimited(r io.Reader) ([]byte, error) {
v, err := io.ReadAll(io.LimitReader(r, sdpLimit))
if len(v) == sdpLimit {
err = errors.New("SDP too large")
}
return v, err
}
func canPresent(perms []string) bool { func canPresent(perms []string) bool {
for _, p := range perms { for _, p := range perms {
if p == "present" { if p == "present" {
@ -119,6 +108,8 @@ func whipICEServers(w http.ResponseWriter) {
} }
} }
const sdpLimit = 1024 * 1024
func whipEndpointHandler(w http.ResponseWriter, r *http.Request) { func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
if redirect(w, r) { if redirect(w, r) {
return return
@ -181,7 +172,7 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
body, err := readLimited(r.Body) body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, sdpLimit))
if err != nil { if err != nil {
httpError(w, err) httpError(w, err)
return return
@ -311,7 +302,7 @@ func whipResourceHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
body, err := readLimited(r.Body) body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, sdpLimit))
if err != nil { if err != nil {
httpError(w, err) httpError(w, err)
return return