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

Implement one-time-salt use and add comprehensive tests #142

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for ignored URLs
snoopdave committed Oct 5, 2024
commit 6ffd15d8f394c616ddf0071f91538bc070a134e9
Original file line number Diff line number Diff line change
@@ -51,7 +51,13 @@ public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;

if ("POST".equals(httpReq.getMethod()) && !isIgnoredURL(httpReq.getServletPath())) {
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() : "";
@@ -88,16 +94,10 @@ public void destroy() {

/**
* Checks if this is an ignored url defined in the salt.ignored.urls property
* @param theUrl the the url
* @param theUrl the url
* @return true, if is ignored resource
*/
private boolean isIgnoredURL(String theUrl) {
int i = theUrl.lastIndexOf('/');

// If it's not a resource then don't ignore it
if (i <= 0 || i == theUrl.length() - 1) {
return false;
}
return ignored.contains(theUrl.substring(i + 1));
return ignored.contains(theUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.roller.weblogger.ui.core.filters;

import org.apache.roller.weblogger.config.WebloggerConfig;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.ui.core.RollerSession;
import org.apache.roller.weblogger.ui.rendering.util.cache.SaltCache;
@@ -10,6 +11,7 @@
import org.mockito.MockitoAnnotations;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -45,6 +47,8 @@ public void setUp() {
@Test
public void testDoFilterWithGetMethod() throws Exception {
when(request.getMethod()).thenReturn("GET");
StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");
when(request.getRequestURL()).thenReturn(requestURL);

filter.doFilter(request, response, chain);

@@ -64,6 +68,8 @@ public void testDoFilterWithPostMethodAndValidSalt() throws Exception {
when(request.getParameter("salt")).thenReturn("validSalt");
when(saltCache.get("validSalt")).thenReturn("userId");
when(rollerSession.getAuthenticatedUser()).thenReturn(new TestUser("userId"));
StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");
when(request.getRequestURL()).thenReturn(requestURL);

filter.doFilter(request, response, chain);

@@ -84,6 +90,8 @@ public void testDoFilterWithPostMethodAndInvalidSalt() throws Exception {
when(request.getServletPath()).thenReturn("/someurl");
when(request.getParameter("salt")).thenReturn("invalidSalt");
when(saltCache.get("invalidSalt")).thenReturn(null);
StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");
when(request.getRequestURL()).thenReturn(requestURL);

assertThrows(ServletException.class, () -> {
filter.doFilter(request, response, chain);
@@ -104,6 +112,8 @@ public void testDoFilterWithPostMethodAndMismatchedUserId() throws Exception {
when(request.getParameter("salt")).thenReturn("validSalt");
when(saltCache.get("validSalt")).thenReturn("differentUserId");
when(rollerSession.getAuthenticatedUser()).thenReturn(new TestUser("userId"));
StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");
when(request.getRequestURL()).thenReturn(requestURL);

assertThrows(ServletException.class, () -> {
filter.doFilter(request, response, chain);
@@ -123,12 +133,41 @@ public void testDoFilterWithPostMethodAndNullRollerSession() throws Exception {
when(request.getServletPath()).thenReturn("/someurl");
when(request.getParameter("salt")).thenReturn("validSalt");
when(saltCache.get("validSalt")).thenReturn("");
StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");
when(request.getRequestURL()).thenReturn(requestURL);

filter.doFilter(request, response, chain);

verify(saltCache, never()).remove("validSalt");
}
}

@Test
public void testDoFilterWithIgnoredURL() throws Exception {
try (MockedStatic<RollerSession> mockedRollerSession = mockStatic(RollerSession.class);
MockedStatic<SaltCache> mockedSaltCache = mockStatic(SaltCache.class);
MockedStatic<WebloggerConfig> mockedWebloggerConfig = mockStatic(WebloggerConfig.class)) {

mockedRollerSession.when(() -> RollerSession.getRollerSession(request)).thenReturn(rollerSession);
mockedSaltCache.when(SaltCache::getInstance).thenReturn(saltCache);
mockedWebloggerConfig.when(() -> WebloggerConfig.getProperty("salt.ignored.urls"))
.thenReturn("https://example.com/app/ignoredurl?param1=value1&m2=value2");

when(request.getMethod()).thenReturn("POST");
StringBuffer requestURL = new StringBuffer("https://example.com/app/ignoredurl");
when(request.getRequestURL()).thenReturn(requestURL);
when(request.getQueryString()).thenReturn("param1=value1&m2=value2");
when(request.getParameter("salt")).thenReturn(null); // No salt provided

filter.init(mock(FilterConfig.class));
filter.doFilter(request, response, chain);

verify(chain).doFilter(request, response);
verify(saltCache, never()).get(anyString());
verify(saltCache, never()).remove(anyString());
}
}

private static class TestUser extends User {
private final String id;