@ -1,4 +1,4 @@
import { afterAll , beforeAll, describe , expect , test , vi } from "vitest" ;
import { afterAll , afterEach, beforeAll, beforeEach , describe , expect , test , vi } from "vitest" ;
import { WebSocket } from "ws" ;
import { GATEWAY_CLIENT_MODES , GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js" ;
import { buildDeviceAuthPayload } from "./device-auth.js" ;
@ -9,6 +9,7 @@ import {
getFreePort ,
installGatewayTestHooks ,
onceMessage ,
rpcReq ,
startGatewayServer ,
startServerWithClient ,
testTailscaleWhois ,
@ -30,8 +31,8 @@ async function waitForWsClose(ws: WebSocket, timeoutMs: number): Promise<boolean
} ) ;
}
const openWs = async ( port : number ) = > {
const ws = new WebSocket ( ` ws://127.0.0.1: ${ port } ` );
const openWs = async ( port : number , headers? : Record < string , string > ) = > {
const ws = new WebSocket ( ` ws://127.0.0.1: ${ port } ` , headers ? { headers } : undefined );
await new Promise < void > ( ( resolve ) = > ws . once ( "open" , resolve ) ) ;
return ws ;
} ;
@ -39,6 +40,7 @@ const openWs = async (port: number) => {
const openTailscaleWs = async ( port : number ) = > {
const ws = new WebSocket ( ` ws://127.0.0.1: ${ port } ` , {
headers : {
origin : "https://gateway.tailnet.ts.net" ,
"x-forwarded-for" : "100.64.0.1" ,
"x-forwarded-proto" : "https" ,
"x-forwarded-host" : "gateway.tailnet.ts.net" ,
@ -50,6 +52,8 @@ const openTailscaleWs = async (port: number) => {
return ws ;
} ;
const originForPort = ( port : number ) = > ` http://127.0.0.1: ${ port } ` ;
describe ( "gateway server auth/connect" , ( ) = > {
describe ( "default auth (token)" , ( ) = > {
let server : Awaited < ReturnType < typeof startGatewayServer > > ;
@ -101,6 +105,147 @@ describe("gateway server auth/connect", () => {
ws . close ( ) ;
} ) ;
test ( "does not grant admin when scopes are empty" , async ( ) = > {
const ws = await openWs ( port ) ;
const res = await connectReq ( ws , { scopes : [ ] } ) ;
expect ( res . ok ) . toBe ( true ) ;
const health = await rpcReq ( ws , "health" ) ;
expect ( health . ok ) . toBe ( false ) ;
expect ( health . error ? . message ) . toContain ( "missing scope" ) ;
ws . close ( ) ;
} ) ;
test ( "does not grant admin when scopes are omitted" , async ( ) = > {
const ws = await openWs ( port ) ;
const token =
typeof ( testState . gatewayAuth as { token? : unknown } | undefined ) ? . token === "string"
? ( ( testState . gatewayAuth as { token? : string } ) . token ? ? undefined )
: process . env . OPENCLAW_GATEWAY_TOKEN ;
expect ( typeof token ) . toBe ( "string" ) ;
const { loadOrCreateDeviceIdentity , publicKeyRawBase64UrlFromPem , signDevicePayload } =
await import ( "../infra/device-identity.js" ) ;
const identity = loadOrCreateDeviceIdentity ( ) ;
const signedAtMs = Date . now ( ) ;
const payload = buildDeviceAuthPayload ( {
deviceId : identity.deviceId ,
clientId : GATEWAY_CLIENT_NAMES.TEST ,
clientMode : GATEWAY_CLIENT_MODES.TEST ,
role : "operator" ,
scopes : [ ] ,
signedAtMs ,
token : token ? ? null ,
} ) ;
const device = {
id : identity.deviceId ,
publicKey : publicKeyRawBase64UrlFromPem ( identity . publicKeyPem ) ,
signature : signDevicePayload ( identity . privateKeyPem , payload ) ,
signedAt : signedAtMs ,
} ;
ws . send (
JSON . stringify ( {
type : "req" ,
id : "c-no-scopes" ,
method : "connect" ,
params : {
minProtocol : PROTOCOL_VERSION ,
maxProtocol : PROTOCOL_VERSION ,
client : {
id : GATEWAY_CLIENT_NAMES.TEST ,
version : "1.0.0" ,
platform : "test" ,
mode : GATEWAY_CLIENT_MODES.TEST ,
} ,
caps : [ ] ,
role : "operator" ,
auth : token ? { token } : undefined ,
device ,
} ,
} ) ,
) ;
const connectRes = await onceMessage < { ok : boolean } > ( ws , ( o ) = > {
if ( ! o || typeof o !== "object" || Array . isArray ( o ) ) {
return false ;
}
const rec = o as Record < string , unknown > ;
return rec . type === "res" && rec . id === "c-no-scopes" ;
} ) ;
expect ( connectRes . ok ) . toBe ( true ) ;
const health = await rpcReq ( ws , "health" ) ;
expect ( health . ok ) . toBe ( false ) ;
expect ( health . error ? . message ) . toContain ( "missing scope" ) ;
ws . close ( ) ;
} ) ;
test ( "rejects device signature when scopes are omitted but signed with admin" , async ( ) = > {
const ws = await openWs ( port ) ;
const token =
typeof ( testState . gatewayAuth as { token? : unknown } | undefined ) ? . token === "string"
? ( ( testState . gatewayAuth as { token? : string } ) . token ? ? undefined )
: process . env . OPENCLAW_GATEWAY_TOKEN ;
expect ( typeof token ) . toBe ( "string" ) ;
const { loadOrCreateDeviceIdentity , publicKeyRawBase64UrlFromPem , signDevicePayload } =
await import ( "../infra/device-identity.js" ) ;
const identity = loadOrCreateDeviceIdentity ( ) ;
const signedAtMs = Date . now ( ) ;
const payload = buildDeviceAuthPayload ( {
deviceId : identity.deviceId ,
clientId : GATEWAY_CLIENT_NAMES.TEST ,
clientMode : GATEWAY_CLIENT_MODES.TEST ,
role : "operator" ,
scopes : [ "operator.admin" ] ,
signedAtMs ,
token : token ? ? null ,
} ) ;
const device = {
id : identity.deviceId ,
publicKey : publicKeyRawBase64UrlFromPem ( identity . publicKeyPem ) ,
signature : signDevicePayload ( identity . privateKeyPem , payload ) ,
signedAt : signedAtMs ,
} ;
ws . send (
JSON . stringify ( {
type : "req" ,
id : "c-no-scopes-signed-admin" ,
method : "connect" ,
params : {
minProtocol : PROTOCOL_VERSION ,
maxProtocol : PROTOCOL_VERSION ,
client : {
id : GATEWAY_CLIENT_NAMES.TEST ,
version : "1.0.0" ,
platform : "test" ,
mode : GATEWAY_CLIENT_MODES.TEST ,
} ,
caps : [ ] ,
role : "operator" ,
auth : token ? { token } : undefined ,
device ,
} ,
} ) ,
) ;
const connectRes = await onceMessage < { ok : boolean ; error ? : { message? : string } } > (
ws ,
( o ) = > {
if ( ! o || typeof o !== "object" || Array . isArray ( o ) ) {
return false ;
}
const rec = o as Record < string , unknown > ;
return rec . type === "res" && rec . id === "c-no-scopes-signed-admin" ;
} ,
) ;
expect ( connectRes . ok ) . toBe ( false ) ;
expect ( connectRes . error ? . message ? ? "" ) . toContain ( "device signature invalid" ) ;
await new Promise < void > ( ( resolve ) = > ws . once ( "close" , ( ) = > resolve ( ) ) ) ;
} ) ;
test ( "sends connect challenge on open" , async ( ) = > {
const ws = new WebSocket ( ` ws://127.0.0.1: ${ port } ` ) ;
const evtPromise = onceMessage < { payload? : unknown } > (
@ -261,7 +406,7 @@ describe("gateway server auth/connect", () => {
} ) ;
test ( "returns control ui hint when token is missing" , async ( ) = > {
const ws = await openWs ( port );
const ws = await openWs ( port , { origin : originForPort ( port ) } );
const res = await connectReq ( ws , {
skipDefaultAuth : true ,
client : {
@ -277,7 +422,7 @@ describe("gateway server auth/connect", () => {
} ) ;
test ( "rejects control ui without device identity by default" , async ( ) = > {
const ws = await openWs ( port );
const ws = await openWs ( port , { origin : originForPort ( port ) } );
const res = await connectReq ( ws , {
token : "secret" ,
device : null ,
@ -334,7 +479,9 @@ describe("gateway server auth/connect", () => {
test ( "allows control ui without device identity when insecure auth is enabled" , async ( ) = > {
testState . gatewayControlUi = { allowInsecureAuth : true } ;
const { server , ws , prevToken } = await startServerWithClient ( "secret" ) ;
const { server , ws , prevToken } = await startServerWithClient ( "secret" , {
wsHeaders : { origin : "http://127.0.0.1" } ,
} ) ;
const res = await connectReq ( ws , {
token : "secret" ,
device : null ,
@ -370,7 +517,10 @@ describe("gateway server auth/connect", () => {
const port = await getFreePort ( ) ;
const server = await startGatewayServer ( port ) ;
const ws = new WebSocket ( ` ws://127.0.0.1: ${ port } ` , {
headers : { "x-forwarded-for" : "203.0.113.10" } ,
headers : {
origin : "https://localhost" ,
"x-forwarded-for" : "203.0.113.10" ,
} ,
} ) ;
const challengePromise = onceMessage < { payload? : unknown } > (
ws ,
@ -383,13 +533,14 @@ describe("gateway server auth/connect", () => {
const { loadOrCreateDeviceIdentity , publicKeyRawBase64UrlFromPem , signDevicePayload } =
await import ( "../infra/device-identity.js" ) ;
const identity = loadOrCreateDeviceIdentity ( ) ;
const scopes = [ "operator.admin" , "operator.approvals" , "operator.pairing" ] ;
const signedAtMs = Date . now ( ) ;
const payload = buildDeviceAuthPayload ( {
deviceId : identity.deviceId ,
clientId : GATEWAY_CLIENT_NAMES.CONTROL_UI ,
clientMode : GATEWAY_CLIENT_MODES.WEBCHAT ,
role : "operator" ,
scopes : [ ] ,
scopes ,
signedAtMs ,
token : "secret" ,
nonce : String ( nonce ) ,
@ -403,6 +554,7 @@ describe("gateway server auth/connect", () => {
} ;
const res = await connectReq ( ws , {
token : "secret" ,
scopes ,
device ,
client : {
id : GATEWAY_CLIENT_NAMES.CONTROL_UI ,
@ -428,7 +580,7 @@ describe("gateway server auth/connect", () => {
process . env . OPENCLAW_GATEWAY_TOKEN = "secret" ;
const port = await getFreePort ( ) ;
const server = await startGatewayServer ( port ) ;
const ws = await openWs ( port );
const ws = await openWs ( port , { origin : originForPort ( port ) } );
const { loadOrCreateDeviceIdentity , publicKeyRawBase64UrlFromPem , signDevicePayload } =
await import ( "../infra/device-identity.js" ) ;
const identity = loadOrCreateDeviceIdentity ( ) ;