Skip to content
Merged
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: 10 additions & 2 deletions console-webapp/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { MaterialModule } from './material.module';

import { BackendService } from './shared/services/backend.service';

import { provideHttpClient } from '@angular/common/http';
import {
HttpClientXsrfModule,
provideHttpClient,
withInterceptorsFromDi,
} from '@angular/common/http';
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
import { BillingInfoComponent } from './billingInfo/billingInfo.component';
import {
Expand Down Expand Up @@ -118,6 +122,10 @@ export class SelectedRegistrarModule {}
MaterialModule,
SelectedRegistrarModule,
SnackBarModule,
HttpClientXsrfModule.withOptions({
cookieName: 'X-CSRF-Token',
headerName: 'X-CSRF-Token',
}),
],
providers: [
BackendService,
Expand All @@ -130,7 +138,7 @@ export class SelectedRegistrarModule {}
subscriptSizing: 'dynamic',
},
},
provideHttpClient(),
provideHttpClient(withInterceptorsFromDi()),
],
})
export class AppModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@
import google.registry.util.DiffUtils;
import google.registry.util.RegistryEnvironment;
import jakarta.inject.Inject;
import jakarta.servlet.http.Cookie;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -143,14 +140,10 @@ protected void setFailedResponse(String message, int code) {
}

private boolean verifyXSRF(User user) {
Optional<Cookie> maybeCookie =
Arrays.stream(consoleApiParams.request().getCookies())
.filter(c -> XsrfTokenManager.X_CSRF_TOKEN.equals(c.getName()))
.findFirst();
if (maybeCookie.isEmpty()
|| !consoleApiParams
.xsrfTokenManager()
.validateToken(user.getEmailAddress(), maybeCookie.get().getValue())) {
String xsrfToken = consoleApiParams.request().getHeader(XsrfTokenManager.X_CSRF_TOKEN);
if (xsrfToken == null
|| xsrfToken.isEmpty()
|| !consoleApiParams.xsrfTokenManager().validateToken(user.getEmailAddress(), xsrfToken)) {
consoleApiParams.response().setStatus(SC_UNAUTHORIZED);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected void getHandler(User user) {
XsrfTokenManager.X_CSRF_TOKEN,
consoleApiParams.xsrfTokenManager().generateToken(user.getEmailAddress()));
xsrfCookie.setSecure(true);
xsrfCookie.setPath("/");
consoleApiParams.response().addCookie(xsrfCookie);

JSONObject json =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ public static ConsoleApiParams createFake(AuthResult authResult) {
new SendEmailUtils(ImmutableList.of("notification@test.example"), gmailClient);
XsrfTokenManager xsrfTokenManager =
new XsrfTokenManager(new FakeClock(Instant.parse("2020-02-02T01:23:45Z")));
String token =
xsrfTokenManager.generateToken(authResult.user().map(User::getEmailAddress).orElse(""));
when(request.getCookies())
.thenReturn(
new Cookie[] {
new Cookie(
XsrfTokenManager.X_CSRF_TOKEN,
xsrfTokenManager.generateToken(
authResult.user().map(User::getEmailAddress).orElse("")))
});
.thenReturn(new Cookie[] {new Cookie(XsrfTokenManager.X_CSRF_TOKEN, token)});
when(request.getHeader(XsrfTokenManager.X_CSRF_TOKEN)).thenReturn(token);
when(request.getRequestURI()).thenReturn("/console/fake-url");
return ConsoleApiParams.create(
request,
Expand Down
Loading