kunwp1 commented on code in PR #9214:
URL: https://github.com/apache/texera/pull/9214#discussion_r4670536555


##########
amber/src/main/scala/org/apache/texera/web/resource/pythonvirtualenvironment/PveWebsocketResource.scala:
##########
@@ -19,18 +19,42 @@
 
 package org.apache.texera.web.resource.pythonvirtualenvironment
 
+import com.fasterxml.jackson.core.JsonProcessingException
+import com.fasterxml.jackson.core.`type`.TypeReference
+import org.apache.texera.amber.util.JSONUtils.objectMapper
+
 import javax.websocket._
 import javax.websocket.server.ServerEndpoint
 import java.util.concurrent.LinkedBlockingQueue
 import scala.concurrent.Future
 import scala.concurrent.ExecutionContext.Implicits.global
+import scala.jdk.CollectionConverters._
 
 /**
   *  WebSocket endpoint for PVE creation and user package installation that 
streams
   *  pip installation logs  to the frontend in real time. The environment 
setup runs
   *  asynchronously, and output is pushed to the client until completion.
   */
 
+object PveWebsocketResource {

Review Comment:
   The endpoint's scaladoc above now documents this object instead of the 
class. Moving the object above it, or the comment down to `@ServerEndpoint`, 
keeps it on the class.



##########
amber/src/main/scala/org/apache/texera/web/resource/pythonvirtualenvironment/PveWebsocketResource.scala:
##########
@@ -19,18 +19,42 @@
 
 package org.apache.texera.web.resource.pythonvirtualenvironment
 
+import com.fasterxml.jackson.core.JsonProcessingException
+import com.fasterxml.jackson.core.`type`.TypeReference
+import org.apache.texera.amber.util.JSONUtils.objectMapper
+
 import javax.websocket._
 import javax.websocket.server.ServerEndpoint
 import java.util.concurrent.LinkedBlockingQueue
 import scala.concurrent.Future
 import scala.concurrent.ExecutionContext.Implicits.global
+import scala.jdk.CollectionConverters._
 
 /**
   *  WebSocket endpoint for PVE creation and user package installation that 
streams
   *  pip installation logs  to the frontend in real time. The environment 
setup runs
   *  asynchronously, and output is pushed to the client until completion.
   */
 
+object PveWebsocketResource {
+
+  private val packagesType = new TypeReference[java.util.List[String]] {}
+
+  /**
+    * Decodes the `packages` query parameter, a JSON array of pip requirement 
strings, into a
+    * trimmed list without blank entries. Anything that is not a JSON array of 
strings is rejected.
+    */
+  private[pythonvirtualenvironment] def parsePackages(json: String): 
List[String] = {
+    val decoded =
+      try objectMapper.readValue(json, packagesType)
+      catch {
+        case e: JsonProcessingException =>
+          throw new IllegalArgumentException(s"Invalid packages parameter: 
$json", e)
+      }
+    decoded.asScala.toList.filter(_ != null).map(_.trim).filter(_.nonEmpty)

Review Comment:
   Two inputs still get through, both only from hand-rolled requests:
   - `packages=null`: Jackson returns `null` for a JSON null, so 
`decoded.asScala` throws a `NullPointerException`, and the client gets its 
message instead of `Invalid packages parameter: null`.
   - Anything after the array is ignored, since the shared mapper leaves 
`FAIL_ON_TRAILING_TOKENS` off. `["numpy"]["scipy"]` and `["numpy"]x` both 
decode to `List(numpy)`.
   
   A reader built once fixes the second:
   ```scala
   private val packagesReader: ObjectReader = objectMapper
     .readerFor(new TypeReference[java.util.List[String]] {})
     .`with`(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
   ```
   and `if (decoded == null)` throwing the same `IllegalArgumentException` 
fixes the first. Numbers and booleans are still coerced (`[1, true]` gives 
`"1"`, `"true"`), but pip rejects those, so I'd leave that.



-- 
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]

Reply via email to