Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
«ÍïMaxMind.com
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The pull request title and description indicate that 4 minimal corrupt test databases are being added, including several variants for search-tree corruption (libmaxminddb-separator-record-*.mmdb). However, only libmaxminddb-metadata-marker-only.mmdb is included in the current diff. Please ensure all intended test fixtures are included in the commit.

Additionally, there is a discrepancy in the description: it mentions "three variants" of the separator record databases, but the pattern {min,max}-{left,right} typically expands to four files. Please clarify the intended set of fixtures.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Most corrupt databases in this repository (as seen in pkg/writer/baddata.go) are generated programmatically. To maintain consistency and allow for easier inspection or future modification, consider adding these new test cases to the WriteBadDataDBs function in pkg/writer/baddata.go instead of checking in binary blobs. For example, libmaxminddb-metadata-marker-only.mmdb is a simple 14-byte file that could be represented as a byte slice in code.

Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions pkg/writer/baddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (w *Writer) WriteBadDataDBs(target string) error {
{"libmaxminddb-corrupt-search-tree.mmdb", buildCorruptSearchTreeDB()},
{"libmaxminddb-empty-map-last-in-metadata.mmdb", buildEmptyMapLastInMetadataDB()},
{"libmaxminddb-empty-array-last-in-metadata.mmdb", buildEmptyArrayLastInMetadataDB()},
{"libmaxminddb-metadata-marker-only.mmdb", buildMetadataMarkerOnlyDB()},
{"libmaxminddb-separator-record-min-left.mmdb", buildSeparatorRecordMinLeftDB()},
{"libmaxminddb-separator-record-min-right.mmdb", buildSeparatorRecordMinRightDB()},
{"libmaxminddb-separator-record-max-left.mmdb", buildSeparatorRecordMaxLeftDB()},
} {
if err := writeRawDB(target, db.name, db.data); err != nil {
return fmt.Errorf("writing %s: %w", db.name, err)
Expand Down
81 changes: 75 additions & 6 deletions pkg/writer/rawmmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ func writeEmptyArray(buf []byte) int {
// writeSearchTree writes a 1-node search tree with 24-bit records,
// both pointing to the data section.
func writeSearchTree(buf []byte, recordValue uint32) int {
buf[0] = byte((recordValue >> 16) & 0xFF)
buf[1] = byte((recordValue >> 8) & 0xFF)
buf[2] = byte(recordValue & 0xFF)
buf[3] = byte((recordValue >> 16) & 0xFF)
buf[4] = byte((recordValue >> 8) & 0xFF)
buf[5] = byte(recordValue & 0xFF)
return writeSearchTreeRecords(buf, recordValue, recordValue)
}

// writeSearchTreeRecords writes a 1-node search tree with 24-bit records
// where the left and right records can hold different values.
func writeSearchTreeRecords(buf []byte, leftRecord, rightRecord uint32) int {
buf[0] = byte((leftRecord >> 16) & 0xFF)
buf[1] = byte((leftRecord >> 8) & 0xFF)
buf[2] = byte(leftRecord & 0xFF)
buf[3] = byte((rightRecord >> 16) & 0xFF)
buf[4] = byte((rightRecord >> 8) & 0xFF)
buf[5] = byte(rightRecord & 0xFF)
return 6
}

Expand Down Expand Up @@ -285,6 +291,69 @@ func buildEmptyMapLastInMetadataDB() []byte {
return buildSimpleDB(writeMetadataBlockEmptyMapLast)
}

// buildMetadataMarkerOnlyDB returns a file that contains only the metadata
// marker (\xab\xcd\xefMaxMind.com) with no metadata bytes following.
// Readers should reject this as invalid metadata rather than allowing a
// zero-length metadata section to reach the decoder.
func buildMetadataMarkerOnlyDB() []byte {
return []byte(metadataMarker)
}

// buildSeparatorRecordDB creates a complete 1-node MMDB where the left and
// right records of node 0 hold the given values. With nodeCount = 1, any
// record value in the half-open range [2, 17) points into the 16-byte
// separator between the search tree and data section. Readers should reject
// such records as a corrupt search tree rather than exposing them as data
// entries with underflowed offsets.
func buildSeparatorRecordDB(leftRecord, rightRecord uint32) []byte {
const nodeCount = 1
const buildEpoch = 1_000_000_000

buf := make([]byte, 1024)
pos := 0

pos += writeSearchTreeRecords(buf[pos:], leftRecord, rightRecord)

// 16-byte null separator
pos += dataSeparatorSize

// Data section: a simple map so a valid record (nodeCount + 16) can
// resolve to a real entry.
pos += writeMap(buf[pos:], 1)
pos += writeString(buf[pos:], "ip")
pos += writeString(buf[pos:], "test")

pos += writeMetadataBlock(buf[pos:], nodeCount, buildEpoch)

return buf[:pos]
}

// buildSeparatorRecordMinLeftDB creates an MMDB whose node 0 left record
// equals nodeCount + 1 (the first byte of the data section separator).
func buildSeparatorRecordMinLeftDB() []byte {
const nodeCount = 1
const validRecord = nodeCount + dataSeparatorSize
return buildSeparatorRecordDB(nodeCount+1, validRecord)
}

// buildSeparatorRecordMinRightDB creates an MMDB whose node 0 right record
// equals nodeCount + 1. The left record is valid, so this exercises the
// right-record-corruption path independently.
func buildSeparatorRecordMinRightDB() []byte {
const nodeCount = 1
const validRecord = nodeCount + dataSeparatorSize
return buildSeparatorRecordDB(validRecord, nodeCount+1)
}

// buildSeparatorRecordMaxLeftDB creates an MMDB whose node 0 left record
// equals nodeCount + 15 (the last byte of the data section separator),
// exercising the upper boundary of the invalid range.
func buildSeparatorRecordMaxLeftDB() []byte {
const nodeCount = 1
const validRecord = nodeCount + dataSeparatorSize
return buildSeparatorRecordDB(nodeCount+dataSeparatorSize-1, validRecord)
}

// buildEmptyArrayLastInMetadataDB creates a valid MMDB where the metadata
// map's last field is "languages" (an empty array []). Tests the array
// validation path of the same off-by-one bug.
Expand Down
Loading