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
12 changes: 11 additions & 1 deletion AdminPortal/src/app/appointment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ export class AppointmentService {

confirmAppointment(id: number) {
let url = "http://localhost:8080/api/appointment/"+id+"/confirm";
return this.http.get(url, { withCredentials: true });
let headers = new Headers();
let xsrfToken = this.readCookie('XSRF-TOKEN');
if (xsrfToken) {
headers.append('X-XSRF-TOKEN', xsrfToken);
Comment on lines +19 to +20

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 appointment confirmation

When an XSRF-TOKEN cookie exists, confirmAppointment triggers a preflight that rejects its X-XSRF-TOKEN header. RequestFilter omits that header from its preflight response, so the appointment remains unconfirmed.

Learn more

The admin portal calls the backend across ports, so the browser applies CORS. Without the cookie, the confirmation POST has no custom header. With the cookie, confirmAppointment adds X-XSRF-TOKEN, forcing an OPTIONS preflight. RequestFilter answers OPTIONS but omits that header from Access-Control-Allow-Headers. The browser blocks the POST before the controller can confirm the appointment. This applies whenever the portal can read an XSRF-TOKEN cookie, including after a CSRF cookie is introduced.

Example: With document.cookie containing XSRF-TOKEN=abc, clicking Confirm causes an OPTIONS request asking permission for x-xsrf-token. The filter excludes it, so the browser never sends the POST and the appointment stays unconfirmed.

Recommended fix: Add x-xsrf-token to the OPTIONS Access-Control-Allow-Headers list in RequestFilter when sending the header, and cover the cookie-present cross-origin flow in a browser-level test.

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 correct, but it can't happen on master today. CSRF is disabled (SecurityConfig: .csrf().disable()), so nothing ever sets an XSRF-TOKEN cookie. That means the header is never added, and the confirm POST is still a simple CORS request with no preflight. The ticket requires the header now so it's ready when CSRF is turned on.

I'm keeping the fix out of this PR on purpose. RequestFilter belongs to COG-1165 (W2), which replaces it with a declarative CorsConfigurationSource. W2 is also when CSRF (and so the cookie) gets turned on, so X-XSRF-TOKEN needs to be in that config's allowed headers. The W1 tickets are meant to be file-disjoint, so I'm leaving RequestFilter alone here and flagging this to the coordinator for COG-1165.

}
return this.http.post(url, null, { headers: headers, withCredentials: true });
}

private readCookie(name: string): string {
let match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
}

}
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.RestController;

Expand All @@ -26,7 +27,7 @@ public List<Appointment> findAppointmentList() {
return appointmentList;
}

@RequestMapping("/{id}/confirm")
@PostMapping("/{id}/confirm")
public void confirmAppointment(@PathVariable("id") Long id) {
appointmentService.confirmAppointment(id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.userFront.service.AppointmentService;

@RunWith(MockitoJUnitRunner.class)
public class AppointmentResourceTest {

@Mock
private AppointmentService appointmentService;

@InjectMocks
private AppointmentResource appointmentResource;

private MockMvc mockMvc;

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

@Test
public void confirmAcceptsPost() throws Exception {
mockMvc.perform(post("/api/appointment/42/confirm"))
.andExpect(status().isOk());

verify(appointmentService).confirmAppointment(42L);
}

@Test
public void confirmRejectsGet() throws Exception {
mockMvc.perform(get("/api/appointment/42/confirm"))
.andExpect(status().isMethodNotAllowed());

verify(appointmentService, never()).confirmAppointment(Mockito.anyLong());
}
}