aicam commented on code in PR #7937:
URL: https://github.com/apache/texera/pull/7937#discussion_r3865579236
##########
file-service/src/main/scala/org/apache/texera/service/resource/ModelResource.scala:
##########
@@ -339,6 +359,54 @@ class ModelResource extends LazyLogging {
}
}
+ @POST
+ @Consumes(Array(MediaType.APPLICATION_JSON))
+ @Produces(Array(MediaType.APPLICATION_JSON))
+ @RolesAllowed(Array("REGULAR", "ADMIN"))
+ @Path("/update/framework")
+ def updateModelFramework(
+ modificator: ModelFrameworkModification,
+ @Auth sessionUser: SessionUser
+ ): Response = {
+ withTransaction(context) { ctx =>
+ val modelDao = new ModelDao(ctx.configuration())
+ val model = getModelByID(ctx, modificator.mid)
+ if (!userHasWriteAccess(ctx, modificator.mid, sessionUser.getUid)) {
+ throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_MODEL_MESSAGE)
+ }
+
+ validateLabel("framework", modificator.framework, SUPPORTED_FRAMEWORKS)
Review Comment:
This validates more strictly than `createModel` does for the same field, in
two ways:
- **Blank/omitted:** create falls back to `DEFAULT_FRAMEWORK` when the value
is null or blank, but here `validateLabel` runs unconditionally, so a null
produces `400 Unsupported framework 'null'.` — a literal `null` rendered into
the user's error toast.
- **Whitespace:** create normalizes with
`Option(request.framework).map(_.trim).filter(_.nonEmpty)`;
`modificator.framework` is passed through raw, so `"onnx "` is accepted at
creation and rejected on update.
Either way it reads to the user as "the value the create form accepted, the
edit form won't." Applying the same `trim`/`filter(_.nonEmpty)`/default
treatment here would make the two paths symmetric.
##########
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:
`FileResolver.resolve` requires at least 5 path segments
(`parsePrefixedPath`), but `validatePathOrThrow` never checks the segment
count. So a cover path that isn't `<version>/<file>` — e.g. `{"coverImage":
"cover.jpg"}`, which `CoverImageRequest`'s own scaladoc warns against but
nothing enforces — builds a 4-segment path here and throws
`org.apache.commons.vfs2.FileNotFoundException`. A version name that doesn't
exist throws the same type from `lookupModel`.
That's an `IOException`, not a `WebApplicationException`, and file-service
registers no mapper for it (only `UnauthorizedExceptionMapper` in
`AuthFeatures`). Dropwizard's default `LoggingExceptionMapper` therefore
returns a generic 500 whose body is `"There was an error processing your
request. It has been logged (ID ...)"`. A client following the existing dataset
pattern (`err.error?.message || "Failed to set cover image"`) shows that string
to the user, so the opaque message actually beats the component's own fallback.
Worth noting this isn't only the write path: `getModelCover` and
`getModelCoverUrl` also call `openCover` on the stored path, so a cover that
stops resolving is a 500 on every render of every card showing that model, not
a single bad request.
This is the same class of failure the `maxPathLength` guard just above was
added for ("so an over-long path is a 400, not a jOOQ-wrapped 500"), and rather
more likely to be hit. A segment-count check in `validatePathOrThrow`, or
catching `FileNotFoundException` here and rethrowing
`BadRequestException`/`NotFoundException`, would close it. The dataset twin
(`DatasetResource.scala`, cover update) has the same gap.
##########
file-service/src/main/scala/org/apache/texera/service/resource/ModelResource.scala:
##########
@@ -339,6 +359,54 @@ class ModelResource extends LazyLogging {
}
}
+ @POST
+ @Consumes(Array(MediaType.APPLICATION_JSON))
+ @Produces(Array(MediaType.APPLICATION_JSON))
+ @RolesAllowed(Array("REGULAR", "ADMIN"))
+ @Path("/update/framework")
+ def updateModelFramework(
+ modificator: ModelFrameworkModification,
+ @Auth sessionUser: SessionUser
+ ): Response = {
+ withTransaction(context) { ctx =>
+ val modelDao = new ModelDao(ctx.configuration())
+ val model = getModelByID(ctx, modificator.mid)
+ if (!userHasWriteAccess(ctx, modificator.mid, sessionUser.getUid)) {
+ throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_MODEL_MESSAGE)
+ }
+
+ validateLabel("framework", modificator.framework, SUPPORTED_FRAMEWORKS)
+
+ model.setFramework(modificator.framework)
+ modelDao.update(model)
+ Response.ok().build()
+ }
+ }
+
+ @POST
+ @Consumes(Array(MediaType.APPLICATION_JSON))
+ @Produces(Array(MediaType.APPLICATION_JSON))
+ @RolesAllowed(Array("REGULAR", "ADMIN"))
+ @Path("/update/format")
+ def updateModelFormat(
+ modificator: ModelFormatModification,
+ @Auth sessionUser: SessionUser
+ ): Response = {
+ withTransaction(context) { ctx =>
+ val modelDao = new ModelDao(ctx.configuration())
+ val model = getModelByID(ctx, modificator.mid)
+ if (!userHasWriteAccess(ctx, modificator.mid, sessionUser.getUid)) {
+ throw new ForbiddenException(ERR_USER_HAS_NO_ACCESS_TO_MODEL_MESSAGE)
+ }
+
+ validateLabel("format", modificator.format, SUPPORTED_FORMATS)
Review Comment:
Same asymmetry as the framework endpoint, plus a state that can't be
reached: `createModel` treats an absent or blank format as legitimate and
stores `null` (`model.setFormat(format.orNull)`, asserted by
`ModelApiForUiSpec`'s `newModel(format = null)` case), but `validateLabel` here
runs unconditionally, so `null`/`""` is a 400 and there is no way back to the
unset state a model can legally be created in.
Given the stated motivation that a mislabelled model shouldn't have to be
recreated, a model wrongly tagged `pickle` that should carry no format at all
still does. Accepting a null/blank value and storing `null` — mirroring create
— would close both this and the trim difference.
--
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]