Skip to content

Commit 295d92a

Browse files
committed
GROOVY-12277: Encode source-derived text emitted into generated HTML
Two places where groovydoc builds HTML around text taken from the source it is documenting, without encoding it for the context it lands in. A {@link} or @see reference is split into a target and a label, both of which are then concatenated into an anchor the tool constructs: the target into href, the label into the element text, and for a resolved class the short name into title as well. None was encoded, so a reference could close the attribute and open a tag of its own. Encode each for its context, the attributes through encodeAttribute and the text through encodeAngleBrackets. An annotation's name and description are emitted into the class declaration and every member heading. description() carries the annotation's arguments as they were written, so an annotation holding a string literal put that literal into the page verbatim. Encode both, leaving the linkable() call alone since that one does produce markup. This is groovydoc's own construction rather than the raw HTML a doc comment body may contain by javadoc parity, so the passthrough that covers a comment body does not extend to it. For the annotation case the text is not from a comment at all: it is source code, and reaches the page without any doc comment being written. Not changed: the @default tag GroovydocJavaVisitor appends for an annotation member's default value. It is added to the raw comment text and no template renders it as a declaration, and constantValueExpression() is only tested for nullity, so that value does not reach a declaration block.
1 parent 55dcfb9 commit 295d92a

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

‎subprojects/groovy-groovydoc/src/main/java/org/codehaus/groovy/tools/groovydoc/SimpleGroovyClassDoc.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,10 @@ else if (ch == ',' && nested == 0) {
658658
}
659659

660660
if (type.startsWith("#"))
661-
return "<a href='" + resolveMethodArgs(rootDoc, classDoc, type) + "'>" + (label == null ? type.substring(1) : label) + "</a>";
661+
// The target and label both come from the doc comment, so they are encoded for the
662+
// context each lands in: the href is an attribute value, the label is element text.
663+
return "<a href='" + encodeAttribute(resolveMethodArgs(rootDoc, classDoc, type)) + "'>"
664+
+ encodeAngleBrackets(label == null ? type.substring(1) : label) + "</a>";
662665

663666
if (type.endsWith("[]")) {
664667
String componentType = type.substring(0, type.length() - 2);
@@ -738,7 +741,8 @@ private static String buildUrl(String relativeRoot, String[] target, String shor
738741
? target[0].replace('$', '.')
739742
: target[0].replace('.', '/').replace('$', '.');
740743
String url = relativeRoot + targetPath + ".html" + (target.length > 1 ? "#" + target[1] : "");
741-
return "<a href='" + url + "' title='" + shortClassName + "'>" + shortClassName + "</a>";
744+
return "<a href='" + encodeAttribute(url) + "' title='" + encodeAttribute(shortClassName) + "'>"
745+
+ encodeAngleBrackets(shortClassName) + "</a>";
742746
}
743747

744748
private GroovyClassDoc resolveClass(GroovyRootDoc rootDoc, String name) {

‎subprojects/groovy-groovydoc/src/main/resources/org/codehaus/groovy/tools/groovydoc/gstringTemplates/classLevel/classDocName.html‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
(t.isStatic()?"static&nbsp;":"")
7373
}
7474
def annotations = { t, sepChar ->
75-
t.annotations() ? t.annotations().collect{ it.isTypeAvailable()?'@'+linkable(it.type().typeName())+it.description():'@'+it.name()+it.description()}.join(sepChar) + sepChar : ''
75+
// An annotation's name and description are source text, not markup: description()
76+
// re-embeds the annotation's arguments verbatim. Only linkable() produces HTML here.
77+
def encode = { org.codehaus.groovy.tools.groovydoc.SimpleGroovyClassDoc.encodeAngleBrackets(it ?: '') }
78+
t.annotations() ? t.annotations().collect{ it.isTypeAvailable()?'@'+linkable(it.type().typeName())+encode(it.description()):'@'+encode(it.name())+encode(it.description())}.join(sepChar) + sepChar : ''
7679
}
7780
def elementTypes = [
7881
"required":"true",

‎subprojects/groovy-groovydoc/src/test/groovy/org/codehaus/groovy/tools/groovydoc/GroovyDocToolTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,52 @@ private String renderSingle(Path sourcePath, String pkg, String simpleName) thro
326326
return output.getText(MOCK_DIR + "/" + pkg + "/" + simpleName + ".html");
327327
}
328328

329+
// GROOVY-12277: a {@link} label and target are doc-comment text that groovydoc puts into
330+
// the href and title of an anchor it builds itself, so they must be encoded for those
331+
// contexts. This is groovydoc's own construction, not the documented raw-HTML passthrough
332+
// of a comment body.
333+
public void testLinkTagCannotBreakOutOfTheAnchorItBuilds() throws Exception {
334+
String pkg = "org/codehaus/groovy/tools/groovydoc/testfiles/docfiles";
335+
Path tmp = Files.createTempDirectory("linktag-");
336+
Path pkgDir = tmp.resolve(pkg);
337+
Files.createDirectories(pkgDir);
338+
Files.writeString(pkgDir.resolve("Helper.groovy"),
339+
"package " + pkg.replace('/', '.') + "\nclass Helper { void go() {} }\n");
340+
Files.writeString(pkgDir.resolve("LinkTag.groovy"),
341+
"package " + pkg.replace('/', '.') + "\n" +
342+
"/**\n" +
343+
" * See {@link Helper x'&gt;&lt;img src=q onerror='alert(1)}\n" +
344+
" * and {@link #go(a' onmouseover='alert(2)) L}\n" +
345+
" */\n" +
346+
"class LinkTag {}\n");
347+
348+
String doc = renderSingle(tmp, pkg, "LinkTag");
349+
assertNotNull(doc);
350+
assertFalse("a link label escaped its attribute in:\n" + doc, doc.contains("<img src=q"));
351+
assertFalse("a link target injected an event handler in:\n" + doc,
352+
doc.contains("onmouseover='alert"));
353+
}
354+
355+
// GROOVY-12277: an annotation's name and description are source text re-embedded verbatim
356+
// into the declaration block, so unlike a doc comment they carry no passthrough licence.
357+
public void testAnnotationTextIsEncodedInDeclarations() throws Exception {
358+
String pkg = "org/codehaus/groovy/tools/groovydoc/testfiles/docfiles";
359+
Path tmp = Files.createTempDirectory("annotation-");
360+
Path pkgDir = tmp.resolve(pkg);
361+
Files.createDirectories(pkgDir);
362+
Files.writeString(pkgDir.resolve("Meta.groovy"),
363+
"package " + pkg.replace('/', '.') + "\n" +
364+
"@interface Meta { String value() }\n");
365+
Files.writeString(pkgDir.resolve("Annotated.groovy"),
366+
"package " + pkg.replace('/', '.') + "\n" +
367+
"@Meta('<img src=q onerror=alert(1)>')\n" +
368+
"class Annotated {}\n");
369+
370+
String doc = renderSingle(tmp, pkg, "Annotated");
371+
assertNotNull(doc);
372+
assertFalse("annotation text was emitted as markup in:\n" + doc, doc.contains("<img src=q"));
373+
}
374+
329375
// Auto-strip opt-out: {@snippet file="X" keepHeader=true} preserves the
330376
// file content verbatim, and lang is inferred from the file's extension
331377
// when no explicit lang= is given.

0 commit comments

Comments
 (0)