mraible commented on code in PR #167: URL: https://github.com/apache/roller/pull/167#discussion_r3891439006
########## app/src/main/java/org/apache/roller/weblogger/ui/struts2/util/ValidateSaltInterceptor.java: ########## @@ -0,0 +1,58 @@ +/* + * 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 com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.interceptor.AbstractInterceptor; + +/** + * Validates salts after Struts has parsed 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) Review Comment: This runs again on every chained invocation of the same request. `bookmarksImport!save` is multipart and has `<result name="success" type="chain">bookmarks</result>`, so the second pass finds the salt already removed from `SaltCache` and throws `Security Violation` after the OPML import has been flushed. Simplest fix: after a successful `consumeSubmittedSalt`, set a request attribute (e.g. `request.setAttribute("salt.validated", Boolean.TRUE)`) and skip validation when it's present. Good candidate for a `ValidateSaltInterceptorTest` case. Related: when Struts can't parse the multipart body (over `struts.multipart.maxSize`), `MultiPartRequestWrapper` has no fields, `getParameter("salt")` is `null` and this throws a 500 instead of letting the `fileUpload` interceptor's size error reach the form as it does today. Checking `((MultiPartRequestWrapper) request).hasErrors()` first would keep that UX. ########## app/src/main/java/org/apache/roller/weblogger/ui/core/filters/ValidateSaltFilter.java: ########## @@ -31,53 +28,35 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.roller.weblogger.config.WebloggerConfig; -import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache; -import org.apache.roller.weblogger.ui.core.RollerSession; /** * Filter checks all POST request for presence of valid salt value and rejects those without * a salt value or with a salt value not generated by this Roller instance. */ public class ValidateSaltFilter implements Filter { private static final Log log = LogFactory.getLog(ValidateSaltFilter.class); - private Set<String> ignored = Collections.emptySet(); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpReq = (HttpServletRequest) request; - String requestURL = httpReq.getRequestURL().toString(); - String queryString = httpReq.getQueryString(); - if (queryString != null) { - requestURL += "?" + queryString; - } - - if ("POST".equals(httpReq.getMethod()) && !isIgnoredURL(requestURL)) { - RollerSession rollerSession = RollerSession.getRollerSession(httpReq); - if (rollerSession != null) { - String userId = rollerSession.getAuthenticatedUser() != null ? rollerSession.getAuthenticatedUser().getId() : ""; - - Object saltObject = httpReq.getAttribute("salt"); // multi-form post case - String salt = saltObject != null ? saltObject.toString() : null; - salt = salt != null ? salt : httpReq.getParameter("salt"); - SaltCache saltCache = SaltCache.getInstance(); - if (salt == null || !Objects.equals(saltCache.get(salt), userId)) { - if (log.isDebugEnabled()) { - log.debug("Valid salt value not found on POST to URL : " + httpReq.getServletPath()); - } - throw new ServletException("Security Violation"); - } + if ("POST".equalsIgnoreCase(httpReq.getMethod())) { + if (SaltValidator.isMultipartFormPost(httpReq) && isStrutsAction(httpReq)) { Review Comment: Edge case: Struts only wraps requests whose full `Content-Type` matches its `MULTIPART_FORM_DATA_REGEX` (boundary ≤ 70 chars, `boundary` before `charset`). A multipart POST this check accepts but Struts doesn't wrap skips validation here and then fails in the interceptor because `getParameter("salt")` is `null`. Browsers never send those, so probably fine, just noting that deferring on media type alone isn't quite the same decision Struts makes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
