Skip to content

Commit b375796

Browse files
Updated broadcasting configuration to ensure authentication requests are directed to the Laravel app with cookies, reducing CORS-related 403 errors.
- Added logic to handle user ID extraction from private channels, preventing subscription to channels of other users. - Improved error handling for private channel subscriptions when no user is authenticated.
1 parent da6323b commit b375796

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

resources/js/bootstrap.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,16 @@ if (userID) {
340340
}
341341

342342
if (window.Processmaker && window.Processmaker.broadcasting) {
343-
const config = window.Processmaker.broadcasting;
343+
const config = { ...window.Processmaker.broadcasting };
344+
345+
// Ensure auth request goes to Laravel app with cookies (reduces 403 from CORS/cookie issues)
346+
if (!config.authEndpoint) {
347+
config.authEndpoint = `${window.location.origin}/broadcasting/auth`;
348+
}
349+
config.auth = config.auth || {};
350+
if (config.auth.withCredentials === undefined) {
351+
config.auth.withCredentials = true;
352+
}
344353

345354
if (config.broadcaster == "pusher") {
346355
window.Pusher = require("pusher-js");
@@ -349,21 +358,34 @@ if (window.Processmaker && window.Processmaker.broadcasting) {
349358

350359
window.Echo = new TenantAwareEcho(config);
351360

352-
// Option 3: Prevent private channel subscription when no user (avoids 403 on /broadcasting/auth)
361+
// Prevent private channel subscription when no user or wrong user (avoids 403 on /broadcasting/auth)
353362
const noOpChannel = {
354363
listen: () => noOpChannel,
355364
notification: () => noOpChannel,
356365
stopListening: () => noOpChannel,
357366
listenForWhisper: () => noOpChannel,
367+
error: () => noOpChannel,
358368
};
359369
const originalPrivate = window.Echo.private.bind(window.Echo);
360370
const getUserId = () =>
361371
window.Processmaker?.userId ||
362372
window.ProcessMaker?.user?.id ||
363373
document.head.querySelector('meta[name="user-id"]')?.content;
364374

375+
// Extract user id from ProcessMaker.Models.User.{id} channel (with optional tenant prefix)
376+
const getUserIdFromChannel = (ch) => {
377+
const match = ch.match(/ProcessMaker\.Models\.User\.(\d+)/);
378+
return match ? match[1] : null;
379+
};
380+
365381
window.Echo.private = (channel, ...args) => {
366-
if (!getUserId()) {
382+
const currentUserId = String(getUserId() || "");
383+
if (!currentUserId) {
384+
return noOpChannel;
385+
}
386+
// Block subscription to another user's channel (would always 403)
387+
const channelUserId = getUserIdFromChannel(channel);
388+
if (channelUserId && channelUserId !== currentUserId) {
367389
return noOpChannel;
368390
}
369391
return originalPrivate(channel, ...args);

0 commit comments

Comments
 (0)