This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-8647-0eab1a53d2814c2a7dfaa5425b4df01a7baf9017
in repository https://gitbox.apache.org/repos/asf/texera.git

commit a47032753b46d8b277c066e10624e64af5649442
Author: Eugene Gu <[email protected]>
AuthorDate: Sun Oct 25 06:27:52 2026 +0000

    fix(amber): compare dashboard workflow uids by value (#8647)
    
    ### What changes were proposed in this PR?
    
    `WorkflowResource.mapWorkflowEntries` sets `DashboardWorkflow.isOwner`
    by comparing the record's owner uid with the caller's uid. It did so
    with `getUid.eq(uid)`. Both operands are `java.lang.Integer`, and
    Scala's `eq` is reference identity, so the comparison only succeeded
    when both boxes were the same object. Two independently autoboxed values
    are the same object only inside the default `java.lang.Integer` cache,
    which spans -128 to 127. This PR changes the comparison to `==`, which
    compares by value and is null-safe on the left operand. The owner uid is
    now read once per row and feeds both `isOwner` and `ownerId`, and the
    flag is a single expression, `uid != null && ownerUid == uid`.
    
    **Why this is a bug.** `isOwner` answers "is the caller this workflow's
    owner". For every uid above 127 the answer came back `false` for the
    owner's own request, while the `ownerId` in the same response equalled
    the caller's uid. The cutoff is a JVM boxing detail, not a property of
    the data, so the field is correct on a fresh database with small uids
    and silently wrong once a deployment has more than 127 accounts.
    `WorkflowSearchQueryBuilder.toEntryImpl` already computed the same flag
    with `==`, so the two implementations disagreed for the same user and
    the same workflow. `.equals` would also compare by value, but
    `baseWorkflowSelect()` LEFT JOINs `WORKFLOW_OF_USER`, so the record's
    uid can be null and `.equals` would throw; `==` is the null-safe form
    and matches the search builder.
    
    **What the flag reaches today.** Two endpoints go through
    `mapWorkflowEntries`: `GET /workflow/list` and the hub's `getTops`, via
    `HubResource.fetchDashboardWorkflowsByWids`. No current frontend code
    reads `isOwner` from either of them. The dashboard listing that does
    gate affordances on `isOwner` is served by `GET /dashboard/search`,
    which uses the search builder and was already correct, and the hub
    landing page renders its own card that does not read the flag. So this
    PR changes API responses rather than any rendered behavior, and no
    screenshots apply. It is still a defect in a published REST response and
    a contradiction between three code paths that answer the same question.
    
    **One consequence worth stating plainly.** `HubResource.getTops` has no
    `@Auth` parameter and takes `uid` as a query parameter, so the uid it
    reflects into `isOwner` is caller-asserted. Before this change a caller
    passing someone else's uid only got `isOwner: true` back for uids inside
    the Integer cache; afterwards it works for any uid. This widens an
    existing weakness rather than opening a new one, and it is not privilege
    escalation: nothing reads that field, and `deleteWorkflow` independently
    re-checks ownership against the authenticated session user. `isOwner`
    from that endpoint must not be treated as an authorization signal. The
    endpoint's missing authentication is out of scope here and belongs in
    its own issue.
    
    The test changes cover both endpoints that go through
    `mapWorkflowEntries`.
    
    `WorkflowResourceSpec` gains one case for the authenticated `GET
    /workflow/list` path, `set isOwner for an owner whose uid is outside the
    Integer cache`. The suite's existing users have uids 1 and 2, inside the
    cache, so it could not have caught this. The new case creates an owner
    with uid 1001, lists through `retrieveWorkflowsBySessionUser`, and
    asserts `isOwner` is `true` with `ownerId` 1001 for the owner and
    `false` for a user who only holds READ access to the same workflow.
    
    `HubResourceSpec`, whose uids already sit outside the Integer cache,
    gains four positive `isOwner` cases and one merged negative, and drops a
    stale `NOTE` comment that documented this bug as deliberately
    unasserted:
    
    - `set isOwner for the owner's own request when the uid is outside the
    Integer cache` (uid 9001) is the regression proper;
    - `set isOwner for the owner's own request when the uid is inside the
    Integer cache` (uid 42) pins that small uids, which were already
    correct, stay correct;
    - `set isOwner for the highest uid inside the Integer cache (127)` and
    `set isOwner for the lowest uid outside the Integer cache (128)`
    localise the break to a single increment rather than to the gap between
    42 and 9001; and
    - `clear isOwner for a workflow with no ownership row, for an
    authenticated and an anonymous caller` asserts that a null record uid
    yields `false` without throwing, and that both uids being null at once
    also yields `false`, where Scala's `==` reports two nulls as equal and
    only the `uid != null` guard keeps the answer `false`.
    
    The pre-existing negative for a different uid and for an anonymous
    caller is unchanged.
    
    ### Any related issues, documentation, discussions?
    
    Closes #8646
    
    Two neighbouring defects were found while working on this and are
    deliberately not addressed here, since each is a different function and
    a different failure mode: `mapWorkflowEntries` dereferences a null
    privilege for a workflow with no `workflow_user_access` row, and
    `WorkflowSearchQueryBuilder.toEntryImpl` lacks the `uid != null` guard
    that this file has, so an anonymous `publicSearch` caller would report
    `isOwner: true` for a workflow with no ownership row.
    
    ### How was this PR tested?
    
    Unit tests, run against the embedded PostgreSQL that `MockTexeraDB`
    provides, so no external database is needed.
    
    ```shell
    sbt "WorkflowExecutionService/testOnly \
      org.apache.texera.web.resource.dashboard.hub.HubResourceSpec \
      org.apache.texera.web.resource.dashboard.file.WorkflowResourceSpec"
    
    sbt "WorkflowExecutionService/testOnly \
      org.apache.texera.web.resource.dashboard.file.WorkflowResourceSpec \
      org.apache.texera.web.resource.dashboard.hub.HubResourceSpec \
      org.apache.texera.web.resource.dashboard.WorkflowSearchQueryBuilderSpec \
      org.apache.texera.web.resource.dashboard.DashboardResourceSpec \
      org.apache.texera.web.resource.dashboard.DatasetSearchQueryBuilderSpec"
    
    sbt scalafmtCheckAll
    sbt "scalafixAll --check"
    ```
    
    Results:
    
    - `HubResourceSpec`: 69/69 passed and `WorkflowResourceSpec`: 81/81
    passed;
    - the five suites that reach `mapWorkflowEntries` or assert on an
    `isOwner` flag: 190/190 passed;
    - `scalafmtCheckAll`: passed; and
    - `scalafixAll --check`: passed.
    
    Both new behaviours were confirmed to fail without the corresponding
    source text, not merely to pass with it. Replacing `ownerUid == uid`
    with `ownerUid.eq(uid)` fails exactly three cases, the uid 9001 and uid
    128 cases in `HubResourceSpec` and the uid 1001 case in
    `WorkflowResourceSpec`, each with `false was not equal to true`, while
    the uid 127 case still passes, which places the break at one increment.
    Separately, deleting the `uid != null` guard while keeping `==` fails
    exactly one case, the merged no-ownership-row case in `HubResourceSpec`,
    on its anonymous-caller assertion with `true was not equal to false`; no
    pre-existing test catches that, which is why the case was added.
    
    ### Was this PR authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 5)
---
 .../dashboard/user/workflow/WorkflowResource.scala | 11 ++--
 .../dashboard/file/WorkflowResourceSpec.scala      | 29 ++++++++++
 .../resource/dashboard/hub/HubResourceSpec.scala   | 63 +++++++++++++++++++---
 3 files changed, 90 insertions(+), 13 deletions(-)

diff --git 
a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowResource.scala
 
b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowResource.scala
index 6fbd19bcc2..3369ae6494 100644
--- 
a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowResource.scala
+++ 
b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowResource.scala
@@ -315,11 +315,10 @@ object WorkflowResource {
       uid: Integer
   ): List[DashboardWorkflow] = {
     workflowEntries
-      .map(workflowRecord =>
+      .map { workflowRecord =>
+        val ownerUid = workflowRecord.into(WORKFLOW_OF_USER).getUid
         DashboardWorkflow(
-          if (uid != null)
-            workflowRecord.into(WORKFLOW_OF_USER).getUid.eq(uid)
-          else false,
+          uid != null && ownerUid == uid,
           workflowRecord
             .into(WORKFLOW_USER_ACCESS)
             .into(classOf[WorkflowUserAccess])
@@ -327,10 +326,10 @@ object WorkflowResource {
             .toString,
           workflowRecord.into(USER).getName,
           workflowRecord.into(WORKFLOW).into(classOf[Workflow]),
-          workflowRecord.into(WORKFLOW_OF_USER).getUid,
+          ownerUid,
           Option(workflowRecord.get("cover_image", classOf[String]))
         )
-      )
+      }
       .asScala
       .toList
   }
diff --git 
a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/file/WorkflowResourceSpec.scala
 
b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/file/WorkflowResourceSpec.scala
index 24cbf8da2a..47a5b2b307 100644
--- 
a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/file/WorkflowResourceSpec.scala
+++ 
b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/file/WorkflowResourceSpec.scala
@@ -1497,4 +1497,33 @@ class WorkflowResourceSpec
     )
   }
 
