Query Logs (PostgreSQL): strip null bytes from answer field before insert#2049
Open
daedaevibin wants to merge 4 commits into
Open
Query Logs (PostgreSQL): strip null bytes from answer field before insert#2049daedaevibin wants to merge 4 commits into
daedaevibin wants to merge 4 commits into
Conversation
DNS query data can contain null bytes (0x00) from malformed or adversarial
packets. PostgreSQL rejects these in text/varchar columns, causing batch
insert failures with Npgsql.PostgresException (22021).
Sanitize by calling .Replace("\0", "") on qname and answer values in
BulkInsertLogsAsync before passing them as Npgsql parameters.
…query log apps Same fix applied to the remaining database logger apps for consistency. All four apps (PostgreSQL, MySQL, SQL Server, SQLite) now sanitize null bytes from qname and answer fields before insertion.
… SQLite query log apps" This reverts commit 17f25e3.
QNAME cannot contain null bytes per DNS wire format. Only the answer field (from TXT record RDATA) is affected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2048
Problem
The Query Logs (PostgreSQL) app throws
Npgsql.PostgresException (22021)when bulk-inserting DNS query logs containing null bytes (0x00) in theanswerfield. PostgreSQL rejects these as invalid UTF-8, causing the entire batch insert to fail and query logs to be lost.Reproduction
Query it through the DNS server:
dig @127.0.0.1 null.test.local TXTThe query log app throws:
Root Cause
DnsTXTRecordData.ToZoneFileEntry()callsEncoding.UTF8.GetString()on raw TXT record bytes. When the wire data contains0x00, this produces a C# string with an embedded null character. Npgsql passes it to PostgreSQL which rejects it.QNAME is safe — the DNS wire format prevents null bytes in domain names. Only the
answerfield (built fromrecord.RDATA.ToString()) is affected.Fix
Strip null bytes from the
answerfield inBulkInsertLogsAsyncbefore passing it as a Npgsql parameter:Verified
Note: I tested this against a live v15.4 instance. The null byte only appeared in the answer field during reproduction; other fields were clean.