C#: wire types 6 and 7 accepted inside map entries and with DiscardUnknownFields (SkipLastField has no default case)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 77/100
Research direction
Start in csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs and inspect SkipLastField and its callers in ReadMapEntry, SkipGroup, and UnknownFieldSet.MergeFieldFrom with discard enabled. Add coverage for invalid wire types 6 and 7 across those parsing paths, then verify each input is rejected with InvalidProtocolBufferException; the issue does not name test files or a test command.
Written by the indexing model from the issue text.
Description
What version of protobuf and what language are you using?
Version: Google.Protobuf 3.36.2 (latest stable on NuGet) and 4.0.0-rc2. The code below is the same on main (dc47c85).
Language: C#
What operating system (Linux, Windows, ...) and version?
macOS 27.0.1 (arm64)
What runtime / compiler are you using (e.g., python version or gcc version)
.NET SDK 10.0.203, project targeting net8.0
What did you do?
I parsed bytes that contain a tag with wire type 6 or 7. Those wire types are reserved and invalid. Two kinds of input are accepted when they should be rejected:
- A bad tag inside a map entry.
- A bad tag in any message parsed with
WithDiscardUnknownFields(true).
using System;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
static void Try(string label, Func<IMessage> parse)
{
try { parse(); Console.WriteLine($"{label}: accepted"); }
catch (InvalidProtocolBufferException e) { Console.WriteLine($"{label}: rejected ({e.Message})"); }
}
static byte[] Hex(string s) => Convert.FromHexString(s);
// google.protobuf.Struct: field 1 is map<string, Value>; 0a <len> ... is one map entry.
Try("Struct 0a011f", () => Struct.Parser.ParseFrom(Hex("0a011f"))); // entry holds field 3, wire type 7
Try("Struct 0a011e", () => Struct.Parser.ParseFrom(Hex("0a011e"))); // entry holds field 3, wire type 6
Try("Struct 0a031f0a00", () => Struct.Parser.ParseFrom(Hex("0a031f0a00"))); // wire type 7, then key ""
Try("Struct 0a031b1f1c", () => Struct.Parser.ParseFrom(Hex("0a031b1f1c"))); // group in the entry holding wire type 7
Try("Empty 1f", () => Empty.Parser.ParseFrom(Hex("1f"))); // control: unknown field 3, wire type 7
Try("Empty 1f, discard", () => Empty.Parser.WithDiscardUnknownFields(true).ParseFrom(Hex("1f")));
Try("Empty 1e, discard", () => Empty.Parser.WithDiscardUnknownFields(true).ParseFrom(Hex("1e")));
What did you expect to see
Every input rejected, as the control Empty 1f is. protobuf-go v1.36.12 rejects all seven (proto.Unmarshal, and proto.UnmarshalOptions{DiscardUnknown: true} for the last two) with proto: cannot parse invalid wire-format data.
What did you see instead?
Same output on 3.36.2 and 4.0.0-rc2:
Struct 0a011f: accepted
Struct 0a011e: accepted
Struct 0a031f0a00: accepted
Struct 0a031b1f1c: accepted
Empty 1f: rejected (Protocol message contained a tag with an invalid wire type.)
Empty 1f, discard: accepted
Empty 1e, discard: accepted
Each accepted Struct holds one map entry, with an empty key and a null value.
Anything else we should know about your project / environment
The cause appears to be ParsingPrimitivesMessages.SkipLastField (csharp/src/Google.Protobuf/ParsingPrimitivesMessages.cs). Its switch on the wire type has cases for StartGroup, EndGroup, Fixed32, Fixed64, LengthDelimited and Varint, but no default. So for wire type 6 or 7 it consumes nothing and returns.
A message's own unknown fields don't go through it. They go through UnknownFieldSet.MergeFieldFrom, whose switch has default: throw InvalidProtocolBufferException.InvalidWireType();, which is why the control case is rejected. Three callers do reach SkipLastField:
ReadMapEntry, for any tag other than the key and value tags;SkipGroup, for each field inside a skipped group;UnknownFieldSet.MergeFieldFromwhenDiscardUnknownFieldsis set.
A default: throw InvalidProtocolBufferException.InvalidWireType(); in SkipLastField would likely fix all three paths. I haven't built or tested that change.
- Dominant language
- C++
- Stars
- 72.2k
- Forks
- 16.4k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 342
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from protocolbuffers/protobuf
-
feature request packaging & distribution python
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
protocolbuffers/protobuf#31654 ·
Maintainers usually reply within 1 day
-
google-protobuf 0.36.2-release fails to compile for ARMv7: @progbits rejected in global_asm!Possibly taken @farisbasha claimed this 1 day ago. Openbug help wanted rust
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
protocolbuffers/protobuf#31619 · 3 comments ·
Maintainers usually reply within 1 day
-
Null pointer passed to memcpy in lupb_decode() when the second argument is nilPossibly taken A pull request linked to this issue is open or already merged. Openbug help wanted lua
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
protocolbuffers/protobuf#31392 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
protocolbuffers/protobuf#31032 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
protocolbuffers/protobuf#31031 ·
Maintainers usually reply within 1 day
All issues in protocolbuffers/protobuf
Similar issues
-
Needs: Triage :mag: Platform: Android
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
react/react-native#60167 ·
Maintainers usually reply within 2 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
tests: ofx_render_test doesn't compile against FFmpeg 4.x (const AVCodec* to av_find_best_stream)Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NatronGitHub/openfx-io#221 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
subsurface/subsurface#5145 ·
Maintainers usually reply within 2 days