bug: token allow-list with a specific template ID returns 500 "column reference \"id\" is ambiguous"

Abierto Apto para principiantes
#32,059 2 comentarios 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@stirby ya está trabajando en esto.

Desde el 12/11/2026.

  • #32103 de @stirby — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go, postgresql

Línea de trabajo

Empieza por TemplateConverter() en coderd/rbac/regosql/configs.go y GetAuthorizedTemplates en coderd/database/modelqueries.go; inspecciona cómo se aplica el filtro compilado de ID de template a la consulta de templates unida. Añade una prueba de regresión para una lista de permitidos de ID de template específica, usando la prueba de consulta o de autorización que cubre esta ruta. El trabajo está terminado cuando la consulta se ejecuta correctamente y devuelve solo los templates permitidos por el token.

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

Descripción

Is there an existing issue for this?
  • I have searched the existing issues
Current Behavior

When testing API-token allow-lists, a token whose allow-list names a specific template (template:<template-uuid>) gets an HTTP 500 from any request that runs the authorized templates query. Workspace creation with the same token succeeds. Reading the workspace back afterwards fails, because building the workspace response loads templates through GetAuthorizedTemplates.

The same token with template:* instead works.

Relevant Log Output
GET /api/v2/workspaces/<workspace-id>
500 {"message":"Internal error fetching workspace resources.","detail":"get templates: pq: column reference \"id\" is ambiguous"}
Expected Behavior

Requests for templates named in the token's allow-list are authorized normally (here, GET /api/v2/workspaces/<workspace-id> returns 200), and templates outside the allow-list are filtered out.

Steps to Reproduce
  1. As an admin, create a regular user <user> with the default Member role in the default organization.
  2. Create a scoped token for that user with a template-specific allow-list:
    coder tokens create --user <user> --lifetime 1d \
      --scope coder:workspaces.create --scope coder:workspaces.access \
      --allow template:<template-uuid> \
      --allow workspace:* \
      --allow organization_member:<user-uuid>
    
  3. With that token, POST /api/v2/users/<user>/workspaces using a version of <template-uuid>. This returns 201.
  4. With that token, GET /api/v2/workspaces/<workspace-id>. This returns the 500 above.
  5. Repeat with --allow template:* instead of --allow template:<template-uuid>. Step 4 then returns 200.
Root cause

TemplateConverter() in coderd/rbac/regosql/configs.go registers resourceIDMatcher(), which emits an unqualified column:

func resourceIDMatcher() sqltypes.VariableMatcher {
	return sqltypes.StringVarMatcher("id :: text", []string{"input", "object", "id"})
}

GetAuthorizedTemplates in coderd/database/modelqueries.go appends the compiled filter to GetTemplatesWithFilter, which joins a second table that also has an id column:

SELECT t.*
FROM template_with_names AS t
LEFT JOIN template_versions tv ON t.active_version_id = tv.id
WHERE ...

The ID predicate is only emitted when the allow-list names a specific template ID. That is why wildcard allow-lists, and tokens without allow-lists, are not affected. The same converter already qualifies the organization column (t.organization_id :: text), so the likely fix is a template-qualified ID matcher in TemplateConverter, such as t.id :: text. A regression test that compiles a non-wildcard template allow-list against this query would cover it.

Workaround

Use --allow template:*. The token can then read and use any template the user's role can already access, so pinning to one template has to be enforced by the client.

Environment
  • Host OS: macOS (arm64), Coder server with built-in PostgreSQL
  • Coder version: v2.38.7. The same code is present in v2.39.1 and on main at 1126533c3169aaea7b44824363375edbf480a742 (resourceIDMatcher and TemplateConverter in coderd/rbac/regosql/configs.go, GetTemplatesWithFilter in coderd/database/queries/templates.sql).
Additional Context

The issue occurs consistently. Possibly related to the broader compiled-SQL audit in #25982, but this is a specific query failure rather than an ACL semantics difference.

Lenguaje dominante
Go
Estrellas
17.5k
Forks
1.7k
Merge medio
2 d 23 h
PR fusionados (30 d)
575

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

Todos los issues de coder/coder

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.