Skip to content

Commit e768943

Browse files
Escape double quotes in sp_LogHunter @custom_message
The #search.command computed column builds an xp_readerrorlog call with the search string wrapped in double quotes: EXECUTE master.dbo.xp_readerrorlog [log], 1, "<search>", " ", ... @custom_message is concatenated straight into the "<search>" slot without escaping, so a literal " in the user-supplied message closes the first argument early. The rest of the @custom_message gets parsed as T-SQL and sp_executesql raises "Incorrect syntax near '+'" or a similar message. Verified by calling with @custom_message = N'hello"+injection'. Doubled the quote on the way in: REPLACE(@custom_message, N'"', N'""'). xp_readerrorlog accepts the doubled quote as a literal inside the search string. Normal strings (no quotes) behave the same as before. Verified against the QsCleanupTest disk-full message lookup with @custom_message = N'Microsoft'. Note: this is not a privilege-escalation vector — xp_readerrorlog already requires securityadmin-level access — but cryptic syntax errors from a valid-looking input are a real operator footgun. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ca2dbd8 commit e768943

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

sp_LogHunter/sp_LogHunter.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,13 @@ BEGIN
491491
(
492492
VALUES
493493
(
494-
N'"' + @custom_message + '"',
494+
/* xp_readerrorlog search strings are wrapped in double quotes
495+
(see the #search.command computed column), so any literal "
496+
inside the user-supplied @custom_message must be doubled to
497+
avoid closing the argument early and producing an
498+
"Incorrect syntax near '+'" error when sp_executesql parses
499+
the generated batch. */
500+
N'"' + REPLACE(@custom_message, N'"', N'""') + N'"',
495501
N'"' + CONVERT(nvarchar(10), DATEADD(DAY, @days_back, SYSDATETIME()), 112) + N'"',
496502
N'"' + CONVERT(nvarchar(30), @start_date) + N'"',
497503
N'"' + CONVERT(nvarchar(30), @end_date) + N'"'

0 commit comments

Comments
 (0)