geodata: validate entry and code tags before matching

This commit is contained in:
Meo597
2026-10-14 22:04:54 +08:00
parent eb9c6030ab
commit a04770587e
2 changed files with 18 additions and 2 deletions
+4 -2
View File
@@ -148,7 +148,8 @@ func seek(br *bufio.Reader, code []byte, size int64) (int, error) {
need := 2 + codeL // TODO: if code too long
for {
if _, err := br.ReadByte(); err != nil {
tag, err := br.ReadByte()
if err != nil {
return 0, err
}
@@ -170,7 +171,8 @@ func seek(br *bufio.Reader, code []byte, size int64) (int, error) {
}
return 0, err
}
if bodyL >= need && len(prefix) >= need && int(prefix[1]) == codeL && bytes.Equal(prefix[2:], code) {
if tag == 0x0a && bodyL >= need && len(prefix) >= need &&
prefix[0] == 0x0a && int(prefix[1]) == codeL && bytes.Equal(prefix[2:], code) {
return bodyL, nil
}
if _, err := br.Discard(bodyL); err != nil {
+14
View File
@@ -1,6 +1,7 @@
package geodata
import (
"bytes"
"fmt"
"os"
"path/filepath"
@@ -12,6 +13,19 @@ import (
"google.golang.org/protobuf/proto"
)
func TestFindChecksEntryAndCodeTags(t *testing.T) {
for _, payload := range [][]byte{
{0x12, 6, 0x0a, 4, 'T', 'E', 'S', 'T'}, // Unknown field posing as entry.
{0x08, 6, 0x0a, 4, 'T', 'E', 'S', 'T'}, // Wrong entry wire type.
{0x0a, 6, 0x1a, 4, 'T', 'E', 'S', 'T'}, // Unknown field posing as code.
{0x0a, 6, 0x08, 4, 'T', 'E', 'S', 'T'}, // Wrong code wire type.
} {
if _, err := find(bytes.NewReader(payload), []byte("TEST"), true, int64(len(payload))); err == nil {
t.Fatalf("matched incorrect entry/code tags in %x", payload)
}
}
}
func TestLoadFileRejectsVarintOverflow(t *testing.T) {
payload := []byte{
0x0a,