This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.2 by this push:
new 1e4d40c018ee [SPARK-58521][UI] Forward `X-Forwarded-Context` header
and skip Master CSP on reverse-proxied UI requests
1e4d40c018ee is described below
commit 1e4d40c018ee24a4c8a6720c4219344bbc3f8ed9
Author: Nflrijal <[email protected]>
AuthorDate: Mon Aug 3 10:27:29 2026 -0700
[SPARK-58521][UI] Forward `X-Forwarded-Context` header and skip Master CSP
on reverse-proxied UI requests
1. Overrode `addProxyHeaders` in `JettyUtils.createProxyHandler` to
automatically attach/chain the `X-Forwarded-Context` header to all proxied HTTP
requests.
2. Updated `HttpSecurityFilter` to skip setting Master's CSP nonce header
on proxied requests (`/proxy/*`), preventing browser CSP blocking of inline
scripts like Event Timeline.
3. Added unit test in `UISuite.scala`.
When `spark.ui.reverseProxy=true` is enabled in Standalone mode, navigating
UI tabs stripped the `/proxy/<id>` prefix. Additionally, Master's CSP header
conflicted with Driver's inline script nonces, blocking Event Timeline scripts.
Yes. Fixes navigation links and interactive UI elements (such as Event
Timeline) when viewing Application UI through Master's reverse proxy
(spark.ui.reverseProxy=true).
Added unit test in UISuite: SPARK-47232: createProxyHandler forwards
X-Forwarded-Context header.
Manually tested batch (SparkPi) and streaming (NetworkWordCount)
applications on a local Standalone cluster (Master, Worker, SparkSubmit in
cluster mode), verifying that all UI tabs and Event Timeline work cleanly.
No
Closes #57549 from Nflrijal/fix/SPARK-47232-driver-proxybase-not-propagated.
Lead-authored-by: Nflrijal <[email protected]>
Co-authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit 71cc5e54e109cc145fac3effc2ee9d59ac42ed54)
Signed-off-by: Dongjoon Hyun <[email protected]>
---
.../org/apache/spark/ui/HttpSecurityFilter.scala | 4 ++-
.../scala/org/apache/spark/ui/JettyUtils.scala | 21 ++++++++++++++-
.../test/scala/org/apache/spark/ui/UISuite.scala | 31 ++++++++++++++++++++++
3 files changed, 54 insertions(+), 2 deletions(-)
diff --git a/core/src/main/scala/org/apache/spark/ui/HttpSecurityFilter.scala
b/core/src/main/scala/org/apache/spark/ui/HttpSecurityFilter.scala
index d06f2162d2f6..5783b9fe1a64 100644
--- a/core/src/main/scala/org/apache/spark/ui/HttpSecurityFilter.scala
+++ b/core/src/main/scala/org/apache/spark/ui/HttpSecurityFilter.scala
@@ -49,9 +49,11 @@ private class HttpSecurityFilter(
val hres = res.asInstanceOf[HttpServletResponse]
hres.setHeader("Cache-Control", "no-cache, no-store, must-revalidate")
+ val isProxyRequest = hreq.getContextPath == "/proxy"
+
val cspNonce = CspNonce.generate()
try {
- if (conf.get(UI_CONTENT_SECURITY_POLICY_ENABLED)) {
+ if (conf.get(UI_CONTENT_SECURITY_POLICY_ENABLED) && !isProxyRequest) {
hres.setHeader("Content-Security-Policy",
s"default-src 'self'; script-src 'self' 'nonce-$cspNonce'; " +
s"style-src 'self' 'unsafe-inline'; img-src 'self' data:; " +
diff --git a/core/src/main/scala/org/apache/spark/ui/JettyUtils.scala
b/core/src/main/scala/org/apache/spark/ui/JettyUtils.scala
index 3c21fd6d2a00..164a3bccf2f3 100644
--- a/core/src/main/scala/org/apache/spark/ui/JettyUtils.scala
+++ b/core/src/main/scala/org/apache/spark/ui/JettyUtils.scala
@@ -27,7 +27,7 @@ import scala.xml.Node
import jakarta.servlet.{DispatcherType, Filter, FilterChain, ServletRequest,
ServletResponse}
import jakarta.servlet.http._
-import org.eclipse.jetty.client.{Response => CResponse}
+import org.eclipse.jetty.client.{Request => CRequest, Response => CResponse}
import org.eclipse.jetty.client.HttpClient
import org.eclipse.jetty.client.transport.HttpClientTransportOverHTTP
import org.eclipse.jetty.compression.server.CompressionHandler
@@ -209,6 +209,25 @@ private[spark] object JettyUtils extends Logging {
.orNull
}
+ override def addProxyHeaders(
+ clientRequest: HttpServletRequest,
+ proxyRequest: CRequest): Unit = {
+ super.addProxyHeaders(clientRequest, proxyRequest)
+ val path = clientRequest.getPathInfo
+ if (path != null) {
+ val prefixTrailingSlashIndex = path.indexOf('/', 1)
+ val prefix = if (prefixTrailingSlashIndex == -1) {
+ path
+ } else {
+ path.substring(0, prefixTrailingSlashIndex)
+ }
+ val existingContext =
Option(clientRequest.getHeader("X-Forwarded-Context")).getOrElse("")
+ val contextPath = Option(clientRequest.getContextPath).getOrElse("")
+ val proxyContext = existingContext + contextPath + prefix
+ proxyRequest.headers(headers => headers.put("X-Forwarded-Context",
proxyContext))
+ }
+ }
+
override def newHttpClient(): HttpClient = {
// SPARK-21176: Use the Jetty logic to calculate the number of
selector threads (#CPUs/2),
// but limit it to 8 max.
diff --git a/core/src/test/scala/org/apache/spark/ui/UISuite.scala
b/core/src/test/scala/org/apache/spark/ui/UISuite.scala
index e506c0a7ab8d..8aa90cec11c7 100644
--- a/core/src/test/scala/org/apache/spark/ui/UISuite.scala
+++ b/core/src/test/scala/org/apache/spark/ui/UISuite.scala
@@ -532,6 +532,37 @@ class UISuite extends SparkFunSuite {
}
+ test("SPARK-58521: createProxyHandler forwards X-Forwarded-Context header") {
+ val (conf, securityMgr, sslOptions) = sslDisabledConf()
+ val targetServer = JettyUtils.startJettyServer("0.0.0.0", 0, sslOptions,
conf)
+ val proxyServer = JettyUtils.startJettyServer("0.0.0.0", 0, sslOptions,
conf)
+
+ @volatile var forwardedContext: String = null
+ val targetHandler = new ServletContextHandler()
+ targetHandler.setContextPath("/")
+ targetHandler.addServlet(new ServletHolder(new HttpServlet {
+ override def doGet(req: HttpServletRequest, resp: HttpServletResponse):
Unit = {
+ forwardedContext = req.getHeader("X-Forwarded-Context")
+ resp.setStatus(HttpServletResponse.SC_OK)
+ }
+ }), "/*")
+ targetServer.addHandler(targetHandler, securityMgr)
+
+ val targetAddr = s"http://$localhost:${targetServer.boundPort}"
+ val proxyHandler = JettyUtils.createProxyHandler(_ => Some(targetAddr))
+ proxyServer.addHandler(proxyHandler, securityMgr)
+
+ try {
+ val proxyUrl =
s"http://$localhost:${proxyServer.boundPort}/proxy/app-123/stages/"
+ TestUtils.withHttpConnection(new URI(proxyUrl).toURL) { conn =>
+ assert(conn.getResponseCode === HttpServletResponse.SC_OK)
+ assert(forwardedContext === "/proxy/app-123")
+ }
+ } finally {
+ stopServer(proxyServer)
+ stopServer(targetServer)
+ }
+ }
}
// Filter for testing; returns a configurable code for every request.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]