forked from apache/zeppelin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CSRF token to terminal interpreter for CSWSH
Apply synchronizer token pattern to terminal interpreter for CSWSH protection
- Loading branch information
Showing
10 changed files
with
153 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
shell/src/main/java/org/apache/zeppelin/shell/terminal/TerminalCsrfTokenManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.zeppelin.shell.terminal; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.apache.zeppelin.shell.terminal.websocket.TerminalSocket; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TerminalCsrfTokenManager { | ||
private static TerminalCsrfTokenManager instance; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(TerminalSocket.class); | ||
|
||
private final Map<String, String> csrfTokens = new ConcurrentHashMap<>(); | ||
|
||
|
||
public static synchronized TerminalCsrfTokenManager getInstance(){ | ||
if (instance == null) { | ||
instance = new TerminalCsrfTokenManager(); | ||
} | ||
return instance; | ||
} | ||
|
||
public String generateToken(String noteId, String paragraphId) { | ||
String key = formatId(noteId, paragraphId); | ||
return csrfTokens.computeIfAbsent(key, k -> UUID.randomUUID().toString()); | ||
} | ||
|
||
public boolean validateToken(String noteId, String paragraphId, String token) { | ||
if (token == null) { | ||
LOGGER.warn("Received null CSRF token for validation"); | ||
return false; | ||
} | ||
|
||
String storedToken = csrfTokens.get(formatId(noteId, paragraphId)); | ||
return Objects.equals(storedToken, token); | ||
} | ||
|
||
private String formatId(String noteId, String paragraphId) { | ||
return noteId + "@" + paragraphId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
shell/src/main/java/org/apache/zeppelin/shell/terminal/TerminalServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.zeppelin.shell.terminal; | ||
|
||
import com.google.common.io.Resources; | ||
import com.hubspot.jinjava.Jinjava; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TerminalServlet extends HttpServlet { | ||
|
||
private Jinjava jinjava = new Jinjava(); | ||
|
||
@Override | ||
protected void doGet( | ||
HttpServletRequest request, | ||
HttpServletResponse response | ||
) | ||
throws ServletException, IOException { | ||
URL urlTemplate = Resources.getResource("ui_templates/terminal-ui.jinja"); | ||
String template = Resources.toString(urlTemplate, StandardCharsets.UTF_8); | ||
|
||
String noteId = request.getParameter("noteId"); | ||
String paragraphId = request.getParameter("paragraphId"); | ||
|
||
String csrfToken = TerminalCsrfTokenManager.getInstance().generateToken(noteId, paragraphId); | ||
|
||
Map<String, Object> context = new HashMap<>(); | ||
context.put("CSRF_TOKEN", csrfToken); | ||
|
||
String renderedTemplate = jinjava.render(template, context); | ||
|
||
response.setContentType("text/html; charset=UTF-8"); | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.getWriter().write(renderedTemplate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-#} | ||
<html> | ||
<header> | ||
<meta name="csrf-token" content="{{ CSRF_TOKEN }}"> | ||
<style> | ||
* { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
html, body { | ||
height: 100%; | ||
} | ||
</style> | ||
<script src="/hterm_all.js"></script> | ||
<script src="/index.js"></script> | ||
</header> | ||
<body> | ||
<div id="terminal" style="position:relative; width:100%; height:100%"></div> | ||
</body> | ||
</html> |