summaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/pgx/v5/pgproto3
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/jackc/pgx/v5/pgproto3')
-rw-r--r--vendor/github.com/jackc/pgx/v5/pgproto3/backend.go13
-rw-r--r--vendor/github.com/jackc/pgx/v5/pgproto3/pgproto3.go9
2 files changed, 22 insertions, 0 deletions
diff --git a/vendor/github.com/jackc/pgx/v5/pgproto3/backend.go b/vendor/github.com/jackc/pgx/v5/pgproto3/backend.go
index 6db77e4a2..efa909c3a 100644
--- a/vendor/github.com/jackc/pgx/v5/pgproto3/backend.go
+++ b/vendor/github.com/jackc/pgx/v5/pgproto3/backend.go
@@ -38,6 +38,7 @@ type Backend struct {
terminate Terminate
bodyLen int
+ maxBodyLen int // maxBodyLen is the maximum length of a message body in octets. If a message body exceeds this length, Receive will return an error.
msgType byte
partialMsg bool
authType uint32
@@ -158,6 +159,9 @@ func (b *Backend) Receive() (FrontendMessage, error) {
b.msgType = header[0]
b.bodyLen = int(binary.BigEndian.Uint32(header[1:])) - 4
+ if b.maxBodyLen > 0 && b.bodyLen > b.maxBodyLen {
+ return nil, &ExceededMaxBodyLenErr{b.maxBodyLen, b.bodyLen}
+ }
b.partialMsg = true
}
@@ -260,3 +264,12 @@ func (b *Backend) SetAuthType(authType uint32) error {
return nil
}
+
+// SetMaxBodyLen sets the maximum length of a message body in octets. If a message body exceeds this length, Receive will return
+// an error. This is useful for protecting against malicious clients that send large messages with the intent of
+// causing memory exhaustion.
+// The default value is 0.
+// If maxBodyLen is 0, then no maximum is enforced.
+func (b *Backend) SetMaxBodyLen(maxBodyLen int) {
+ b.maxBodyLen = maxBodyLen
+}
diff --git a/vendor/github.com/jackc/pgx/v5/pgproto3/pgproto3.go b/vendor/github.com/jackc/pgx/v5/pgproto3/pgproto3.go
index ef5a54896..8df383c2c 100644
--- a/vendor/github.com/jackc/pgx/v5/pgproto3/pgproto3.go
+++ b/vendor/github.com/jackc/pgx/v5/pgproto3/pgproto3.go
@@ -70,6 +70,15 @@ func (e *writeError) Unwrap() error {
return e.err
}
+type ExceededMaxBodyLenErr struct {
+ MaxExpectedBodyLen int
+ ActualBodyLen int
+}
+
+func (e *ExceededMaxBodyLenErr) Error() string {
+ return fmt.Sprintf("invalid body length: expected at most %d, but got %d", e.MaxExpectedBodyLen, e.ActualBodyLen)
+}
+
// getValueFromJSON gets the value from a protocol message representation in JSON.
func getValueFromJSON(v map[string]string) ([]byte, error) {
if v == nil {