dongjoon-hyun commented on code in PR #56669:
URL: https://github.com/apache/spark/pull/56669#discussion_r3461199891


##########
core/src/main/scala/org/apache/spark/ui/HttpSecurityFilter.scala:
##########
@@ -51,13 +51,42 @@ private class HttpSecurityFilter(
 
     val cspNonce = CspNonce.generate()
     try {
-      if (conf.get(UI_CONTENT_SECURITY_POLICY_ENABLED)) {
-        hres.setHeader("Content-Security-Policy",
-          s"default-src 'self'; script-src 'self' 'nonce-$cspNonce'; " +
-          s"style-src 'self' 'unsafe-inline'; img-src 'self' data:; " +
-          s"object-src 'none'; base-uri 'self';")
+      val cspEnabled = conf.get(UI_CONTENT_SECURITY_POLICY_ENABLED)
+      val frameAncestorsEnabled = 
conf.get(UI_CONTENT_SECURITY_POLICY_FRAME_ANCESTORS_ENABLED)
+
+      if (cspEnabled || frameAncestorsEnabled) {
+        // Use CSP frame-ancestors as the primary clickjacking protection 
mechanism.
+        // X-Frame-Options ALLOW-FROM is deprecated and ignored by modern 
browsers
+        // (Chrome, Firefox, Edge, Safari), so frame-ancestors is used instead.
+        val frameAncestors = conf.get(UI_ALLOW_FRAMING_FROM)
+          .filterNot(v => v.equalsIgnoreCase("SAMEORIGIN"))
+          .map { uri =>
+            if (uri.equalsIgnoreCase("DENY")) {
+              "frame-ancestors 'none'"
+            } else {
+              // Sanitize the URI: truncate at semicolons to prevent CSP 
directive injection,
+              // and strip newlines to prevent header injection.
+              val sanitized = uri.replaceAll("[\\r\\n]+", "").split(";", 
2)(0).trim
+              if (sanitized.isEmpty) "frame-ancestors 'self'"
+              else s"frame-ancestors 'self' $sanitized"
+            }
+          }
+          .getOrElse("frame-ancestors 'self'")
+
+        if (cspEnabled) {
+          hres.setHeader("Content-Security-Policy",
+            s"default-src 'self'; script-src 'self' 'nonce-$cspNonce'; " +
+            s"style-src 'self' 'unsafe-inline'; img-src 'self' data:; " +
+            s"object-src 'none'; base-uri 'self'; $frameAncestors;")
+        } else {
+          hres.setHeader("Content-Security-Policy", s"$frameAncestors;")

Review Comment:
   Well, I don't think this is correct.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to