Protobuf: `nextName()` / `nextNameMatch()` do not skip an unread string value, decode its bytes as the next tag

Đã đóng Phù hợp với người mới
#874 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Một pull request liên quan đã được merge.

  • #878 của @cowtowncoder — đã merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java
Lĩnh vực
backend, databases

Hướng nghiên cứu

Bắt đầu từ ProtobufParser.nextToken() và so sánh cách nó bỏ qua một giá trị chưa hoàn chỉnh với nextName(), nextName(SerializableString) và nextNameMatch(PropertyNameMatcher). Tái hiện lỗi bằng ví dụ ở cấp parser được cung cấp, trong đó một chuỗi chưa được đọc đứng trước trường a, rồi bổ sung kiểm thử cho từng phương thức và trường hợp databind. Hoàn tất khi các lời gọi đó đọc được a = 5 và các bài kiểm thử của mô-đun protobuf đều chạy qua.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

protobuf

ProtobufParser.nextToken() starts by skipping the rest of a string or bytes value that was not read:

if (_tokenIncomplete) {
    _tokenIncomplete = false;
    _skipBytes(_decodedLength);
}

nextName(), nextName(SerializableString) and nextNameMatch(PropertyNameMatcher) do not. If the value before them was never accessed, its bytes are decoded as the next field tag.

This happens in databind whenever a string value is skipped without being read (for example a field in the schema that the bean does not have) and is followed by another field. Reproduced on 3.x (ab5956249); 3.2 is the same, and 2.x has the same omission in nextFieldName() and nextFieldName(SerializableString).

Reproduction
public static class Bean { public int a; }

ProtobufMapper m = ProtobufMapper.builder()
        .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
ProtobufSchema schema = m.schemaLoader()
        .parse("message M { optional string extra = 1; optional int32 a = 2; }");
Map<String, Object> value = new LinkedHashMap<>();
value.put("extra", "zz");
value.put("a", 5);
byte[] doc = m.writer(schema).writeValueAsBytes(value);

// Parser level
try (JsonParser p = m.reader(schema).createParser(doc)) {
    p.nextToken(); // START_OBJECT
    p.nextToken(); // PROPERTY_NAME "extra"
    p.nextToken(); // VALUE_STRING, text not accessed
    p.nextName();  // expected "a"
}

// Databind
m.readerFor(Bean.class).with(schema).readValue(doc);
Call Result
nextToken() instead of nextName() name a, value 5 (correct)
nextName() StreamReadException: Undefined property (id 15, wire type 2) for message type M: not allowed to ignore, as StreamReadFeature.IGNORE_UNDEFINED disabled
readValue(doc) into Bean same exception (with IGNORE_UNDEFINED enabled: Unexpected end-of-input in VALUE_STRING)

The tag id 15, wire type 2 is the string's own bytes, "zz" (0x7A), decoded as a tag.

Suggested fix

Add the same skip at the start of the three methods (and of the two nextFieldName() methods on 2.x):

// As in nextToken(): skip the rest of a value that was not read
if (_tokenIncomplete) {
    _tokenIncomplete = false;
    _skipBytes(_decodedLength);
}

With this change, both the parser-level and the databind cases read a = 5, and the protobuf module's tests pass (201 tests).

(Found while comparing stock databind with jackson-module-blackbird, which uses nextNameMatch() for every property name, including the first one and names in records.)

Ngôn ngữ chính
Java
Star
348
Fork
159
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
45

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của FasterXML/jackson-dataformats-binary

Tất cả issue của FasterXML/jackson-dataformats-binary

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.