tanishqgandhi1908 commented on code in PR #7937:
URL: https://github.com/apache/texera/pull/7937#discussion_r3865799531


##########
file-service/src/main/scala/org/apache/texera/service/util/CoverImageUtils.scala:
##########
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.texera.service.util
+
+import jakarta.ws.rs.BadRequestException
+import org.apache.commons.io.FilenameUtils
+import org.apache.texera.amber.core.storage.model.OnVersionedFileResource
+import org.apache.texera.amber.core.storage.util.LakeFSStorageClient
+import org.apache.texera.amber.core.storage.{DocumentFactory, FileResolver, 
ResourceType}
+import org.apache.texera.service.resource.ResourceNaming
+import 
org.apache.texera.service.util.LakeFSExceptionHandler.withLakeFSErrorHandling
+
+/**
+  * Resource-agnostic halves of the cover-image endpoints, shared by datasets 
and models.
+  * The extension allowlist is a security control, not a convention: a cover 
is handed to
+  * the browser as a presigned URL, so a duplicated allowlist that drifts is a 
real risk.
+  * Access checks, the DAO update and the Response shape stay in each resource.
+  */
+object CoverImageUtils {
+
+  val SIZE_LIMIT_BYTES: Long = 10 * 1024 * 1024 // 10 MB
+
+  /** cover_image is varchar(255) on `model`; `dataset` passes its own 
narrower limit. */
+  val MAX_PATH_LENGTH: Int = 255
+
+  private val ALLOWED_EXTENSIONS: Set[String] = Set(".jpg", ".jpeg", ".png", 
".gif", ".webp")
+
+  /** Normalizes a cover path relative to the resource root and enforces the 
image allowlist. */
+  def validatePathOrThrow(coverImage: String, maxPathLength: Int): String = {
+    if (coverImage == null || coverImage.trim.isEmpty) {
+      throw new BadRequestException("Cover image path is required")
+    }
+
+    val normalized = 
ResourceNaming.validateAndNormalizeFilePathOrThrow(coverImage)
+
+    val extension = FilenameUtils.getExtension(normalized)
+    if (extension == null || 
!ALLOWED_EXTENSIONS.contains(s".$extension".toLowerCase)) {
+      throw new BadRequestException("Invalid file type")
+    }
+
+    // Guard the column width here so an over-long path is a 400, not a 
jOOQ-wrapped 500.
+    if (normalized.length > maxPathLength) {
+      throw new BadRequestException(s"Cover image path must be at most 
$maxPathLength characters")
+    }
+    normalized
+  }
+
+  /**
+    * Opens the committed image the cover path points at, under the given 
resource root.
+    * The resource type is a parameter so this never learns about datasets or 
models.
+    */
+  def openCover(
+      resourceType: ResourceType.Value,
+      ownerEmail: String,
+      resourceName: String,
+      normalized: String
+  ): OnVersionedFileResource =
+    DocumentFactory
+      .openReadonlyDocument(
+        
FileResolver.resolve(s"$resourceType/$ownerEmail/$resourceName/$normalized")

Review Comment:
   Confirmed, and the read path was the worse half — thanks for spotting that. 
Fixed both ends:
   
   validatePathOrThrow now requires at least <version>/<file>, so a bare 
cover.jpg is a 400 naming the expected shape instead of reaching FileResolver.
   openCover returns Option and catches vfs2.FileNotFoundException. The write 
path uses openCoverOrBadRequest → 400; the reads treat unresolvable as absent, 
so cover-url returns {"url": null} and cover returns 404. A stale cover now 
renders the default rather than 500ing on every card.
   
   Worth noting the dataset 500 is pre-existing — DatasetResource had the same 
unguarded FileResolver.resolve before this PR. Since the code is now shared, 
the one fix closes both, which is a point in the shared util's favour.
   
   Tests added for the bare filename, a path pointing at a version that never 
existed, and a stored cover that stops resolving. DatasetResourceSpec's 152 
tests still pass untouched.



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