Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions core/src/main/scala/org/apache/spark/ui/UIUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.ui

import java.{util => ju}
import java.lang.{Long => JLong}
import java.net.{URLDecoder, URLEncoder}
import java.net.{URI, URISyntaxException, URLDecoder, URLEncoder}
import java.nio.charset.StandardCharsets.UTF_8
import java.time.{Instant, ZoneId}
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -787,8 +787,22 @@ private[spark] object UIUtils extends Logging {
if (proxy) {
val proxyPrefix = sys.props.getOrElse("spark.ui.proxyBase", "")
proxyPrefix + "/proxy/" + id
} else {
} else if (isSafeHref(origHref)) {
origHref
} else {
// The href may come from an external registrant (e.g. an application's appUiUrl or
// a worker's webUiAddress); render anything unrecognized as a dead link.
"#"
}
}

/** Returns true if the given href is relative or uses the http(s) scheme. */
private def isSafeHref(href: String): Boolean = href != null && {
try {
val scheme = new URI(href).getScheme
scheme == null || scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")
} catch {
case _: URISyntaxException => false
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/src/test/scala/org/apache/spark/ui/UIUtilsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,14 @@ class UIUtilsSuite extends SparkFunSuite {
assert(html.contains("data-toggle-details"), "detailsUINode should use data-toggle-details")
assert(html.contains("stacktrace-details"), "detailsUINode should contain stacktrace-details")
}

test("makeHref only renders http(s) or relative URLs as hyperlinks") {
// appUiUrl and webUiAddress come from external registrants.
assert(UIUtils.makeHref(proxy = false, "app-1", "http://host:4040") === "http://host:4040")
assert(UIUtils.makeHref(proxy = false, "app-1", "https://host:4040") === "https://host:4040")
assert(UIUtils.makeHref(proxy = false, "app-1", "/relative/path") === "/relative/path")
assert(UIUtils.makeHref(proxy = false, "app-1", null) === "#")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The null input short-circuits at href != null before the URI parse, so this test never exercises the two branches that implement the PR's core behavior: a non-null value with a disallowed scheme, and a value that trips URISyntaxException. Since the test is named "only renders http(s) or relative URLs as hyperlinks", consider adding assertions for the reject path, e.g.:

assert(UIUtils.makeHref(proxy = false, "app-1", "javascript:alert(1)") === "#")
assert(UIUtils.makeHref(proxy = false, "app-1", "ht tp://bad url") === "#") // malformed -> URISyntaxException

Non-blocking.

// Reverse-proxy mode is unaffected: the href is generated by the master itself.
assert(UIUtils.makeHref(proxy = true, "app-1", "badlink").endsWith("/proxy/app-1"))
}
}