+  // The listing's isOwner compares two boxed Integers. testUser and testUser2 
have uids 1
+  // and 2, inside the default Integer autoboxing cache where reference 
equality happens to
+  // agree with value equality, so this case needs an owner whose uid is 
outside it.
+  "WorkflowResource.retrieveWorkflowsBySessionUser" should "set isOwner for an 
owner whose uid is outside the Integer cache" in {
+    val owner = new User
+    owner.setUid(Integer.valueOf(1001))
+    owner.setName("large_uid_owner")
+    owner.setRole(UserRoleEnum.REGULAR)
+    new UserDao(getDSLContext.configuration()).insert(owner)
+    val ownerSession = new SessionUser(owner)
+
+    val wid = seedWorkflow(ownerSession, 
"wf_of_large_uid_user").workflow.getWid
+    try {
+      val own = workflowResource.retrieveWorkflowsBySessionUser(ownerSession)
+      assert(own.map(_.workflow.getWid) == List(wid))
+      assert(own.head.ownerId == owner.getUid)
+      assert(own.head.isOwner, "the owner's own listing must report isOwner")
+
+      // A reader with the same workflow in their listing is not its owner.
+      grantAccess(wid, testUser, PrivilegeEnum.READ)
+      val shared = 
workflowResource.retrieveWorkflowsBySessionUser(sessionUser1)
+      assert(shared.exists(_.workflow.getWid == wid))
+      assert(!shared.find(_.workflow.getWid == wid).get.isOwner)
+    } finally {
+      // afterEach only clears testUser's and testUser2's own workflows.
+      workflowResource.deleteWorkflow(WorkflowIDs(List(wid)), ownerSession)
+    }
+  }
+
 }
