Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZEPPELIN-6142] Apply synchronizer token pattern to terminal interpreter for CSWSH protection #4892

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

seung-00
Copy link
Contributor

@seung-00 seung-00 commented Nov 3, 2024

What is this PR for?

This PR applies synchronizer token pattern to terminal interpreter for CSWSH protection. The terminal interpreter is reported with a CSWSH(Cross-Site WebSocket Hijacking) vulnerability. An earlier PR partially addressed this issue by implementing an origin check. However, this measure does not provide full protection, as it can't prevent attacks from scripts running within the same origin.

To enhance security, I implemented the synchronizer token pattern as an additional layer of protection. I modified the terminal HTML to be rendered with a Jinja template, allowing a CSRF token to be injected. This CSRF token is then validated on receiving the first WebSocket message, providing robust protection against CSWSH attacks.

References:

What type of PR is it?

Improvement

Todos

What is the Jira issue?

https://issues.apache.org/jira/browse/ZEPPELIN-6142

How should this be tested?

  1. After removing the origin check(by making the checkOrigin method always return true), connect to the terminal WebSocket and send a message without a valid CSRF token.
  2. Check that the connection is promptly closed and that an error is logged on the server.

Screenshots (if appropriate)

Questions:

  • Does the license files need to update? No
  • Is there breaking changes for older versions? No
  • Does this needs documentation? No

Apply synchronizer token pattern to terminal interpreter for CSWSH protection
@seung-00 seung-00 changed the title [ZEPPELIN-6142] Add CSRF token to terminal interpreter for CSWSH [ZEPPELIN-6142] Apply synchronizer token pattern to terminal interpreter for CSWSH protection Nov 3, 2024

private static final Logger LOGGER = LoggerFactory.getLogger(TerminalSocket.class);

private final Map<String, String> csrfTokens = new ConcurrentHashMap<>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CSRF tokens are managed per note and paragraph.


// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
ClassLoader clazz = TerminalThread.class.getClassLoader();
URL url = clazz.getResource("html");
URL url = clazz.getResource("static");
Copy link
Contributor Author

@seung-00 seung-00 Nov 3, 2024

Choose a reason for hiding this comment

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

Renamed html directory to static, as index.html is now rendered by Jinja and has been moved to an ui_templates directory.

HttpServletRequest request,
HttpServletResponse response
)
throws ServletException, IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it OK to remove ServletException here? My IDE shows that it is never thrown in this method.

Copy link
Contributor Author

@seung-00 seung-00 Nov 4, 2024

Choose a reason for hiding this comment

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

I agree with you. That said, other classes that extend from HttpServlet, like PrometheusServlet and IndexHtmlServlet, don’t use these exceptions but keep them explicitly.
IMO, for consistency, it’s better to keep them in this PR too. If you plan to remove them, it’d be better to handle that in a separate PR.

String csrfToken = TerminalCsrfTokenManager.getInstance().generateToken(noteId, paragraphId);

Map<String, Object> context = new HashMap<>();
context.put("CSRF_TOKEN", csrfToken);
Copy link
Contributor

@tbonelee tbonelee Nov 4, 2024

Choose a reason for hiding this comment

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

From my understanding, this handler is publicly accesible. When testing in my local environment, I noticed that, since it includes the CSRF token, anyone who knows the mapped address for terminal-ui could potentially access the terminal.
It might be worth considering receiving the CSRF token exclusively through the channel between NotebookServer and the WebSocket (e.g., within HTML elements rendered by terminal-dashboard.jinja).
This way, even if someone discovers the terminal UI address, they wouldn't have direct access to the CSRF token.

Copy link
Contributor Author

@seung-00 seung-00 Nov 4, 2024

Choose a reason for hiding this comment

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

You're right. It lacks authentication, so it’s still vulnerable to token theft. But other HTML elements, like terminal-dashboard, aren’t accessible within the iframe for terminal-ui.

@seung-00
Copy link
Contributor Author

seung-00 commented Nov 4, 2024

@tbonelee
Thank you for the review. I’m going to give some more thought to the CSRF token theft issue that you mentioned, and add a commit for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants