-
Notifications
You must be signed in to change notification settings - Fork 12
Read a realm identifier without assuming it is a URL #5931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
backspace
merged 5 commits into
main
from
cs-12692-read-a-realm-identifier-without-assuming-it-is-a-url
Aug 31, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3402568
Read a realm identifier without assuming it is a URL
backspace bd52016
Match a hosted route against the realm's mounted path
backspace dc1bf1e
Mint realm-picker option ids resolved
backspace 5c3e7d6
Resolve the realm identifiers these two lookups still assumed were URLs
backspace 1ccbb98
Pin the realm identifier prefix branches with tests
backspace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/host/tests/integration/realm-server-form-agnostic-test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { getService } from '@universal-ember/test-support'; | ||
| import window from 'ember-window-mock'; | ||
| import { module, test } from 'qunit'; | ||
|
|
||
| import { baseRealm } from '@cardstack/runtime-common'; | ||
|
|
||
| import { SessionLocalStorageKey } from '@cardstack/host/utils/local-storage-keys'; | ||
|
|
||
| import { | ||
| testRealmURL, | ||
| setupIntegrationTestRealm, | ||
| setupLocalIndexing, | ||
| } from '../helpers'; | ||
| import { setupBaseRealm } from '../helpers/base-realm'; | ||
| import { setupMockMatrix } from '../helpers/mock-matrix'; | ||
| import { setupRenderingTest } from '../helpers/setup'; | ||
|
|
||
| // `window` here is ember-window-mock's, the same one the service reads its | ||
| // session tokens through — `setupRenderingTest` installs it. Writing to the | ||
| // real global instead would leave the service seeing no tokens at all. | ||
| // | ||
| // A realm mapped to an origin of its own, so the resolved URL is not the test | ||
| // realm's — `getRealmServersForRealms` skips anything at the test realm origin, | ||
| // which would hide the token lookup this exercises. | ||
| const MAPPED_REALM_URL = 'https://mapped-realm.example.com/realm/'; | ||
| const REALM_SERVER_URL = 'https://mapped-server.example.com/'; | ||
| const PREFIX = '@form-agnostic-test/'; | ||
|
|
||
| // A session token is only ever read for its claims here, never verified, so a | ||
| // header-and-payload pair is the whole shape that matters. | ||
| function sessionToken(claims: Record<string, unknown>): string { | ||
| return `header.${btoa(JSON.stringify(claims))}`; | ||
| } | ||
|
|
||
| // `getRealmServersForRealms` answers "which realm server serves these realms?" | ||
| // by looking each realm's session token up by its identifier. A registered | ||
| // prefix is a realm identifier too, and it matches no token key — so without | ||
| // resolution the lookup misses, the loop skips the realm, and an empty result | ||
| // set makes the function answer with *this* realm server rather than the | ||
| // realm's. That is a wrong answer returned quietly, which is what these pin. | ||
| module('Integration | realm-server | identifier forms', function (hooks) { | ||
| setupRenderingTest(hooks); | ||
| setupLocalIndexing(hooks); | ||
|
|
||
| let mockMatrixUtils = setupMockMatrix(hooks, { | ||
| loggedInAs: '@testuser:localhost', | ||
| activeRealms: [baseRealm.url, testRealmURL], | ||
| autostart: true, | ||
| }); | ||
|
|
||
| setupBaseRealm(hooks); | ||
|
|
||
| hooks.beforeEach(async function () { | ||
| await setupIntegrationTestRealm({ mockMatrixUtils, contents: {} }); | ||
| }); | ||
|
|
||
| hooks.afterEach(function () { | ||
| window.localStorage.removeItem(SessionLocalStorageKey); | ||
| getService('network').virtualNetwork.removeRealmMapping(PREFIX); | ||
| }); | ||
|
|
||
| test('finds a realm server for a realm named by a URL identifier', function (assert) { | ||
| window.localStorage.setItem( | ||
| SessionLocalStorageKey, | ||
| JSON.stringify({ | ||
| [MAPPED_REALM_URL]: sessionToken({ realmServerURL: REALM_SERVER_URL }), | ||
| }), | ||
| ); | ||
|
|
||
| let realmServer = getService('realm-server'); | ||
| assert.deepEqual( | ||
| realmServer.getRealmServersForRealms([MAPPED_REALM_URL]), | ||
| [REALM_SERVER_URL], | ||
| 'the URL form finds its token and reports the realm’s own server', | ||
| ); | ||
| }); | ||
|
|
||
| test('finds a realm server for a realm named by a registered prefix', function (assert) { | ||
| // The token is filed under the realm's URL, which is how a realm resource | ||
| // that logged in with a URL files it — the prefix has to resolve to reach it. | ||
| getService('network').virtualNetwork.addRealmMapping( | ||
| PREFIX, | ||
| MAPPED_REALM_URL, | ||
| ); | ||
| window.localStorage.setItem( | ||
| SessionLocalStorageKey, | ||
| JSON.stringify({ | ||
| [MAPPED_REALM_URL]: sessionToken({ realmServerURL: REALM_SERVER_URL }), | ||
| }), | ||
| ); | ||
|
|
||
| let realmServer = getService('realm-server'); | ||
| assert.deepEqual( | ||
| realmServer.getRealmServersForRealms([PREFIX]), | ||
| [REALM_SERVER_URL], | ||
| 'the prefix form resolves to the same token and the same server', | ||
| ); | ||
| }); | ||
|
|
||
| test('a prefix whose token is filed under the prefix is still found', function (assert) { | ||
| // The other direction: a realm resource created from the prefix files its | ||
| // token under the prefix, so the unresolved spelling has to work too. | ||
| getService('network').virtualNetwork.addRealmMapping( | ||
| PREFIX, | ||
| MAPPED_REALM_URL, | ||
| ); | ||
| window.localStorage.setItem( | ||
| SessionLocalStorageKey, | ||
| JSON.stringify({ | ||
| [PREFIX]: sessionToken({ realmServerURL: REALM_SERVER_URL }), | ||
| }), | ||
| ); | ||
|
|
||
| let realmServer = getService('realm-server'); | ||
| assert.deepEqual( | ||
| realmServer.getRealmServersForRealms([PREFIX]), | ||
| [REALM_SERVER_URL], | ||
| 'both spellings are accepted as token keys', | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Claude Code 🤖]
onChangestill doesnew URL(opt.id), andopt.idis a realm identifier straight fromavailableRealmIdentifiers— so this component's hard failure is untouched while its soft one is fixed. A prefix reaching the label path now yields the realm's name; the same prefix reaching a click throwsTypeError: Invalid URLand takes the picker's selection with it.The lint rule can't reach it:
PickerOption.idis plainstring, so theRealmIdentifierbrand is gone the moment the identifier is stored into an option and read back. That makes green lint no evidence for this file.Resolving at the parse point isn't enough either, because the two ends mint ids in different spellings:
realmOptionsuses the identifier,pickerSelectedusesurl.href, andPickermatches selection byo.id === option.id(packages/boxel-ui/src/components/picker/index.gts). With a prefix registered, a selected realm would come back as an option that never renders selected.Both fall out if the ids are minted resolved — inject the network service and build
realmOptionsids asvn.toURL(identifier).href. Then everyopt.idis URL-form,new URL(opt.id)is safe, and the option/selected spellings agree.Class: pre-existing, not introduced here — but it is in this file and inside the PR's own stated scope, and it is the failure that throws. Non-blocking only if you'd rather split it; I'd take it here.
Generated by Claude Code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Claude Code 🤖] Taken here.
realmOptionsnow mints ids throughresolvedRealmURLHref, so everyopt.idis URL-form:onChange's parse is safe, and the option/selected spellings agree soPicker's id comparison can't miss. Label andrealm.info()still read the identifier as given —info()is form-agnostic already, andrealmIdentifierSegmentswants the identifier, not the mount path.Confirmed no consumer depends on the old spelling:
onChangehands outURL[], andselectedURLsis minted by the parent rather than from option ids.