Skip to content

AVRO-4311: [java] Escape backslashes in generated Javadoc#3880

Open
iemejia wants to merge 2 commits into
apache:mainfrom
iemejia:AVRO-4311-java-unicode-escape
Open

AVRO-4311: [java] Escape backslashes in generated Javadoc#3880
iemejia wants to merge 2 commits into
apache:mainfrom
iemejia:AVRO-4311-java-unicode-escape

Conversation

@iemejia

@iemejia iemejia commented Jul 19, 2026

Copy link
Copy Markdown
Member

What is the purpose of the change

The Java code generator (avro-compiler / avro-maven-plugin) writes schema
documentation strings into the Javadoc comments of generated Java sources.
The escapeForJavadoc helper escapes the comment terminator (*/) and HTML
metacharacters, but it leaves backslashes untouched.

The Java compiler translates Unicode escapes (\uXXXX) across the whole source
file, including inside comments, before comments are recognized (JLS 3.3). As a
result, a documentation string containing backslash sequences can be
reinterpreted by the compiler and change the generated source in unintended
ways.

This change updates escapeForJavadoc to also neutralize backslashes by
encoding them as the HTML entity \, so documentation content is always
emitted as inert text in the generated Javadoc. The string-literal path
(escapeForJavaString, used for the embedded schema) already doubles
backslashes and is unaffected.

Fixes AVRO-4311.

Verifying this change

This change added tests and can be verified as follows:

  • Added TestSpecificCompiler#unicodeEscapesInDocsAreNeutralized, which checks
    both the escapeForJavadoc helper and full code generation against several
    escape forms (multiple us, backslash-producing escapes, even-length
    backslash runs, uppercase-hex decoys, and a literal */), asserting that no
    backslash reaches the generated Javadoc comments.
  • The existing escaping test (docsAreEscaped_avro4053) continues to pass.
  • Full avro-compiler module test suite is green (mvn -pl compiler test) and
    Spotless formatting passes.

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? not applicable

The code generator writes schema documentation strings into the
Javadoc comments of generated Java sources. escapeForJavadoc escaped
the comment terminator and HTML metacharacters but left backslashes
untouched.

The Java compiler translates Unicode escapes (\uXXXX) across the whole
source file, including inside comments, before comments are recognized
(JLS 3.3). A documentation string containing backslash sequences could
therefore be reinterpreted by the compiler and change the generated
source in unintended ways.

Neutralize backslashes in escapeForJavadoc by encoding them as the
HTML entity \, so documentation content is always emitted as inert
text. Add a regression test covering several escape forms. The
string-literal path (escapeForJavaString) already doubles backslashes
and is unaffected.
@github-actions github-actions Bot added the Java Pull Requests for Java binding label Jul 19, 2026
@iemejia
iemejia requested a review from Copilot July 19, 2026 15:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens the Java specific-code generator’s Javadoc emission by ensuring schema documentation cannot be reinterpreted by javac as Unicode escapes (JLS 3.3), which could otherwise terminate comments early and enable code injection in generated sources.

Changes:

  • Update SpecificCompiler.escapeForJavadoc to neutralize backslashes by emitting &#92; (in addition to existing */ and </> escaping).
  • Add a new regression test that exercises multiple Unicode-escape variants and verifies no raw backslash reaches generated Javadoc comments.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java Extends Javadoc-escaping helper to neutralize backslashes and documents the rationale (Unicode-escape translation before comment recognition).
lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java Adds an end-to-end regression test covering multiple malicious doc patterns to ensure generated Javadoc contains no raw backslashes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Address review feedback: the end-to-end assertion only inspected lines
starting with "/**" or "*", so the middle physical lines of a
multi-line Javadoc block (a doc containing newlines) were not checked.

Replace the line-based scan with one that removes all Java string
literals (the embedded schema, which legitimately contains doubled
backslashes) and then asserts no backslash remains in the surrounding
code and comments. Add a doc vector that spans multiple physical lines.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@iemejia
iemejia requested a review from RyanSkraba July 20, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants