mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Guard pairing token renewal against concurrent edits and cap pairing names
This commit is contained in:
1 parent
3c7bc80988
commit
12dab07b87
4 files changed
+65
-3
No files matched your search
@@ -599,6 +599,44 @@ describe('OpenCodeServerRegistry', () => {
|
||||
expect(fetchFn).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('does not overwrite a password changed while the renewal request is in flight', async () => {
|
||||
const originalToken = pairingToken(Date.now() + 3 * DAY_MS)
|
||||
const server = await createServer({
|
||||
name: 'Tunnel',
|
||||
baseUrl: 'https://studio.example.com',
|
||||
password: originalToken,
|
||||
})
|
||||
|
||||
fetchFn.mockImplementation(async (url) => {
|
||||
if (String(url).endsWith('/api/pair')) return jsonResponse({ code: 'renew-code', expires_in: 300 })
|
||||
await registry.update(server.id, { password: 'changed-while-renewing' })
|
||||
return jsonResponse({ token: renewedToken })
|
||||
})
|
||||
|
||||
await registry.renewExpiringPairingTokens()
|
||||
|
||||
expect(decryptSecret(readStoredSecret(db, server.id)!.value)).toBe('changed-while-renewing')
|
||||
})
|
||||
|
||||
it('warns and does not renew an already-expired pairing token', async () => {
|
||||
const originalToken = pairingToken(Date.now() - DAY_MS)
|
||||
const server = await createServer({
|
||||
name: 'Tunnel',
|
||||
baseUrl: 'https://studio.example.com',
|
||||
password: originalToken,
|
||||
})
|
||||
const warn = vi.spyOn(logger, 'warn').mockImplementation(() => {})
|
||||
|
||||
await registry.renewExpiringPairingTokens()
|
||||
|
||||
expect(decryptSecret(readStoredSecret(db, server.id)!.value)).toBe(originalToken)
|
||||
expect(fetchFn).not.toHaveBeenCalled()
|
||||
expect(warn).toHaveBeenCalledWith(
|
||||
`The pairing token for OpenCode server ${server.id} has expired; pair the server again`,
|
||||
)
|
||||
warn.mockRestore()
|
||||
})
|
||||
|
||||
it('keeps the current token and does not throw when renewal fails', async () => {
|
||||
const originalToken = pairingToken(Date.now() + 3 * DAY_MS)
|
||||
const server = await createServer({
|
||||
|
||||
@@ -321,9 +321,22 @@ export class OpenCodeServerRegistry {
|
||||
if (expiry === null) return
|
||||
|
||||
const remaining = expiry - Date.now()
|
||||
if (remaining <= 0 || remaining > TOKEN_RENEWAL_WINDOW_MS) return
|
||||
if (remaining <= 0) {
|
||||
logger.warn(`The pairing token for OpenCode server ${serverId} has expired; pair the server again`)
|
||||
return
|
||||
}
|
||||
if (remaining > TOKEN_RENEWAL_WINDOW_MS) return
|
||||
|
||||
const renewed = await this.pairWithToken(server.baseUrl, token)
|
||||
const current = this.get(serverId)
|
||||
if (
|
||||
current === null ||
|
||||
current.baseUrl !== server.baseUrl ||
|
||||
readOpenCodeServerPassword(this.db, serverId) !== token
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
updateOpenCodeServer(this.db, serverId, { password: renewed })
|
||||
this.invalidateClient(serverId)
|
||||
} catch (error) {
|
||||
|
||||
@@ -130,6 +130,16 @@ describe('parseOpenCodeConnectLink', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('caps a long QR payload name to the create request maximum', () => {
|
||||
const payload = JSON.stringify({
|
||||
code: 'abc',
|
||||
urls: ['https://abc.def.opentunnel.xyz'],
|
||||
name: 'n'.repeat(150),
|
||||
})
|
||||
|
||||
expect(parseOpenCodeConnectLink(payload)?.name).toBe('n'.repeat(100))
|
||||
})
|
||||
|
||||
it.each([
|
||||
['no urls', JSON.stringify({ code: 'abc', urls: [] })],
|
||||
['a non-string url', JSON.stringify({ code: 'abc', urls: [42] })],
|
||||
|
||||
@@ -166,8 +166,9 @@ function parseConnectUrl(text: string): OpenCodeConnectLink | null {
|
||||
|
||||
export function parseOpenCodeConnectLink(text: string): OpenCodeConnectLink | null {
|
||||
const trimmed = text.trim();
|
||||
if (trimmed.startsWith("{")) return parseConnectPayload(trimmed);
|
||||
return parseConnectUrl(trimmed);
|
||||
const link = trimmed.startsWith("{") ? parseConnectPayload(trimmed) : parseConnectUrl(trimmed);
|
||||
if (link === null) return null;
|
||||
return { ...link, name: link.name.slice(0, 100) };
|
||||
}
|
||||
|
||||
export function buildManagerAppLink(link: ManagerAppLink): string {
|
||||
|
||||
Reference in new issue
Block a user