-
Notifications
You must be signed in to change notification settings - Fork 160
Separate submitted and response salts in UI filters #167
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * 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. For additional information regarding | ||
| * copyright in this work, please see the NOTICE file in the top level | ||
| * directory of this distribution. | ||
| */ | ||
|
|
||
| package org.apache.roller.weblogger.ui.core.filters; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.Objects; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import org.apache.roller.weblogger.ui.core.RollerSession; | ||
| import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache; | ||
|
|
||
| /** | ||
| * Shared validation for salts submitted by UI forms. | ||
| */ | ||
| public final class SaltValidator { | ||
|
|
||
| private static final String MULTIPART_FORM_DATA = "multipart/form-data"; | ||
|
|
||
| public static final String VALIDATED_REQUEST_ATTRIBUTE = | ||
| SaltValidator.class.getName() + ".validated"; | ||
|
|
||
| private SaltValidator() { | ||
| } | ||
|
|
||
| /** | ||
| * Validates and consumes the salt submitted as a request parameter. | ||
| * | ||
| * @param request current request | ||
| * @return true when no Roller session is present or the submitted salt is valid | ||
| */ | ||
| public static boolean consumeSubmittedSalt(HttpServletRequest request) { | ||
| RollerSession rollerSession = RollerSession.getRollerSession(request); | ||
| if (rollerSession == null) { | ||
| return true; | ||
| } | ||
|
|
||
| String userId = rollerSession.getAuthenticatedUser() != null | ||
| ? rollerSession.getAuthenticatedUser().getId() : ""; | ||
| String salt = request.getParameter("salt"); | ||
| if (salt == null) { | ||
| return false; | ||
| } | ||
|
|
||
| SaltCache saltCache = SaltCache.getInstance(); | ||
| synchronized (saltCache) { | ||
| if (!Objects.equals(saltCache.get(salt), userId)) { | ||
|
Contributor
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. Now that this is the only path, |
||
| return false; | ||
| } | ||
| saltCache.remove(salt); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Validates and consumes the submitted salt or rejects the request. | ||
| * | ||
| * @param request current request | ||
| * @throws ServletException when the submitted salt is missing or invalid | ||
| */ | ||
| public static void requireSubmittedSalt(HttpServletRequest request) | ||
| throws ServletException { | ||
| if (!consumeSubmittedSalt(request)) { | ||
| throw new ServletException("Security Violation"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns true for a multipart form POST, which Struts parses after the | ||
| * servlet filters have run. | ||
| * | ||
| * @param request current request | ||
| * @return true for multipart/form-data POST requests | ||
| */ | ||
| public static boolean isMultipartFormPost(HttpServletRequest request) { | ||
| if (!"POST".equalsIgnoreCase(request.getMethod())) { | ||
| return false; | ||
| } | ||
|
|
||
| String contentType = request.getContentType(); | ||
| if (contentType == null) { | ||
| return false; | ||
| } | ||
|
|
||
| int parameterStart = contentType.indexOf(';'); | ||
| String mediaType = parameterStart >= 0 | ||
| ? contentType.substring(0, parameterStart) : contentType; | ||
| return MULTIPART_FORM_DATA.equals(mediaType.trim().toLowerCase(Locale.ENGLISH)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * 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. For additional information regarding | ||
| * copyright in this work, please see the NOTICE file in the top level | ||
| * directory of this distribution. | ||
| */ | ||
|
|
||
| package org.apache.roller.weblogger.ui.struts2.util; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.apache.roller.weblogger.ui.core.filters.SaltValidator; | ||
| import org.apache.struts2.StrutsStatics; | ||
| import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; | ||
|
|
||
| import com.opensymphony.xwork2.ActionContext; | ||
| import com.opensymphony.xwork2.ActionInvocation; | ||
| import com.opensymphony.xwork2.interceptor.AbstractInterceptor; | ||
|
|
||
| /** | ||
| * Validates salts after Struts has wrapped a multipart form request. | ||
| */ | ||
| public class ValidateSaltInterceptor extends AbstractInterceptor implements StrutsStatics { | ||
|
|
||
| private static final long serialVersionUID = 2446434402795510394L; | ||
| private static final Log log = LogFactory.getLog(ValidateSaltInterceptor.class); | ||
|
|
||
| @Override | ||
| public String intercept(ActionInvocation invocation) throws Exception { | ||
| ActionContext context = invocation.getInvocationContext(); | ||
| HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); | ||
|
|
||
| if (SaltValidator.isMultipartFormPost(request) | ||
| && !Boolean.TRUE.equals(request.getAttribute( | ||
| SaltValidator.VALIDATED_REQUEST_ATTRIBUTE))) { | ||
| if (request instanceof MultiPartRequestWrapper | ||
| && ((MultiPartRequestWrapper) request).hasErrors()) { | ||
| return invocation.invoke(); | ||
| } | ||
|
|
||
| try { | ||
| SaltValidator.requireSubmittedSalt(request); | ||
| } catch (ServletException e) { | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("Valid salt value not found on multipart POST to URL : " | ||
| + request.getServletPath()); | ||
| } | ||
| throw e; | ||
| } | ||
| request.setAttribute( | ||
| SaltValidator.VALIDATED_REQUEST_ATTRIBUTE, Boolean.TRUE); | ||
| } | ||
|
|
||
| return invocation.invoke(); | ||
| } | ||
| } |
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.
#165 (comment)