diff --git 
a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala
 
b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala
index 88b910107e..695cf9bf46 100644
--- 
a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala
+++ 
b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala
@@ -129,6 +129,12 @@ class HubResourceSpec
   // 
---------------------------------------------------------------------------
 
   private val thirdUid = 9003
+  // Owners inside the default java.lang.Integer autoboxing cache (-128..127), 
where
+  // reference equality on boxed values happens to agree with value equality. 
The
+  // 9001-9003 uids above sit outside it.
+  private val cachedUid = 42
+  private val lastCachedUid = 127
+  private val firstUncachedUid = 128
   private val Ds: EntityType = EntityType.Dataset
   private val Md: EntityType = EntityType.Model
 
@@ -377,6 +383,9 @@ class HubResourceSpec
     // A third liker, needed by the ranking cases below to make "most liked" a
     // strict order (3 > 2 > 1) rather than a tie between two users' likes.
     userDao.insert(makeUser(thirdUid, "hub_third"))
+    userDao.insert(makeUser(cachedUid, "hub_cached_uid"))
+    userDao.insert(makeUser(lastCachedUid, "hub_uid_127"))
+    userDao.insert(makeUser(firstUncachedUid, "hub_uid_128"))
 
     val wf = new Workflow
     wf.setWid(Integer.valueOf(wid))
