Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d9754f3

Browse files
committedJun 26, 2025
[SPARK-52381][Core] JsonProtocol: Only accept valid SparkListenerEvent subclasses
extra check scalastyle Update JsonProtocolSuite.scala
1 parent efb03b1 commit d9754f3

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed
 

‎core/src/main/scala/org/apache/spark/util/JsonProtocol.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,18 @@ private[spark] class JsonProtocol(sparkConf: SparkConf) extends JsonUtils {
919919
case `stageExecutorMetrics` => stageExecutorMetricsFromJson(json)
920920
case `blockUpdate` => blockUpdateFromJson(json)
921921
case `resourceProfileAdded` => resourceProfileAddedFromJson(json)
922-
case other => mapper.readValue(json.toString, Utils.classForName(other))
923-
.asInstanceOf[SparkListenerEvent]
922+
case other =>
923+
if (other.startsWith("org.apache.spark")) {
924+
val otherClass = Utils.classForName(other)
925+
if (classOf[SparkListenerEvent].isAssignableFrom(otherClass)) {
926+
mapper.readValue(json.toString, otherClass)
927+
.asInstanceOf[SparkListenerEvent]
928+
} else {
929+
throw new SparkException(s"Unknown event type: $other")
930+
}
931+
} else {
932+
throw new SparkException(s"Unknown event type: $other")
933+
}
924934
}
925935
}
926936

‎core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,37 @@ class jsonProtocolSuite extends SparkFunSuite {
10221022
"String value length (10000) exceeds the maximum allowed"
10231023
))
10241024
}
1025+
1026+
test("SPARK-52381: only read Spark classes") {
1027+
val unknownJson =
1028+
"""{
1029+
| "Event" : "com.example.UnknownEvent",
1030+
| "foo" : "foo"
1031+
|}""".stripMargin
1032+
try {
1033+
jsonProtocol.sparkEventFromJson(unknownJson)
1034+
fail("Expected SparkException for unknown event type")
1035+
} catch {
1036+
case e: SparkException =>
1037+
assert(e.getMessage.startsWith("Unknown event type"))
1038+
}
1039+
}
1040+
1041+
test("SPARK-52381: only read classes that extend SparkListenerEvent") {
1042+
val unknownJson =
1043+
"""{
1044+
| "Event" : "org.apache.spark.SparkException",
1045+
| "foo" : "foo"
1046+
|}""".stripMargin
1047+
try {
1048+
jsonProtocol.sparkEventFromJson(unknownJson)
1049+
fail("Expected SparkException for unknown event type")
1050+
} catch {
1051+
case e: SparkException =>
1052+
assert(e.getMessage.startsWith("Unknown event type"))
1053+
}
1054+
}
1055+
10251056
}
10261057

10271058

0 commit comments

Comments
 (0)
Please sign in to comment.