Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package bsoncore |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "errors" |
| 12 | "fmt" |
| 13 | "io" |
| 14 | "strconv" |
| 15 | |
| 16 | "github.com/go-stack/stack" |
| 17 | "github.com/mongodb/mongo-go-driver/bson/bsontype" |
| 18 | ) |
| 19 | |
| 20 | // DocumentValidationError is an error type returned when attempting to validate a document. |
| 21 | type DocumentValidationError string |
| 22 | |
| 23 | func (dve DocumentValidationError) Error() string { return string(dve) } |
| 24 | |
| 25 | // NewDocumentLengthError creates and returns an error for when the length of a document exceeds the |
| 26 | // bytes available. |
| 27 | func NewDocumentLengthError(length, rem int) error { |
| 28 | return DocumentValidationError( |
| 29 | fmt.Sprintf("document length exceeds available bytes. length=%d remainingBytes=%d", length, rem), |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | // InsufficientBytesError indicates that there were not enough bytes to read the next component. |
| 34 | type InsufficientBytesError struct { |
| 35 | Source []byte |
| 36 | Remaining []byte |
| 37 | Stack stack.CallStack |
| 38 | } |
| 39 | |
| 40 | // NewInsufficientBytesError creates a new InsufficientBytesError with the given Document, remaining |
| 41 | // bytes, and the current stack. |
| 42 | func NewInsufficientBytesError(src, rem []byte) InsufficientBytesError { |
| 43 | return InsufficientBytesError{Source: src, Remaining: rem, Stack: stack.Trace().TrimRuntime()} |
| 44 | } |
| 45 | |
| 46 | // Error implements the error interface. |
| 47 | func (ibe InsufficientBytesError) Error() string { |
| 48 | return "too few bytes to read next component" |
| 49 | } |
| 50 | |
| 51 | // ErrorStack returns a string representing the stack at the point where the error occurred. |
| 52 | func (ibe InsufficientBytesError) ErrorStack() string { |
| 53 | s := bytes.NewBufferString("too few bytes to read next component: [") |
| 54 | |
| 55 | for i, call := range ibe.Stack { |
| 56 | if i != 0 { |
| 57 | s.WriteString(", ") |
| 58 | } |
| 59 | |
| 60 | // go vet doesn't like %k even though it's part of stack's API, so we move the format |
| 61 | // string so it doesn't complain. (We also can't make it a constant, or go vet still |
| 62 | // complains.) |
| 63 | callFormat := "%k.%n %v" |
| 64 | |
| 65 | s.WriteString(fmt.Sprintf(callFormat, call, call, call)) |
| 66 | } |
| 67 | |
| 68 | s.WriteRune(']') |
| 69 | |
| 70 | return s.String() |
| 71 | } |
| 72 | |
| 73 | // Equal checks that err2 also is an ErrTooSmall. |
| 74 | func (ibe InsufficientBytesError) Equal(err2 error) bool { |
| 75 | switch err2.(type) { |
| 76 | case InsufficientBytesError: |
| 77 | return true |
| 78 | default: |
| 79 | return false |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // InvalidDepthTraversalError is returned when attempting a recursive Lookup when one component of |
| 84 | // the path is neither an embedded document nor an array. |
| 85 | type InvalidDepthTraversalError struct { |
| 86 | Key string |
| 87 | Type bsontype.Type |
| 88 | } |
| 89 | |
| 90 | func (idte InvalidDepthTraversalError) Error() string { |
| 91 | return fmt.Sprintf( |
| 92 | "attempt to traverse into %s, but it's type is %s, not %s nor %s", |
| 93 | idte.Key, idte.Type, bsontype.EmbeddedDocument, bsontype.Array, |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | // ErrMissingNull is returned when a document's last byte is not null. |
| 98 | const ErrMissingNull DocumentValidationError = "document end is missing null byte" |
| 99 | |
| 100 | // ErrNilReader indicates that an operation was attempted on a nil io.Reader. |
| 101 | var ErrNilReader = errors.New("nil reader") |
| 102 | |
| 103 | // ErrInvalidLength indicates that a length in a binary representation of a BSON document is invalid. |
| 104 | var ErrInvalidLength = errors.New("document length is invalid") |
| 105 | |
| 106 | // ErrEmptyKey indicates that no key was provided to a Lookup method. |
| 107 | var ErrEmptyKey = errors.New("empty key provided") |
| 108 | |
| 109 | // ErrElementNotFound indicates that an Element matching a certain condition does not exist. |
| 110 | var ErrElementNotFound = errors.New("element not found") |
| 111 | |
| 112 | // ErrOutOfBounds indicates that an index provided to access something was invalid. |
| 113 | var ErrOutOfBounds = errors.New("out of bounds") |
| 114 | |
| 115 | // Document is a raw bytes representation of a BSON document. |
| 116 | type Document []byte |
| 117 | |
| 118 | // NewDocumentFromReader reads a document from r. This function will only validate the length is |
| 119 | // correct and that the document ends with a null byte. |
| 120 | func NewDocumentFromReader(r io.Reader) (Document, error) { |
| 121 | if r == nil { |
| 122 | return nil, ErrNilReader |
| 123 | } |
| 124 | |
| 125 | var lengthBytes [4]byte |
| 126 | |
| 127 | // ReadFull guarantees that we will have read at least len(lengthBytes) if err == nil |
| 128 | _, err := io.ReadFull(r, lengthBytes[:]) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | |
| 133 | length, _, _ := readi32(lengthBytes[:]) // ignore ok since we always have enough bytes to read a length |
| 134 | if length < 0 { |
| 135 | return nil, ErrInvalidLength |
| 136 | } |
| 137 | document := make([]byte, length) |
| 138 | |
| 139 | copy(document, lengthBytes[:]) |
| 140 | |
| 141 | _, err = io.ReadFull(r, document[4:]) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | if document[length-1] != 0x00 { |
| 147 | return nil, ErrMissingNull |
| 148 | } |
| 149 | |
| 150 | return document, nil |
| 151 | } |
| 152 | |
| 153 | // Lookup searches the document, potentially recursively, for the given key. If there are multiple |
| 154 | // keys provided, this method will recurse down, as long as the top and intermediate nodes are |
| 155 | // either documents or arrays. If an error occurs or if the value doesn't exist, an empty Value is |
| 156 | // returned. |
| 157 | func (d Document) Lookup(key ...string) Value { |
| 158 | val, _ := d.LookupErr(key...) |
| 159 | return val |
| 160 | } |
| 161 | |
| 162 | // LookupErr is the same as Lookup, except it returns an error in addition to an empty Value. |
| 163 | func (d Document) LookupErr(key ...string) (Value, error) { |
| 164 | if len(key) < 1 { |
| 165 | return Value{}, ErrEmptyKey |
| 166 | } |
| 167 | length, rem, ok := ReadLength(d) |
| 168 | if !ok { |
| 169 | return Value{}, NewInsufficientBytesError(d, rem) |
| 170 | } |
| 171 | |
| 172 | length -= 4 |
| 173 | |
| 174 | var elem Element |
| 175 | for length > 1 { |
| 176 | elem, rem, ok = ReadElement(rem) |
| 177 | length -= int32(len(elem)) |
| 178 | if !ok { |
| 179 | return Value{}, NewInsufficientBytesError(d, rem) |
| 180 | } |
| 181 | if elem.Key() != key[0] { |
| 182 | continue |
| 183 | } |
| 184 | if len(key) > 1 { |
| 185 | tt := bsontype.Type(elem[0]) |
| 186 | switch tt { |
| 187 | case bsontype.EmbeddedDocument: |
| 188 | val, err := elem.Value().Document().LookupErr(key[1:]...) |
| 189 | if err != nil { |
| 190 | return Value{}, err |
| 191 | } |
| 192 | return val, nil |
| 193 | case bsontype.Array: |
| 194 | val, err := elem.Value().Array().LookupErr(key[1:]...) |
| 195 | if err != nil { |
| 196 | return Value{}, err |
| 197 | } |
| 198 | return val, nil |
| 199 | default: |
| 200 | return Value{}, InvalidDepthTraversalError{Key: elem.Key(), Type: tt} |
| 201 | } |
| 202 | } |
| 203 | return elem.ValueErr() |
| 204 | } |
| 205 | return Value{}, ErrElementNotFound |
| 206 | } |
| 207 | |
| 208 | // Index searches for and retrieves the element at the given index. This method will panic if |
| 209 | // the document is invalid or if the index is out of bounds. |
| 210 | func (d Document) Index(index uint) Element { |
| 211 | elem, err := d.IndexErr(index) |
| 212 | if err != nil { |
| 213 | panic(err) |
| 214 | } |
| 215 | return elem |
| 216 | } |
| 217 | |
| 218 | // IndexErr searches for and retrieves the element at the given index. |
| 219 | func (d Document) IndexErr(index uint) (Element, error) { |
| 220 | length, rem, ok := ReadLength(d) |
| 221 | if !ok { |
| 222 | return nil, NewInsufficientBytesError(d, rem) |
| 223 | } |
| 224 | |
| 225 | length -= 4 |
| 226 | |
| 227 | var current uint |
| 228 | var elem Element |
| 229 | for length > 1 { |
| 230 | elem, rem, ok = ReadElement(rem) |
| 231 | length -= int32(len(elem)) |
| 232 | if !ok { |
| 233 | return nil, NewInsufficientBytesError(d, rem) |
| 234 | } |
| 235 | if current != index { |
| 236 | current++ |
| 237 | continue |
| 238 | } |
| 239 | return elem, nil |
| 240 | } |
| 241 | return nil, ErrOutOfBounds |
| 242 | } |
| 243 | |
| 244 | // DebugString outputs a human readable version of Document. It will attempt to stringify the |
| 245 | // valid components of the document even if the entire document is not valid. |
| 246 | func (d Document) DebugString() string { |
| 247 | if len(d) < 5 { |
| 248 | return "<malformed>" |
| 249 | } |
| 250 | var buf bytes.Buffer |
| 251 | buf.WriteString("Document") |
| 252 | length, rem, _ := ReadLength(d) // We know we have enough bytes to read the length |
| 253 | buf.WriteByte('(') |
| 254 | buf.WriteString(strconv.Itoa(int(length))) |
| 255 | length -= 4 |
| 256 | buf.WriteString("){") |
| 257 | var elem Element |
| 258 | var ok bool |
| 259 | for length > 1 { |
| 260 | elem, rem, ok = ReadElement(rem) |
| 261 | length -= int32(len(elem)) |
| 262 | if !ok { |
| 263 | buf.WriteString(fmt.Sprintf("<malformed (%d)>", length)) |
| 264 | break |
| 265 | } |
| 266 | fmt.Fprintf(&buf, "%s ", elem.DebugString()) |
| 267 | } |
| 268 | buf.WriteByte('}') |
| 269 | |
| 270 | return buf.String() |
| 271 | } |
| 272 | |
| 273 | // String outputs an ExtendedJSON version of Document. If the document is not valid, this method |
| 274 | // returns an empty string. |
| 275 | func (d Document) String() string { |
| 276 | if len(d) < 5 { |
| 277 | return "" |
| 278 | } |
| 279 | var buf bytes.Buffer |
| 280 | buf.WriteByte('{') |
| 281 | |
| 282 | length, rem, _ := ReadLength(d) // We know we have enough bytes to read the length |
| 283 | |
| 284 | length -= 4 |
| 285 | |
| 286 | var elem Element |
| 287 | var ok bool |
| 288 | first := true |
| 289 | for length > 1 { |
| 290 | if !first { |
| 291 | buf.WriteByte(',') |
| 292 | } |
| 293 | elem, rem, ok = ReadElement(rem) |
| 294 | length -= int32(len(elem)) |
| 295 | if !ok { |
| 296 | return "" |
| 297 | } |
| 298 | fmt.Fprintf(&buf, "%s", elem.String()) |
| 299 | first = false |
| 300 | } |
| 301 | buf.WriteByte('}') |
| 302 | |
| 303 | return buf.String() |
| 304 | } |
| 305 | |
| 306 | // Elements returns this document as a slice of elements. The returned slice will contain valid |
| 307 | // elements. If the document is not valid, the elements up to the invalid point will be returned |
| 308 | // along with an error. |
| 309 | func (d Document) Elements() ([]Element, error) { |
| 310 | length, rem, ok := ReadLength(d) |
| 311 | if !ok { |
| 312 | return nil, NewInsufficientBytesError(d, rem) |
| 313 | } |
| 314 | |
| 315 | length -= 4 |
| 316 | |
| 317 | var elem Element |
| 318 | var elems []Element |
| 319 | for length > 1 { |
| 320 | elem, rem, ok = ReadElement(rem) |
| 321 | length -= int32(len(elem)) |
| 322 | if !ok { |
| 323 | return elems, NewInsufficientBytesError(d, rem) |
| 324 | } |
| 325 | if err := elem.Validate(); err != nil { |
| 326 | return elems, err |
| 327 | } |
| 328 | elems = append(elems, elem) |
| 329 | } |
| 330 | return elems, nil |
| 331 | } |
| 332 | |
| 333 | // Values returns this document as a slice of values. The returned slice will contain valid values. |
| 334 | // If the document is not valid, the values up to the invalid point will be returned along with an |
| 335 | // error. |
| 336 | func (d Document) Values() ([]Value, error) { |
| 337 | length, rem, ok := ReadLength(d) |
| 338 | if !ok { |
| 339 | return nil, NewInsufficientBytesError(d, rem) |
| 340 | } |
| 341 | |
| 342 | length -= 4 |
| 343 | |
| 344 | var elem Element |
| 345 | var vals []Value |
| 346 | for length > 1 { |
| 347 | elem, rem, ok = ReadElement(rem) |
| 348 | length -= int32(len(elem)) |
| 349 | if !ok { |
| 350 | return vals, NewInsufficientBytesError(d, rem) |
| 351 | } |
| 352 | if err := elem.Value().Validate(); err != nil { |
| 353 | return vals, err |
| 354 | } |
| 355 | vals = append(vals, elem.Value()) |
| 356 | } |
| 357 | return vals, nil |
| 358 | } |
| 359 | |
| 360 | // Validate validates the document and ensures the elements contained within are valid. |
| 361 | func (d Document) Validate() error { |
| 362 | length, rem, ok := ReadLength(d) |
| 363 | if !ok { |
| 364 | return NewInsufficientBytesError(d, rem) |
| 365 | } |
| 366 | if int(length) > len(d) { |
| 367 | return d.lengtherror(int(length), len(d)) |
| 368 | } |
| 369 | if d[length-1] != 0x00 { |
| 370 | return ErrMissingNull |
| 371 | } |
| 372 | |
| 373 | length -= 4 |
| 374 | var elem Element |
| 375 | |
| 376 | for length > 1 { |
| 377 | elem, rem, ok = ReadElement(rem) |
| 378 | length -= int32(len(elem)) |
| 379 | if !ok { |
| 380 | return NewInsufficientBytesError(d, rem) |
| 381 | } |
| 382 | err := elem.Validate() |
| 383 | if err != nil { |
| 384 | return err |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if len(rem) < 1 || rem[0] != 0x00 { |
| 389 | return ErrMissingNull |
| 390 | } |
| 391 | return nil |
| 392 | } |
| 393 | |
| 394 | func (Document) lengtherror(length, rem int) error { |
| 395 | return DocumentValidationError(fmt.Sprintf("document length exceeds available bytes. length=%d remainingBytes=%d", length, rem)) |
| 396 | } |