Documentation
¶
Overview ¶
Package grpcwire reads gRPC request messages without their schema: it splits a body into gRPC messages and finds the text in each protobuf message, for rules to inspect.
Protobuf's wire format doesn't say which length-delimited fields are strings, bytes or nested messages. A field whose bytes are a whole protobuf message is read as one (its fields get their own names); a field whose bytes are UTF-8 is text, whatever its characters. Some bytes are both, and give both. A group is read as a nested message. Binary bytes give their runs of readable text. Numbers outside a length-delimited field and anything that fails to parse give nothing.
Index ¶
Constants ¶
const FrameHeader = 5
FrameHeader is a gRPC message's prefix: a compressed flag (0 or 1) and the message length, 4 bytes big-endian.
const LongPath = "~"
LongPath ends a path cut to MaxName bytes: no ArgumentPattern has it.
const MaxName = 256
MaxName is the longest field path Fields gives, the longest an exclusion can name (ArgumentPattern): a longer one keeps its first MaxName-1 bytes and ends in LongPath, which no exclusion can name, so that one exclusion never covers every path cut to the same name.
Variables ¶
var DefaultLimits = Limits{Depth: 16, Fields: 1024}
DefaultLimits are those the pipeline uses.
Functions ¶
Types ¶
type Field ¶
Field is text found in a message: Name is the path of field numbers from the message, dot-separated ("2.1": field 1 of the message in field 2).
func Fields ¶
func Fields(msg []byte, s *grpcschema.Schema, t *grpcschema.Message, lim Limits, out []Field) (fields []Field, unknown int, ok bool)
Fields returns the text fields of msg, a protobuf message of type t of schema s, appended to out; unknown, the fields t doesn't describe; and whether the message parsed whole and stayed within lim (design 0013 §6).
Each field t describes is read by its kind: a string as text (as Strings reads UTF-8, a byte that isn't UTF-8 a space), bytes as text (bytesText): whole when they are mostly printable UTF-8 of at least 8 bytes (minBytesText), else by their runs of at least 12 readable characters (minBytesRun), a message or group by its type, one level deeper (a message also as text, as Strings reads one: whole when printable, else the runs between its fields), numbers as nothing, and a packed repeated number as bytes, or as unknown when it isn't a list of its kind's numbers. Fields are named by their proto names, joined by dots ("user.name"), without indexes; an extension "[package.extension]". An Any's value is read as the type its last type_url names, under the Any's name, when s has it and it is a message; else as Strings reads it.
What t doesn't describe is read as Strings reads it, named by number under the nearest name ("user.7"): a field number t doesn't have, a field sent with a wire type its kind doesn't take, and a message field whose bytes aren't a message. Every occurrence of a field is read, and every member of a oneof. The reading is charged against a budget of bytes read linear in msg (readBudget), and the text given against one of bytes of text (textBudget): past either, the message is over lim.
func Strings ¶
Strings returns the text fields of a protobuf message, appended to out, and reports whether the message parsed whole and stayed within lim. A message that doesn't parse gives what was found before the error. A panic while reading (it runs on an adapter's goroutine, which nothing else recovers) is a message not read whole, as one that doesn't parse.
type Limits ¶
type Limits struct {
// Depth is how deep nested messages are read.
Depth int
// Fields is how many text fields are returned.
Fields int
}
Limits bound the work on one body.