@@ -1137,13 +1146,39 @@ class HubResourceSpec
     entry.coverImage shouldBe empty
   }
 
-  // NOTE: `isOwner == true` is deliberately not asserted anywhere. 
WorkflowResource's
-  // mapWorkflowEntries compares the two uids with `AnyRef.eq` (reference 
equality on
-  // boxed Integers), so it is true only for uids that land in the 
java.lang.Integer
-  // cache (-128..127); this suite's uids (9001-9003) are outside it, so the 
owner's own
-  // request reports false today. Asserting it either way would either cement 
that bug or
-  // force this suite onto unrealistically small uids. The two negatives below 
hold for
-  // the current implementation and for a value-equality fix alike.
+  // mapWorkflowEntries compares two boxed Integers, so the owner's own 
request must be
+  // recognised on both sides of the default Integer cache (-128..127).
+  it should "set isOwner for the owner's own request when the uid is outside 
the Integer cache" in {
+    val id = seedWorkflow(811003, "wf_mine_uncached", owner = ownerUid)
+
+    fetchDashboardWorkflowsByWids(Seq(id), 
Integer.valueOf(ownerUid)).head.isOwner shouldBe true
+  }
+
+  it should "set isOwner for the owner's own request when the uid is inside 
the Integer cache" in {
+    val id = seedWorkflow(811004, "wf_mine_cached", owner = cachedUid)
+
+    fetchDashboardWorkflowsByWids(Seq(id), 
Integer.valueOf(cachedUid)).head.isOwner shouldBe true
+  }
+
+  // The boundary itself: 127 is the last cached value and 128 the first 
uncached one.
+  it should "set isOwner for the highest uid inside the Integer cache (127)" 
in {
+    val id = seedWorkflow(811006, "wf_mine_uid_127", owner = lastCachedUid)
+
+    fetchDashboardWorkflowsByWids(
+      Seq(id),
+      Integer.valueOf(lastCachedUid)
+    ).head.isOwner shouldBe true
+  }
+
+  it should "set isOwner for the lowest uid outside the Integer cache (128)" 
in {
+    val id = seedWorkflow(811007, "wf_mine_uid_128", owner = firstUncachedUid)
+
+    fetchDashboardWorkflowsByWids(
+      Seq(id),
+      Integer.valueOf(firstUncachedUid)
+    ).head.isOwner shouldBe true
+  }
+
   it should "clear isOwner for a different uid and for an anonymous (null) 
uid" in {
     val id = seedWorkflow(811002, "wf_not_mine", owner = ownerUid)
 
@@ -1151,6 +1186,20 @@ class HubResourceSpec
     fetchDashboardWorkflowsByWids(Seq(id), null).head.isOwner shouldBe false
   }
 
+  // workflow_of_user is LEFT JOINed, so a workflow with no ownership row has 
a null owner
+  // uid; the access row keeps the privilege non-null. For the anonymous 
caller both uids
+  // are null, and Scala's `==` treats two nulls as equal, so the `uid != 
null` guard in
+  // mapWorkflowEntries is what keeps that one false.
+  it should "clear isOwner for a workflow with no ownership row, for an 
authenticated and an anonymous caller" in {
+    val id = seedWorkflow(811005, "wf_unowned", withOwnerRows = false)
+    grantWorkflowAccess(id, ownerUid, PrivilegeEnum.READ)
+
+    val entry = fetchDashboardWorkflowsByWids(Seq(id), 
Integer.valueOf(ownerUid)).head
+    entry.ownerId shouldBe null
+    entry.isOwner shouldBe false
+    fetchDashboardWorkflowsByWids(Seq(id), null).head.isOwner shouldBe false
+  }
+
   // Every resolved id hits LakeFS, so an empty list is the only input 
testable without one.
   "fetchDashboardVersionedResourcesByIds" should "return an empty list for an 
empty id list" in {
     fetchDashboardVersionedResourcesByIds(

Reply via email to