Skip to content

Commit 3f24991

Browse files
committed
- Fix access to undefined request user for unauthenticated requests
- Redis lookup via custom `vcap` environment configuration
1 parent 03593a5 commit 3f24991

File tree

7 files changed

+37
-24
lines changed

7 files changed

+37
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8-
## Version 1.0.1 - 2024-06-xx
8+
## Version 1.0.1 - 2024-06-03
99

1010
### Fixed
1111

12-
- tbd
12+
- Fix access to undefined request user for unauthenticated requests
13+
- Redis lookup via custom `vcap` environment configuration
1314

1415
## Version 1.0.0 - 2024-05-03
1516

package-lock.json

Lines changed: 11 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@eslint/js": "9.4.0",
5555
"@sap/cds": "^7.9.2",
5656
"@sap/cds-dk": "^7.9.2",
57-
"@sap/xssec": "4.0.0",
57+
"@sap/xssec": "4.0.1",
5858
"@socket.io/redis-adapter": "^8.3.0",
5959
"@socket.io/redis-streams-adapter": "^0.2.2",
6060
"@types/express": "^4.17.21",
@@ -132,6 +132,21 @@
132132
"type": "boolean",
133133
"description": "Enable websocket adapter in local environment",
134134
"default": false
135+
},
136+
"vcap": {
137+
"type": "object",
138+
"description": "VCAP service environment",
139+
"properties": {
140+
"label": {
141+
"type": "string",
142+
"description": "VCAP service label"
143+
},
144+
"tag": {
145+
"type": "string",
146+
"description": "VCAP service tag"
147+
}
148+
},
149+
"additionalProperties": true
135150
}
136151
}
137152
}
@@ -145,7 +160,7 @@
145160
}
146161
}
147162
},
148-
"weboscket": {
163+
"websocket": {
149164
"kind": "ws"
150165
}
151166
}

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,15 @@ function deriveElements(event, data) {
351351
function deriveUser(event, data, headers, req) {
352352
if ((headers?.wsExcludeCurrentUser || headers?.excludeCurrentUser) !== undefined) {
353353
if (headers?.wsExcludeCurrentUser || headers?.excludeCurrentUser) {
354-
return req.context.user.id;
354+
return req.context.user?.id;
355355
}
356356
return;
357357
}
358358
let user =
359359
event["@websocket.user"] || event["@ws.user"] || event["@websocket.broadcast.user"] || event["@ws.broadcast.user"];
360360
switch (user) {
361361
case "excludeCurrent":
362-
return req.context.user.id;
362+
return req.context.user?.id;
363363
}
364364
if (event.elements) {
365365
for (const name in event.elements) {
@@ -371,7 +371,7 @@ function deriveUser(event, data, headers, req) {
371371
element["@ws.broadcast.user"];
372372
switch (user) {
373373
case "excludeCurrent":
374-
return data[name] ? req.context.user.id : undefined;
374+
return data[name] ? req.context.user?.id : undefined;
375375
}
376376
}
377377
}

src/redis/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const createClientBase = (options = {}) => {
5656
}
5757
let credentials;
5858
try {
59-
credentials = xsenv.serviceCredentials({ label: "redis-cache" });
59+
credentials = xsenv.serviceCredentials({ label: "redis-cache", ...cds.env.websocket?.adapter?.vcap });
6060
} catch (err) {
6161
LOG?.info(err.message);
6262
}

src/socket/socket.io.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SocketIOServer extends SocketServer {
3434
try {
3535
this.enforceAuth(socket);
3636
socket.tenant = socket.request.tenant;
37-
socket.user = socket.request.user.id;
37+
socket.user = socket.request.user?.id;
3838
socket.join(room({ tenant: socket.tenant }));
3939
socket.join(room({ tenant: socket.tenant, user: socket.user }));
4040
if (socket.request._query?.id) {

src/socket/ws.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class SocketWSServer extends SocketServer {
6666
try {
6767
this.enforceAuth(ws);
6868
ws.tenant = ws.request.tenant;
69-
ws.user = ws.request.user.id;
69+
ws.user = ws.request.user?.id;
7070
const facade = {
7171
service,
7272
socket: ws,

0 commit comments

Comments
 (0)