Sync-DbaLoginPermission and Copy-DbaLogin - Database permissions are synced by the name of the login instead of the database user

Abierto
#10,837 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
42/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
sql

Línea de trabajo

Start in private/functions/Update-SqlPermission.ps1: the revoke calls near lines 261, 262 and 279 and the grant call near line 373 enumerate permissions by login name where the database user name ($newDbUsername, $dbUsername) should be used. Read the reproduction, then add a context to Sync-DbaLoginPermission.Tests.ps1 that fails on the current code. Done means that context passes and the unrelated user keeps its permissions. The issue says to wait for #10801, which changes the same helper, before changing it.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Problem

Update-SqlPermission syncs the database permissions of a login by the name of the login. A database principal is a user, and a user can have a name other than its login. The helper is private. Copy-DbaLogin uses it for every created and updated login, and Sync-DbaLoginPermission for every login it syncs.

On development (72ae9ee82), private/functions/Update-SqlPermission.ps1:

  • The revoke part (lines 261, 262 and 279) calls $destDb.EnumDatabasePermissions($newLoginName) and $sourceDb.EnumDatabasePermissions($loginName), and revokes from $newLoginName. The name of the user is in $newDbUsername and $dbUsername, set a few lines above.
  • The grant part (line 373) reads $sourceDb.EnumDatabasePermissions($loginName) and grants to $newDbUsername.
Reproduction

On development 72ae9ee82, with SQL Server 2022 as the source and SQL Server 2025 as the destination:

  • A login L has the same SID on both instances.
  • A database D exists on both, and in D a user U for L has another name than the login.
  • On the source: GRANT VIEW DEFINITION TO [U].
  • On the destination: GRANT ALTER ANY SCHEMA TO [U], and an unrelated user without login that has the name of the login: CREATE USER [L] WITHOUT LOGIN; GRANT VIEW DATABASE STATE TO [L].

Sync-DbaLoginPermission -Source <source> -Destination <destination> -Login L returns Successful without a warning. Afterwards, on the destination:

  • U still has ALTER ANY SCHEMA, which the source user does not have.
  • U did not get VIEW DEFINITION.
  • The unrelated user L lost VIEW DATABASE STATE: the revoke hit a principal that has nothing to do with the login.
The same place has a second gap

The revoke runs without AS <grantor>. For server permissions, #10801 measured that a REVOKE without AS takes back only the grant of sa when two logins granted the same permission, and fixed it there by revoking each row as its grantor (0fc03cbca). The database part has the same call. That was not measured at database level yet.

Proposed fix

Use the database user for both enumerations and for the revoke, and revoke each row as its grantor, as 0fc03cbca does for the server permissions. Regression test: the reproduction above as a new context in Sync-DbaLoginPermission.Tests.ps1. It needs only the copy pair, so it runs in CI. The fix should wait until #10801 is merged, because #10801 changes the same helper.

Related: #9283 (closed), a database user whose name differs from its login, with -LoginRenameHashtable.


This text was created by Claude and reviewed by Andreas Jordan.

Lenguaje dominante
PowerShell
Estrellas
2.9k
Forks
869
Merge medio
2 d 4 h
PR fusionados (30 d)
38

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dataplat/dbatools

Todos los issues de dataplat/dbatools

Issues similares

Más issues de Security

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.