Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions public/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = protocol + '//' + window.location.host;
let ws;
function connectWS() {
const wsAuthUrl = wsUrl + '?key=' + encodeURIComponent(API_KEY || '');
ws = new WebSocket(wsAuthUrl);
// Browser WebSocket connections automatically include the dashboard's
// session cookie, so avoid echoing the long-lived access key into the URL.
ws = new WebSocket(wsUrl);
ws.onopen = () => console.log('WS Connected');
ws.onclose = () => setTimeout(connectWS, 3000);
ws.onmessage = (event) => {
Expand Down
6 changes: 4 additions & 2 deletions tests/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ describe('WebSocket Authentication', () => {
ws.on('error', done);
});

test('correct x-claw-key in query string → connected (receives heartbeat)', done => {
const ws = new WebSocket(`ws://127.0.0.1:${port}?key=testkey123`);
test('correct x-claw-key header → connected (receives heartbeat)', done => {
const ws = new WebSocket(`ws://127.0.0.1:${port}`, {
headers: { 'x-claw-key': 'testkey123' },
});
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
expect(msg).toHaveProperty('type', 'heartbeat');
Expand Down