From e2b3e28ed519b979f23a66eea9d136afcc2f2449 Mon Sep 17 00:00:00 2001 From: Tanishq Gandhi Date: Sat, 28 Nov 2026 10:14:25 -0800 Subject: [PATCH] fix(auth): match the email ignoring case when sharing OwnerAccess.registeredUid, used by every share endpoint, now matches lower(email) like sign-up does, preferring an exact match when two accounts differ only in case. The workflow self-grant check ignores case too. Closes #9738 --- .../workflow/WorkflowAccessResource.scala | 2 +- .../workflow/WorkflowAccessResourceSpec.scala | 10 +++ .../apache/texera/auth/util/OwnerAccess.scala | 8 +- .../util/OwnerAccessRegisteredUidSpec.scala | 75 +++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 common/auth/src/test/scala/org/apache/texera/auth/util/OwnerAccessRegisteredUidSpec.scala diff --git a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala index b6925ec7fd7..3561d7cef4d 100644 --- a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala +++ b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResource.scala @@ -121,7 +121,7 @@ class WorkflowAccessResource() { @PathParam("privilege") privilege: String, @Auth user: SessionUser ): Unit = { - val isModifyingOwnAccess = email.equals(user.getEmail) + val isModifyingOwnAccess = email.equalsIgnoreCase(user.getEmail) val currentPrivilege = getPrivilege(wid, user.getUid) val hasExistingAccess = !currentPrivilege.eq(PrivilegeEnum.NONE) diff --git a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala index 4d4470a6a01..3b7069de70b 100644 --- a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/user/workflow/WorkflowAccessResourceSpec.scala @@ -234,6 +234,16 @@ class WorkflowAccessResourceSpec extends WorkflowAccessDatabaseSpec(withTargetUs ) } + it should "grant access when the email's casing differs from the account's (issue #9738)" in { + seedUserWithoutAccess("new_grantee", "newgrantee@test.com") + + grant("NewGrantee@Test.com", "READ", new SessionUser(userWithWrite)) + + assert( + WorkflowAccess.getPrivilege(testWorkflowWid, newGranteeUid) == PrivilegeEnum.READ + ) + } + it should "change an existing grantee's privilege on re-grant (READ -> WRITE)" in { grant("read@test.com", "WRITE", new SessionUser(userWithWrite)) diff --git a/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala b/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala index 22292eafa8e..06085361255 100644 --- a/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala +++ b/common/auth/src/main/scala/org/apache/texera/auth/util/OwnerAccess.scala @@ -21,6 +21,7 @@ import jakarta.ws.rs.ForbiddenException import org.apache.texera.dao.jooq.generated.Tables.USER import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum import org.jooq.DSLContext +import org.jooq.impl.DSL /** The rules every shared resource applies to its owner and to the users it is shared with. */ object OwnerAccess { @@ -39,13 +40,18 @@ object OwnerAccess { /** The uid of the registered account with this email, or None when the email is unknown or * belongs to a placeholder; neither can be granted access. + * + * Matched ignoring case, like sign-up, since stored emails keep the casing the user typed. If + * two accounts differ only in case, the exact match wins. */ def registeredUid(ctx: DSLContext, email: String): Option[Integer] = Option( ctx .select(USER.UID) .from(USER) - .where(USER.EMAIL.eq(email).and(USER.IS_PLACEHOLDER.isFalse)) + .where(DSL.lower(USER.EMAIL).eq(email.trim.toLowerCase).and(USER.IS_PLACEHOLDER.isFalse)) + .orderBy(DSL.when(USER.EMAIL.eq(email), 0).otherwise(1)) + .limit(1) .fetchOne(USER.UID) ) } diff --git a/common/auth/src/test/scala/org/apache/texera/auth/util/OwnerAccessRegisteredUidSpec.scala b/common/auth/src/test/scala/org/apache/texera/auth/util/OwnerAccessRegisteredUidSpec.scala new file mode 100644 index 00000000000..56fe5b08409 --- /dev/null +++ b/common/auth/src/test/scala/org/apache/texera/auth/util/OwnerAccessRegisteredUidSpec.scala @@ -0,0 +1,75 @@ +// 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.auth.util + +import org.apache.texera.dao.MockTexeraDB +import org.apache.texera.dao.jooq.generated.enums.UserRoleEnum +import org.apache.texera.dao.jooq.generated.tables.daos.UserDao +import org.apache.texera.dao.jooq.generated.tables.pojos.User +import org.scalatest.BeforeAndAfterAll +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +class OwnerAccessRegisteredUidSpec + extends AnyFlatSpec + with Matchers + with BeforeAndAfterAll + with MockTexeraDB { + + private def insertUser(uid: Int, email: String, placeholder: Boolean = false): Unit = { + val u = new User + u.setUid(Integer.valueOf(uid)) + u.setName(s"user$uid") + u.setEmail(email) + u.setRole(UserRoleEnum.REGULAR) + u.setIsPlaceholder(placeholder) + new UserDao(getDSLContext.configuration()).insert(u) + } + + override def beforeAll(): Unit = { + initializeDBAndReplaceDSLContext() + insertUser(1, "alice@example.com") + insertUser(2, "Bob.Smith@example.com") + insertUser(3, "carol@example.com", placeholder = true) + // Two accounts that differ only in case, which the plain UNIQUE on email allows. + insertUser(4, "dave@example.com") + insertUser(5, "Dave@example.com") + } + + override def afterAll(): Unit = closeConnectionPool() + + private def uidOf(email: String): Option[Int] = + OwnerAccess.registeredUid(getDSLContext, email).map(_.intValue()) + + "registeredUid" should "find an account whatever the casing typed (issue #9738)" in { + uidOf("alice@example.com") shouldBe Some(1) + uidOf("Alice@Example.com") shouldBe Some(1) + uidOf("bob.smith@example.com") shouldBe Some(2) + uidOf("BOB.SMITH@EXAMPLE.COM") shouldBe Some(2) + } + + it should "prefer the exact match when two accounts differ only in case" in { + uidOf("dave@example.com") shouldBe Some(4) + uidOf("Dave@example.com") shouldBe Some(5) + } + + it should "not find a placeholder or an unknown email" in { + uidOf("Carol@example.com") shouldBe None + uidOf("nobody@example.com") shouldBe None + } +}