Files
zitadel/internal/api/well_known/well_known_test.go
T
3c3bd2bc57 fix(api): correct caching and relations of well-known app link files (#12634)
# Which Problems Are Solved

Two issues with the native app link well-known files reported after the
v4.17.0 rollout:

**1. `assetlinks.json` intermittently returned an empty `[]`** on
ZITADEL Cloud without any configuration change, across all of a
customer's environments, breaking native passkey verification. The
configuration itself was stored correctly.

The responses were served with `Cache-Control: public, max-age=<n>`. The
`public` directive opts the response into shared caches: the CDN in
front of ZITADEL Cloud is configured to cache only responses that
explicitly set `public` or `s-maxage`. That cache is a fleet of
independent edge nodes, each holding its own copy. A single momentarily
inconsistent origin response (e.g. an empty file) gets pinned on the
edge that captured it for the whole `max-age` and served to every client
routed there, while other edges serve the correct file — producing the
observed flip-flop. This is confirmed by the JWKS endpoint, which serves
`max-age=<n>, must-revalidate` **without** `public`, has never been
cached by the CDN, and has never shown this inconsistency.

**2. Passkey creation failed on Google Pixel devices** while working on
iOS and other Android devices. Google's [Credential Manager
prerequisites](https://developer.android.com/identity/credential-manager/prerequisites)
require the Digital Asset Links statement for passkeys to declare
**both** `delegate_permission/common.handle_all_urls` and
`delegate_permission/common.get_login_creds`; ZITADEL only served
`get_login_creds`, and Pixel devices enforce the documented contract
strictly.

# How the Problems Are Solved

- Drop `public` and mirror the JWKS endpoint: `Cache-Control:
max-age=<n>, must-revalidate`. Clients (the Apple and Google verifiers,
browsers) still cache the files for `max-age`; the origin stays the
single source of truth. The load profile matches JWKS, which already
serves every request from origin at far higher volume, so this does not
introduce a thundering-herd risk. A test guard asserts `public` is never
emitted.
- Emit both required relations in `/.well-known/assetlinks.json`,
matching Google's documented example. The additional relation only
permits the app to handle links for the domain; it has no effect unless
the app itself declares matching intent filters, and the entry is
configured by the app owner.

# Additional Changes

None. `AppLinksCacheControlMaxAge` and its `0 → no-store` behaviour are
unchanged.

# Additional Context

- Introduced with the native app links feature (#12580 / #12497).
- Immediate relief for the caching issue was applied by invalidating the
CDN cache for both paths; this change prevents recurrence.
- To be cherry-picked to `v4.x` from `main` once merged.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
2026-08-24 15:25:39 +02:00

246 lines
6.7 KiB
Go

package well_known
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
http_util "github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/query"
)
func TestBuildAppleAppSiteAssociation(t *testing.T) {
t.Parallel()
tt := []struct {
name string
configs []*query.OIDCAppLinkConfig
wantApps []string
}{
{
name: "empty",
configs: nil,
wantApps: []string{},
},
{
name: "skips incomplete ios",
configs: []*query.OIDCAppLinkConfig{
{IOSTeamID: "TEAM"},
{IOSBundleID: "com.example"},
},
wantApps: []string{},
},
{
name: "aggregates and dedupes",
configs: []*query.OIDCAppLinkConfig{
{IOSTeamID: "TEAM1", IOSBundleID: "com.one"},
{IOSTeamID: "TEAM2", IOSBundleID: "com.two"},
{IOSTeamID: "TEAM1", IOSBundleID: "com.one"},
{AndroidPackageName: "com.android", AndroidSHA256CertFingerprints: []string{"AA"}},
},
wantApps: []string{"TEAM1.com.one", "TEAM2.com.two"},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := buildAppleAppSiteAssociation(tc.configs)
assert.Equal(t, tc.wantApps, got.WebCredentials.Apps)
})
}
}
func TestBuildAssetLinks(t *testing.T) {
t.Parallel()
tt := []struct {
name string
configs []*query.OIDCAppLinkConfig
want []assetLink
}{
{
name: "empty",
configs: nil,
want: []assetLink{},
},
{
name: "skips incomplete android",
configs: []*query.OIDCAppLinkConfig{
{AndroidPackageName: "com.example"},
{AndroidSHA256CertFingerprints: []string{"AA"}},
},
want: []assetLink{},
},
{
name: "aggregates and normalizes fingerprints",
configs: []*query.OIDCAppLinkConfig{
{
IOSTeamID: "TEAM",
IOSBundleID: "com.ios",
AndroidPackageName: "com.one",
AndroidSHA256CertFingerprints: []string{"aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899"},
},
{
AndroidPackageName: "com.two",
AndroidSHA256CertFingerprints: []string{
"cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb",
"eeff00112233445566778899aabbccddeeff00112233445566778899aabbccdd",
},
},
},
want: []assetLink{
{
Relation: []string{"delegate_permission/common.handle_all_urls", "delegate_permission/common.get_login_creds"},
Target: assetLinkTarget{
Namespace: "android_app",
PackageName: "com.one",
SHA256CertFingerprints: []string{
"AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99",
},
},
},
{
Relation: []string{"delegate_permission/common.handle_all_urls", "delegate_permission/common.get_login_creds"},
Target: assetLinkTarget{
Namespace: "android_app",
PackageName: "com.two",
SHA256CertFingerprints: []string{
"CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB",
"EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD",
},
},
},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, buildAssetLinks(t.Context(), tc.configs))
})
}
}
func TestNormalizeSHA256Fingerprint(t *testing.T) {
t.Parallel()
tt := []struct {
name string
in string
want string
}{
{
name: "already canonical",
in: "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99",
want: "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99",
},
{
name: "lowercase with colons",
in: "aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99",
want: "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99",
},
{
name: "lowercase without separators",
in: "aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899",
want: "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99",
},
{
name: "invalid continuous hex uppercased",
in: "aabb",
want: "AABB",
},
{
name: "colon form uppercased without reparse",
in: "aa:bb",
want: "AA:BB",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, normalizeSHA256Fingerprint(t.Context(), tc.in))
})
}
}
type stubAppLinkQuerier struct {
configs []*query.OIDCAppLinkConfig
err error
}
func (s stubAppLinkQuerier) SearchOIDCAppLinkConfigs(_ context.Context) ([]*query.OIDCAppLinkConfig, error) {
return s.configs, s.err
}
func TestHandlerCacheControl(t *testing.T) {
t.Parallel()
tt := []struct {
name string
maxAge time.Duration
wantHeader string
}{
{
name: "default max-age",
maxAge: 5 * time.Minute,
wantHeader: "max-age=300, must-revalidate",
},
{
name: "no-store when zero",
maxAge: 0,
wantHeader: "no-store",
},
{
name: "no-store when negative",
maxAge: -time.Minute,
wantHeader: "no-store",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
h := NewHandler(stubAppLinkQuerier{}, Config{AppLinksCacheControlMaxAge: tc.maxAge})
for _, path := range []string{AppleAppSiteAssociationPath, AssetLinksPath} {
req := httptest.NewRequest(http.MethodGet, path, nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
cacheControl := rec.Header().Get(http_util.CacheControl)
assert.Equal(t, tc.wantHeader, cacheControl, path)
assert.NotContains(t, cacheControl, "stale-while-revalidate")
// "public" must never be set: it opts the response into shared caches
// (e.g. a CDN), whose independent edges can pin diverging copies.
assert.NotContains(t, cacheControl, "public", path)
}
})
}
}
func TestHandlerHeadOmitsBody(t *testing.T) {
t.Parallel()
h := NewHandler(stubAppLinkQuerier{}, Config{AppLinksCacheControlMaxAge: 5 * time.Minute})
for _, path := range []string{AppleAppSiteAssociationPath, AssetLinksPath} {
t.Run(path, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(http.MethodHead, path, nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
assert.Equal(t, "max-age=300, must-revalidate", rec.Header().Get(http_util.CacheControl))
assert.Empty(t, rec.Body.Bytes())
})
}
}