Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions AdminPortal/src/app/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ export class UserService {

enableUser (username: string) {
let url = "http://localhost:8080/api/user/"+username+"/enable";
return this.http.get(url, { withCredentials: true });
return this.http.post(url, null, { headers: this.xsrfHeaders(), withCredentials: true });
}

disableUser (username: string) {
let url = "http://localhost:8080/api/user/"+username+"/disable";
return this.http.get(url, { withCredentials: true });
return this.http.post(url, null, { headers: this.xsrfHeaders(), withCredentials: true });
}

private xsrfHeaders(): Headers {
const headers = new Headers();
const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]*)/);
if (match) {
headers.append('X-XSRF-TOKEN', decodeURIComponent(match[1]));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 CSRF cookie blocks admin account actions

When xsrfHeaders finds an XSRF cookie, both account actions fail from the Angular portal. The custom header triggers a preflight, but RequestFilter excludes x-xsrf-token from allowed headers.

Learn more

The admin portal runs on localhost:4200 and sends credentialed requests to localhost:8080. An XSRF-TOKEN cookie makes xsrfHeaders add a custom header to each POST. Browsers then send an OPTIONS preflight before the POST. RequestFilter responds with an allow-list that omits x-xsrf-token, so the browser never sends the action. The new standalone MVC tests do not exercise browser CORS behavior.

Example: An admin loads the portal after localhost:8080 issues an XSRF-TOKEN cookie. Clicking Disable sends an OPTIONS request asking to use x-xsrf-token; the response rejects the header, so the user remains enabled.

Recommended fix: Add x-xsrf-token to RequestFilter's preflight Access-Control-Allow-Headers list in the same change as the new header, and add a test for an OPTIONS request with Access-Control-Request-Headers: x-xsrf-token.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally not changed here. XSRF-TOKEN is only issued once W2 turns CSRF on (CookieCsrfTokenRepository). CSRF is disabled on this branch, so xsrfHeaders() returns empty headers and the POST stays a CORS simple request with no preflight. I checked this live: POST from Origin http://localhost:4200 returns 200 with the ACAO/ACAC headers (evidence in the PR body). The allow-list fix is owned by W2 sibling COG-1165, which replaces RequestFilter with a CorsConfigurationSource whose allowedHeaders already includes x-xsrf-token. Editing RequestFilter here would break the file-disjoint slicing and conflict with that ticket.

}
return headers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -42,12 +43,12 @@ public List<SavingsTransaction> getSavingsTransactionList(@RequestParam("usernam
return transactionService.findSavingsTransactionList(username);
}

@RequestMapping("/user/{username}/enable")
@PostMapping("/user/{username}/enable")
public void enableUser(@PathVariable("username") String username) {
userService.enableUser(username);
}

@RequestMapping("/user/{username}/disable")
@PostMapping("/user/{username}/disable")
public void diableUser(@PathVariable("username") String username) {
userService.disableUser(username);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.userFront.resource;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.userFront.service.TransactionService;
import com.userFront.service.UserService;

@RunWith(MockitoJUnitRunner.class)
public class UserResourceTest {

@Mock
private UserService userService;

@Mock
private TransactionService transactionService;

@InjectMocks
private UserResource userResource;

private MockMvc mockMvc;

@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(userResource).build();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 POST tests omit security configuration

standaloneSetup tests routing without Spring Security. An integration test can verify that only admins can use the new POST endpoints.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on purpose. UserResourceTest only checks the HTTP-method mapping, which is the change in this PR. Authorization (@PreAuthorize("hasRole('ADMIN')") on the class) is untouched. Real-app coverage with security on comes from the W0 Testcontainers admin smoke suite, and this PR merges after it. I also exercised the endpoints manually against the running app as a logged-in ROLE_ADMIN user (see the PR body).

}

@Test
public void postEnableEnablesUser() throws Exception {
mockMvc.perform(post("/api/user/alice/enable")).andExpect(status().isOk());
verify(userService).enableUser("alice");
}

@Test
public void postDisableDisablesUser() throws Exception {
mockMvc.perform(post("/api/user/alice/disable")).andExpect(status().isOk());
verify(userService).disableUser("alice");
}

@Test
public void getEnableIsMethodNotAllowed() throws Exception {
mockMvc.perform(get("/api/user/alice/enable")).andExpect(status().isMethodNotAllowed());
verify(userService, never()).enableUser("alice");
}

@Test
public void getDisableIsMethodNotAllowed() throws Exception {
mockMvc.perform(get("/api/user/alice/disable")).andExpect(status().isMethodNotAllowed());
verify(userService, never()).disableUser("alice");
}
}