-
Notifications
You must be signed in to change notification settings - Fork 714
Remove csrfguard register from web.xml #4533
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
Draft
Zeta201
wants to merge
2
commits into
wso2:4.12.x
Choose a base branch
from
Zeta201:csrfguard-fix
base: 4.12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
core/org.wso2.carbon.ui/src/main/java/org/wso2/carbon/ui/CsrfJavaScriptServletProxy.java
This file contains hidden or 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,73 @@ | ||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||
| * Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * WSO2 LLC. 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. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| package org.wso2.carbon.ui; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import javax.servlet.ServletConfig; | ||||||||||||||||||||||||||
| import javax.servlet.ServletException; | ||||||||||||||||||||||||||
| import javax.servlet.http.HttpServlet; | ||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletResponse; | ||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Proxy servlet for the OWASP CSRFGuard JavaScriptServlet. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * <p>The third-party {@code JavaScriptServlet} overrides {@code init(ServletConfig)} without | ||||||||||||||||||||||||||
| * calling {@code super.init(config)}, so {@code GenericServlet.getServletConfig()} returns null. | ||||||||||||||||||||||||||
| * Equinox's {@code HttpServletRequestWrapperImpl.getSession()} calls | ||||||||||||||||||||||||||
| * {@code registeredServlet.getServletConfig().getServletContext()} on the servlet registered with | ||||||||||||||||||||||||||
| * the OSGi HTTP Whiteboard — resulting in a NullPointerException on every request to the CSRF JS | ||||||||||||||||||||||||||
| * endpoint.</p> | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * <p>This proxy is registered with OSGi instead. It correctly calls {@code super.init(config)}, | ||||||||||||||||||||||||||
| * so {@code getServletConfig()} is never null from Equinox's perspective. It then calls | ||||||||||||||||||||||||||
| * {@code delegate.init(config)} which sets the static {@code servletConfig} field inside | ||||||||||||||||||||||||||
| * {@code JavaScriptServlet} needed for its own logic (e.g. resolving context-path init params). | ||||||||||||||||||||||||||
| * All requests are forwarded to the delegate unchanged.</p> | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| public class CsrfJavaScriptServletProxy extends HttpServlet { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private static final long serialVersionUID = 1L; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private final HttpServlet delegate; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public CsrfJavaScriptServletProxy(HttpServlet delegate) { | ||||||||||||||||||||||||||
| this.delegate = delegate; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void init(ServletConfig config) throws ServletException { | ||||||||||||||||||||||||||
| // Must call super.init() so GenericServlet stores the config in its instance field. | ||||||||||||||||||||||||||
| // Equinox's HttpServletRequestWrapperImpl resolves the session via | ||||||||||||||||||||||||||
| // getServletConfig().getServletContext() on this (the registered) servlet. | ||||||||||||||||||||||||||
| super.init(config); | ||||||||||||||||||||||||||
| // Also initialise the delegate so it sets its static servletConfig field. | ||||||||||||||||||||||||||
| delegate.init(config); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| protected void service(HttpServletRequest request, HttpServletResponse response) | ||||||||||||||||||||||||||
| throws ServletException, IOException { | ||||||||||||||||||||||||||
| delegate.service(request, response); | ||||||||||||||||||||||||||
|
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log Improvement Suggestion No: 2
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void destroy() { | ||||||||||||||||||||||||||
| delegate.destroy(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
This file contains hidden or 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,6 +56,7 @@ | |||||||||
| import org.wso2.carbon.ui.CarbonUIAuthenticator; | ||||||||||
| import org.wso2.carbon.ui.CarbonUIUtil; | ||||||||||
| import org.wso2.carbon.ui.DefaultCarbonAuthenticator; | ||||||||||
| import org.wso2.carbon.ui.CsrfJavaScriptServletProxy; | ||||||||||
| import org.wso2.carbon.ui.TenantAwareCsrfJsFilter; | ||||||||||
| import org.wso2.carbon.ui.TextJavascriptHandler; | ||||||||||
| import org.wso2.carbon.ui.TilesJspServlet; | ||||||||||
|
|
@@ -84,6 +85,8 @@ | |||||||||
| import javax.servlet.Servlet; | ||||||||||
| import javax.servlet.ServletContextListener; | ||||||||||
| import javax.servlet.Filter; | ||||||||||
| import javax.servlet.http.HttpServlet; | ||||||||||
|
|
||||||||||
| @Component(name = "core.ui.dscomponent", immediate = true) | ||||||||||
| public class CarbonUIServiceComponent { | ||||||||||
|
|
||||||||||
|
|
@@ -318,7 +321,12 @@ public void start(BundleContext context) throws Exception { | |||||||||
|
|
||||||||||
| // Register CSRFGuard listeners and filter using HTTP Whiteboard pattern | ||||||||||
| try { | ||||||||||
| // Register CsrfGuardHttpSessionListener - generates per-session CSRF tokens | ||||||||||
| // Register CsrfGuardHttpSessionListener - generates per-session CSRF tokens. | ||||||||||
| // Must be registered via OSGi (not web.xml) so that the token is stored in the | ||||||||||
| // HttpSessionAdaptor-wrapped (namespaced) session. JSP tags and CsrfGuardFilter | ||||||||||
| // both access the session through the Equinox-wrapped request, so the token must | ||||||||||
| // be at the namespaced key. The Tomcat-level CsrfGuardFilter in web.xml has been | ||||||||||
| // removed to avoid a dual-session token conflict. | ||||||||||
| Class<?> sessionListenerClass = Class.forName("org.owasp.csrfguard.CsrfGuardHttpSessionListener"); | ||||||||||
| Object sessionListener = sessionListenerClass.newInstance(); | ||||||||||
|
|
||||||||||
|
|
@@ -333,15 +341,31 @@ public void start(BundleContext context) throws Exception { | |||||||||
| Class<?> csrfGuardFilterClass = Class.forName("org.owasp.csrfguard.CsrfGuardFilter"); | ||||||||||
| Filter csrfGuardFilter = (Filter) csrfGuardFilterClass.newInstance(); | ||||||||||
|
|
||||||||||
| Dictionary<String, String> csrfFilterProps = new Hashtable<>(); | ||||||||||
| csrfFilterProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/carbon/*"); | ||||||||||
| Dictionary<String, Object> csrfFilterProps = new Hashtable<>(); | ||||||||||
| csrfFilterProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/*"); | ||||||||||
| csrfFilterProps.put("osgi.http.whiteboard.context.select", "(osgi.http.whiteboard.context.name=carbonContext)"); | ||||||||||
| csrfFilterProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_NAME, "CsrfGuardFilter"); | ||||||||||
| csrfFilterProps.put("service.ranking", "100"); | ||||||||||
| csrfFilterProps.put(Constants.SERVICE_RANKING, Integer.valueOf(100)); | ||||||||||
|
|
||||||||||
| context.registerService(Filter.class, csrfGuardFilter, csrfFilterProps); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log Improvement Suggestion No: 3
Suggested change
|
||||||||||
|
|
||||||||||
| // Register TenantAwareCsrfJsFilter filter. | ||||||||||
| // Register JavaScriptServlet directly as an OSGi HTTP Whiteboard servlet for the | ||||||||||
| // super-tenant path. This ensures the token is read from the Equinox-namespaced | ||||||||||
| // (adapted) session, consistent with how CsrfGuardHttpSessionListener stores it. | ||||||||||
| Class<?> jsServletClass = Class.forName("org.owasp.csrfguard.servlet.JavaScriptServlet"); | ||||||||||
| HttpServlet jsServlet = (HttpServlet) jsServletClass.newInstance(); | ||||||||||
| // Wrap with CsrfJavaScriptServletProxy so that the OSGi-registered servlet calls | ||||||||||
| // super.init(config), ensuring getServletConfig() is never null in Equinox's | ||||||||||
| // HttpServletRequestWrapperImpl.getSession() without modifying the third-party library. | ||||||||||
| Servlet jsServletProxy = new CsrfJavaScriptServletProxy(jsServlet); | ||||||||||
| Dictionary<String, String> jsServletProps = new Hashtable<>(); | ||||||||||
| jsServletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/carbon/admin/js/csrfPrevention.js"); | ||||||||||
| jsServletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "JavaScriptServlet"); | ||||||||||
| jsServletProps.put("osgi.http.whiteboard.context.select", "(osgi.http.whiteboard.context.name=carbonContext)"); | ||||||||||
| context.registerService(Servlet.class, jsServletProxy, jsServletProps); | ||||||||||
|
|
||||||||||
| // Register TenantAwareCsrfJsFilter for tenant paths only (/t/*). | ||||||||||
| // It rewrites contextPath/servletPath for tenant URLs and delegates to JavaScriptServlet. | ||||||||||
| Filter csrfJsFilterCarbon = new TenantAwareCsrfJsFilter(); | ||||||||||
| Dictionary<String, Object> csrfJsFilterCarbonProps = new Hashtable<>(); | ||||||||||
| csrfJsFilterCarbonProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/t/*"); | ||||||||||
|
|
||||||||||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log Improvement Suggestion No: 1