Fix various bad tenant isolation

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-10-30 15:39:38 +01:00
parent a42670d5bd
commit 29917578fc
30 changed files with 1259 additions and 1369 deletions

View File

@@ -153,3 +153,52 @@ WHERE %s
return err
}
// LoadFilesByIDs loads multiple files by their IDs in a single query
// Returns a map of file ID to File for efficient lookup
func LoadFilesByIDs(
ctx context.Context,
conn pg.Conn,
fileIDs []gid.GID,
) (map[gid.GID]*File, error) {
if len(fileIDs) == 0 {
return make(map[gid.GID]*File), nil
}
q := `
SELECT
id,
bucket_name,
mime_type,
file_name,
file_key,
file_size,
created_at,
updated_at,
deleted_at
FROM
files
WHERE
id = ANY(@file_ids)
AND deleted_at IS NULL
`
args := pgx.StrictNamedArgs{"file_ids": fileIDs}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query files: %w", err)
}
files, err := pgx.CollectRows(rows, pgx.RowToStructByName[File])
if err != nil {
return nil, fmt.Errorf("cannot collect files: %w", err)
}
result := make(map[gid.GID]*File, len(files))
for i := range files {
result[files[i].ID] = &files[i]
}
return result, nil
}

View File

@@ -237,11 +237,10 @@ WHERE
return nil
}
// Tenant scope is not applied because this is used to query invitations across all tenants
// for a user who doesn't have tenant access yet (before accepting an invitation).
func (i *Invitations) LoadByEmail(
ctx context.Context,
conn pg.Conn,
scope Scoper,
email string,
cursor *page.Cursor[InvitationOrderField],
filter *InvitationFilter,

View File

@@ -106,10 +106,10 @@ LIMIT 1;
return nil
}
// Tenant id scope is not applied in this functions because we want to access all user's organizations.
func (o *Organizations) LoadByUserID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
userID gid.GID,
cursor *page.Cursor[OrganizationOrderField],
) error {
@@ -140,10 +140,11 @@ FROM
INNER JOIN
user_org ON organizations.id = user_org.organization_id
WHERE
%s
%S
AND %s
`
q = fmt.Sprintf(q, cursor.SQLFragment())
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"user_id": userID}
maps.Copy(args, cursor.SQLArguments())
@@ -163,7 +164,6 @@ WHERE
return nil
}
// Tenant id scope is not applied in this function because we want to access all user's organizations.
func (o *Organizations) LoadAllByUserID(
ctx context.Context,
conn pg.Conn,
@@ -379,3 +379,50 @@ LIMIT 1
return nil
}
func (o *Organizations) BatchLoadByID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationIDs []gid.GID,
) error {
q := `
SELECT
tenant_id,
id,
name,
logo_file_id,
horizontal_logo_file_id,
description,
website_url,
email,
headquarter_address,
custom_domain_id,
created_at,
updated_at
FROM
organizations
WHERE
%s
AND id = ANY(@organization_ids)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"organization_ids": organizationIDs}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query organizations: %w", err)
}
organizations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Organization])
if err != nil {
return fmt.Errorf("cannot collect organizations: %w", err)
}
*o = organizations
return nil
}

View File

@@ -441,3 +441,66 @@ ORDER BY created_at ASC;
return result, nil
}
// LoadSAMLConfigurationsByOrganizationIDsAndEmailDomain loads SAML configurations for multiple organizations
// and a given email domain in a single query. This is used to avoid N+1 queries.
func LoadSAMLConfigurationsByOrganizationIDsAndEmailDomain(
ctx context.Context,
conn pg.Conn,
organizationIDs []gid.GID,
emailDomain string,
) (map[gid.GID]*SAMLConfiguration, error) {
if len(organizationIDs) == 0 {
return make(map[gid.GID]*SAMLConfiguration), nil
}
q := `
SELECT
id,
organization_id,
email_domain,
enabled,
enforcement_policy,
idp_entity_id,
idp_sso_url,
idp_certificate,
idp_metadata_url,
attribute_email,
attribute_firstname,
attribute_lastname,
attribute_role,
auto_signup_enabled,
domain_verified,
domain_verification_token,
domain_verified_at,
created_at,
updated_at
FROM
auth_saml_configurations
WHERE
organization_id = ANY(@organization_ids)
AND email_domain = @email_domain
`
args := pgx.StrictNamedArgs{
"organization_ids": organizationIDs,
"email_domain": emailDomain,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query auth_saml_configurations: %w", err)
}
configs, err := pgx.CollectRows(rows, pgx.RowToStructByName[SAMLConfiguration])
if err != nil {
return nil, fmt.Errorf("cannot collect saml_configurations: %w", err)
}
result := make(map[gid.GID]*SAMLConfiguration, len(configs))
for i := range configs {
result[configs[i].OrganizationID] = &configs[i]
}
return result, nil
}