Repository navigation
Add a PatternLayout conversion word that encodes special characters - #853
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
PatternLayout output a value exactly as it was, so a message, NDC or
MDC value, thread name or logger name containing a line break was
written as several lines, splitting or forging events for anything that
reads the log one line per event. A pattern that embedded a value in
HTML, XML or a JSON string produced invalid output when the value
contained a markup character, a quote or a control character. The
Apache Logging security FAQ recommends sanitizing every Pattern layout
field in that situation, with %enc{%m}{CRLF} as the Log4j Core example,
but Log4cxx had no equivalent.
Add EncodingPatternConverter, registered in PatternLayout as "enc" and
"encode" (the Log4j 2 names). %enc{pattern}{format} formats the nested
pattern, applying the format modifiers of each nested converter, and
replaces the characters that are special in the selected format:
- HTML (the default, as in Log4j 2): CR and LF become \r and \n, and
&<>"'/ become HTML character entity references
- XML: &<>"', tab, CR and LF become XML entity or character references
(Transform::appendEscapingAttribute)
- JSON: the characters not allowed in a JSON string are escaped as
JSONLayout does (JSONLayout::appendItem without the enclosing quotes)
- CRLF: CR and LF become \r and \n
The format name is case insensitive and may be surrounded by spaces. An
unknown name, or %enc without a pattern, is reported in the internal
debugging output; HTML is used for the former and nothing is output for
the latter. The HTML and CRLF replacements append each run of ordinary
characters in one operation instead of character by character. A format
modifier on %enc applies to the encoded text.
A nested pattern needs options that contain braces, as in
%enc{%d{HH:mm:ss} %m}{CRLF}, but PatternParser ended every option at the
first closing brace. An option now ends at its matching closing brace.
When an option has no matching closing brace it still ends at the first
one, so patterns that were valid before are parsed as before.
Document the conversion word and nested braces in the PatternLayout
reference and add a change log entry.
PatternLayoutTest has tests for each format, the default format, the
encode alias, case insensitive format names, nested patterns with
options and format modifiers, format modifiers on %enc itself, nesting
%enc in %enc, and invalid options. PatternParserTestCase checks that
nested options are extracted, that an option without a matching closing
brace still ends at the first closing brace, and that the converter
reports a throwable conversion in its nested pattern.
Fixes apache#852
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 #852
Problem
PatternLayoutoutputs every value as it is. A message, NDC or MDC value, thread name or logger name that contains a line break is written as several lines, which splits or forges events for anything that reads the log one line per event. A pattern that embeds a value in HTML, XML or a JSON string produces invalid output when the value contains<,&,"or a control character.The Apache Logging security FAQ says that when Pattern layout must be used, all fields should be sanitized explicitly, with
%enc{%m}{CRLF}in Log4j Core as the example. Log4cxx had no equivalent. It also could not be added as an ordinary conversion word, becausePatternParserended every option at the first}, so%enc{%d{HH:mm:ss} %m}{CRLF}was split inside%d{HH:mm:ss}.Change
Nested options.
PatternParser::extractOptionsnow ends an option at its matching closing brace, so an option can contain a nested pattern with options of its own. If an option has no matching closing brace, it still ends at the first}, so every pattern that worked before is parsed the same way.%enc/%encode. A newEncodingPatternConverteris registered inPatternLayoutunder the Log4j 2 names.%enc{pattern}{format}formats the nested pattern (the nested converters keep their own format modifiers) and then replaces the characters that are special in the selected format:HTML(default, as in Log4j 2)\rand\n;&<>"'/with HTML character entity referencesXML&<>"', tab, CR and LF with entity or character references (Transform::appendEscapingAttribute)JSONJSONLayoutescapes (JSONLayout::appendItemwithout the enclosing quotes)CRLF\rand\nPatternLayoutconversion word. The rules come from the virtualgetFormatSpecifiers(), so conversion words added by a subclass work too. A format modifier on%encitself applies to the encoded text, e.g.%.-200enc{%m}{CRLF}.%encwith no pattern gives a warning and outputs nothing.handlesThrowable()returns true when the nested pattern contains a converter that handles throwables.Documentation: an
enc/encoderow in thePatternLayoutconversion table, a paragraph saying that options may contain balanced braces, theEncodingPatternConverterclass reference, and a change log entry under the 2.0.0 new features.Testing
New
PatternLayoutTesttests:testEncodeCRLF: CR and LF are replaced, theencodealias and a lower case name with spaces work, and other characters are left alonetestEncodeHTML: the default format and explicitHTML/htmltestEncodeXML,testEncodeJSON: the replaced characters, including a control character, and a JSON document built in the patterntestEncodeNestedPattern: a nested pattern with options, format modifiers, literal braces and an MDC value; minimum width and truncate-from-end modifiers on%enc;%encnested in%enctestEncodeInvalidOptions: no pattern, an empty pattern and an unknown format nameNew
PatternParserTestCasetests:testNestedOptions: nested options are extracted, and an option without a matching closing brace still ends at the first}testEncodeHandlesThrowableWith the
extractOptionschange reverted,testNestedOptionsandtestEncodeNestedPatternfail.On macOS (Apple clang 21, arm64),
patternlayouttest,patternparsertestcaseandfilenamepatterntestcasepass with the defaultutf-8logchar and with-DLOG4CXX_CHAR=wchar_t. The changed library files compile with-DCMAKE_CXX_STANDARD=11and produce no new warnings with-Wall -Wextra -Wpedantic -Wshadow -Wconversion.Deep nesting is bounded by the pattern length. A 700-level
%enc{%enc{...}}pattern (4.2 KB, more than the 4096-byte pattern limit ofPatternLayoutApiFuzzer) parses and formats in a thread with an 8 MB stack. In a Debug build each nesting level uses about 2–3 KB of stack.