Skip to content

Security fix: Restrict WebSocket origins to prevent Cross-Site WebSocket Hijacking (CWE-942)#1160

Open
anushkasrvstv wants to merge 3 commits into
viru0909-dev:mainfrom
anushkasrvstv:fix/websocket-origin-validation-cwe-942
Open

Security fix: Restrict WebSocket origins to prevent Cross-Site WebSocket Hijacking (CWE-942)#1160
anushkasrvstv wants to merge 3 commits into
viru0909-dev:mainfrom
anushkasrvstv:fix/websocket-origin-validation-cwe-942

Conversation

@anushkasrvstv
Copy link
Copy Markdown

Pull Request

PR Description

Summary

This PR fixes a security vulnerability where WebSocket endpoints previously allowed unrestricted or wildcard origins (*), which could lead to Cross-Site WebSocket Hijacking (CWE-942).

It introduces strict origin validation to ensure only explicitly trusted frontend origins can establish WebSocket connections.

Security Fix

➤Removed wildcard "" origin usage from WebSocket configuration
➤Added strict validation for allowed origins in WebSocketConfig
➤Rejects:
▫Empty origins
▫Null values
▫Wildcard "
" origins
➤Prevents Cross-Site WebSocket Hijacking (CWE-942)

Changes

➤Introduced app.websocket.allowed-origins configuration property
➤Validated origins using getValidatedOrigins() method
➤Applied secure origins to:
▫/api/ws/notifications (raw WebSocket handler)
▫/api/ws/stomp (STOMP + SockJS endpoints)
➤Updated application.properties with configurable allowlist

Result

➤WebSocket connections are restricted to trusted origins only
➤Eliminates wildcard-based security risk
➤No breaking changes for valid configured environments
➤Backward compatible via environment variables (WEBSOCKET_ALLOWED_ORIGINS)

Issue Reference

Closes #1069

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 4, 2026

@anushkasrvstv is attempting to deploy a commit to the CodeBlooded's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 4, 2026

Hi @anushkasrvstv, thanks for contributing to Nyay Setu! 🎉

I have automatically:

  • 👤 Assigned this PR to you.
  • 🏷️ Applied the gssoc:approved label.

Our workflows will now analyze your changes to classify:

  • 📈 PR Difficulty: level:*
  • 🧩 PR Type: type:*
  • 🌟 PR Quality: quality:*

Tip

Ensure your PR description references the issue it resolves (e.g. Closes #123). This allows the bot to inherit any additional labels from that issue!

Happy coding! 🚀

@anushkasrvstv
Copy link
Copy Markdown
Author

Implemented the fix for WebSocket origin validation.
Waiting for review. Thank you!

Copy link
Copy Markdown
Owner

@viru0909-dev viru0909-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires Changes: The test suite failed. Please fix the failing tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: WebSocket endpoint allows connections from any origin, enabling cross-site WebSocket hijacking (CWE-942)

2 participants