@@ -3,6 +3,10 @@ import { mkdir } from "node:fs/promises";
33import path from "node:path" ;
44import { chromium , type Browser , type Page } from "playwright" ;
55import { afterAll , beforeAll , describe , expect , it } from "vitest" ;
6+ import {
7+ buildControlUiCspHeader ,
8+ computeInlineScriptHashes ,
9+ } from "../../../src/gateway/control-ui-csp.ts" ;
610import {
711 canRunPlaywrightChromium ,
812 installMockGateway ,
@@ -148,6 +152,7 @@ const testPresenceUsers = [
148152 id : testProfile . id ,
149153 name : testProfile . displayName ,
150154 email : testProfile . emails [ 0 ] ,
155+ avatarUrl : `/api/users/${ testProfile . id } /avatar?v=${ testProfile . updatedAt } ` ,
151156 } ,
152157] ;
153158
@@ -215,9 +220,31 @@ describeControlUiE2e("Control UI profile page mocked Gateway E2E", () => {
215220 }
216221 } ) ;
217222
218- it ( "renders the gateway avatar route in the profile preview" , async ( ) => {
219- const context = await browser . newContext ( ) ;
223+ it ( "shares one authenticated avatar between the sidebar and profile preview" , async ( ) => {
224+ if ( captureUiProof ) {
225+ await mkdir ( proofDir , { recursive : true } ) ;
226+ }
227+ const context = await browser . newContext ( {
228+ ...( captureUiProof
229+ ? { recordVideo : { dir : proofDir , size : { width : 1280 , height : 800 } } }
230+ : { } ) ,
231+ viewport : { width : 1280 , height : 800 } ,
232+ } ) ;
220233 const page = await context . newPage ( ) ;
234+ await page . route ( `${ server . baseUrl } settings/profile` , async ( route ) => {
235+ const response = await route . fetch ( ) ;
236+ const body = await response . text ( ) ;
237+ await route . fulfill ( {
238+ body,
239+ headers : {
240+ ...response . headers ( ) ,
241+ "content-security-policy" : buildControlUiCspHeader ( {
242+ inlineScriptHashes : computeInlineScriptHashes ( body ) ,
243+ } ) ,
244+ } ,
245+ response,
246+ } ) ;
247+ } ) ;
221248 const gatewayUrl = server . baseUrl . replace ( / ^ h t t p / u, "ws" ) . replace ( / \/ $ / u, "" ) ;
222249 await page . addInitScript ( ( sameOriginGatewayUrl ) => {
223250 (
@@ -229,16 +256,20 @@ describeControlUiE2e("Control UI profile page mocked Gateway E2E", () => {
229256 token : "test" ,
230257 } ;
231258 } , gatewayUrl ) ;
232- const avatarRequests : string [ ] = [ ] ;
233- // The gateway serves the avatar (uploaded first, Gravatar fallback second)
234- // behind its own same-origin route; the Control UI renders only that route,
235- // so the preview never requests gravatar.com directly — the Control UI CSP
236- // (img-src 'self') would block it.
259+ const avatarRequests : Array < { authorization ?: string ; url : string } > = [ ] ;
260+ // Profile images require the same bearer auth as gateway RPCs. One cached
261+ // blob keeps the sidebar and preview inside the Control UI's image CSP.
237262 await page . route ( `**/api/users/${ testProfile . id } /avatar*` , async ( route ) => {
238- avatarRequests . push ( route . request ( ) . url ( ) ) ;
263+ avatarRequests . push ( {
264+ authorization : route . request ( ) . headers ( ) . authorization ,
265+ url : route . request ( ) . url ( ) ,
266+ } ) ;
239267 await route . fulfill ( {
240- body : '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>' ,
241- contentType : "image/svg+xml" ,
268+ body : Buffer . from (
269+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/a6kAAAAASUVORK5CYII=" ,
270+ "base64" ,
271+ ) ,
272+ contentType : "image/png" ,
242273 status : 200 ,
243274 } ) ;
244275 } ) ;
@@ -254,19 +285,49 @@ describeControlUiE2e("Control UI profile page mocked Gateway E2E", () => {
254285 try {
255286 const response = await page . goto ( `${ server . baseUrl } settings/profile` ) ;
256287 expect ( response ?. status ( ) ) . toBe ( 200 ) ;
288+ expect ( response ?. headers ( ) [ "content-security-policy" ] ) . toContain (
289+ "img-src 'self' data: blob:" ,
290+ ) ;
257291
258292 const profileAvatar = page . locator ( "#settings-profile-identity openclaw-viewer-avatar img" ) ;
259293 await profileAvatar . waitFor ( { timeout : 10_000 } ) ;
260- // profile-page derives the src from userProfileAvatarUrl(id, updatedAt);
261- // the gateway origin may absolutize it, so match the canonical path suffix.
262- expect ( await profileAvatar . getAttribute ( "src" ) ) . toContain (
263- `/api/users/${ testProfile . id } /avatar?v=2` ,
264- ) ;
294+ const imageUrl = await profileAvatar . getAttribute ( "src" ) ;
295+ expect ( imageUrl ) . toMatch ( / ^ b l o b : / u) ;
265296 await expect
266- . poll ( ( ) =>
267- avatarRequests . some ( ( url ) => url . includes ( `/api/users/${ testProfile . id } /avatar` ) ) ,
268- )
269- . toBe ( true ) ;
297+ . poll ( ( ) => profileAvatar . evaluate ( ( image ) => ( image as HTMLImageElement ) . naturalWidth ) )
298+ . toBe ( 1 ) ;
299+ expect (
300+ await profileAvatar . evaluate ( ( image ) =>
301+ image . closest ( ".viewer-avatar" ) ?. classList . contains ( "is-fallback" ) ,
302+ ) ,
303+ ) . toBe ( false ) ;
304+ if ( captureUiProof ) {
305+ await page . screenshot ( {
306+ animations : "disabled" ,
307+ path : path . join ( proofDir , "03-authenticated-profile-avatar.png" ) ,
308+ } ) ;
309+ }
310+
311+ await page . getByRole ( "button" , { name : "Back to app" } ) . click ( ) ;
312+ const sidebarAvatar = page . locator ( ".sidebar-identity-card openclaw-viewer-avatar img" ) ;
313+ await sidebarAvatar . waitFor ( { timeout : 10_000 } ) ;
314+ await expect . poll ( ( ) => avatarRequests . length ) . toBe ( 1 ) ;
315+ expect ( avatarRequests [ 0 ] ) . toEqual ( {
316+ authorization : "Bearer e2e-device-token" ,
317+ url : expect . stringContaining ( `/api/users/${ testProfile . id } /avatar?v=2` ) ,
318+ } ) ;
319+ expect ( await sidebarAvatar . getAttribute ( "src" ) ) . toBe ( imageUrl ) ;
320+ expect (
321+ await sidebarAvatar . evaluate ( ( image ) =>
322+ image . closest ( ".viewer-avatar" ) ?. classList . contains ( "is-fallback" ) ,
323+ ) ,
324+ ) . toBe ( false ) ;
325+ if ( captureUiProof ) {
326+ await page . screenshot ( {
327+ animations : "disabled" ,
328+ path : path . join ( proofDir , "03-authenticated-user-avatar-cache.png" ) ,
329+ } ) ;
330+ }
270331 } finally {
271332 await context . close ( ) ;
272333 }
0 commit comments