Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(backend): a phone signed in by Harness removes a machine with no billing (#2771)

Bubu-Dudu-52hzaeba47a3c971
diff --git a/backend/src/routes/machines.ts b/backend/src/routes/machines.ts
index b565ebca8..c04cb2fb5 100644
--- a/backend/src/routes/machines.ts
+++ b/backend/src/routes/machines.ts
@@ -407,11 +407,24 @@ export async function machineRoutes(app: FastifyInstance): Promise<void> {
'/api/machines/:machineId',
{ preHandler: validateParams(machineParams) },
async (req, reply) => {
- await machineService.destroy(
- req.params.machineId,
- req.user!,
- await requestAccessToken(req),
- )
+ // Only a billed machine needs the SSO token: its subscription is cancelled with it. A session nothing
+ // backs (a phone signed in by Harness) removes any other machine all the same; it used to be refused
+ // up front, the free Harness-EE07 included (iPhone, 2026-11-03). A billed one still asks for the sign-in.
+ let accessToken: string | undefined
+ let signIn: AppError | undefined
+ try {
+ accessToken = await requestAccessToken(req)
+ } catch (err) {
+ if (!(err instanceof AppError) || err.code !== 'NEEDS_AUTONOMOUS_SIGN_IN') throw err
+ signIn = err
+ }
+ try {
+ await machineService.destroy(req.params.machineId, req.user!, accessToken)
+ } catch (err) {
+ // destroy's own refusal of a billed machine without a token: say what to do instead.
+ if (signIn && err instanceof AppError && err.statusCode === 401 && err.code === 'UNAUTHORIZED') throw signIn
+ throw err
+ }
return sendSuccess(reply, { deleted: true })
},
)
diff --git a/backend/src/routes/machinesBillingToken.test.ts b/backend/src/routes/machinesBillingToken.test.ts
index 0e52a2ba5..d5003166b 100644
--- a/backend/src/routes/machinesBillingToken.test.ts
+++ b/backend/src/routes/machinesBillingToken.test.ts
@@ -27,6 +27,7 @@ import { machineRoutes } from './machines.js'
import { errorHandler } from '../middlewares/errorHandler.js'
import { SsoAuthError, type AuthUser } from '../lib/ssoAuth.js'
import { HARNESS_ACCESS_PREFIX, newHarnessToken } from '../lib/harnessTokenFormat.js'
+import { AppError } from '../errors/index.js'
const MACHINE = 'a'.repeat(32)
const SSO_USER: AuthUser = { sub: 'u1', email: '[email protected]', role: 'user', autonomousEnv: 'prod' }
@@ -88,4 +89,30 @@ describe('the SSO token billing forwards', () => {
expect(res.statusCode).toBe(200)
expect(m.destroy).toHaveBeenCalledWith(MACHINE, HARNESS_USER, 'kept-sso-access')
})
+
+ const remove = () => app.inject({ method: 'DELETE', url: `/api/machines/${MACHINE}`,
+ headers: { authorization: `Bearer ${newHarnessToken(HARNESS_ACCESS_PREFIX)}` } })
+
+ it('is not needed to remove a machine with no billing, from a session nothing backs', async () => {
+ caller = HARNESS_USER
+ const res = await remove()
+ expect(res.statusCode).toBe(200)
+ expect(m.destroy).toHaveBeenCalledWith(MACHINE, HARNESS_USER, undefined)
+ })
+
+ it('is still asked for by a billed machine: sign in with Google or Apple', async () => {
+ caller = HARNESS_USER
+ m.destroy.mockRejectedValueOnce(new AppError('SSO access token required to cancel machine billing', 401, 'UNAUTHORIZED'))
+ const res = await remove()
+ expect(res.statusCode).toBe(403)
+ expect(res.json().error.code).toBe('NEEDS_AUTONOMOUS_SIGN_IN')
+ })
+
+ it('does not hide another refusal of the removal', async () => {
+ caller = HARNESS_USER
+ m.destroy.mockRejectedValueOnce(new AppError('Not your machine', 403, 'FORBIDDEN'))
+ const res = await remove()
+ expect(res.statusCode).toBe(403)
+ expect(res.json().error.code).toBe('FORBIDDEN')
+ })
})