C#: wire types 6 and 7 accepted inside map entries and with DiscardUnknownFields (SkipLastField has no default case)

Open Beginner friendly
#31,768 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

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:

  1. A bad tag inside a map entry.
  2. 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.MergeFieldFrom when DiscardUnknownFields is 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from protocolbuffers/protobuf

All issues in protocolbuffers/protobuf

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.