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
22 changes: 17 additions & 5 deletions AdminPortal/src/app/login.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/mergeMap';
import {xsrfOptions} from './xsrf';

@Injectable()
export class LoginService {
Expand All @@ -10,17 +14,25 @@ export class LoginService {
sendCredential(username: string, password: string) {
let url = 'http://localhost:8080/index';
let params = 'username='+username+'&password='+password;
let headers = new Headers(
{
'Content-Type': 'application/x-www-form-urlencoded'
// 'Access-Control-Allow-Credentials' : true
return this.fetchCsrfToken().mergeMap(() => {
let headers = new Headers(
{
'Content-Type': 'application/x-www-form-urlencoded'
// 'Access-Control-Allow-Credentials' : true
});
return this.http.post(url, params, xsrfOptions(headers));
});
return this.http.post(url, params, {headers: headers, withCredentials : true});
}

logout() {
let url = 'http://localhost:8080/logout';
return this.http.get(url, { withCredentials: true });
}

// GET /index is permitAll, so unlike a protected URL it is not stored as the post-login redirect target.
private fetchCsrfToken(): Observable<any> {
const url = 'http://localhost:8080/index';
return this.http.get(url, { withCredentials: true }).catch(() => Observable.of(null));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔍 Bootstrap failures are invisible to login

fetchCsrfToken converts every GET error into a value, then attempts login. Once token issuance depends on that GET, the login UI cannot distinguish a bootstrap failure from a rejected login.

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 intentional. The bootstrap GET is best-effort, and a failure there shouldn't stop the login attempt: while server CSRF is off, the POST works without a token. Once COG-1166 turns CSRF on, a failed bootstrap means the POST goes out without X-XSRF-TOKEN, the server rejects it with 403, and that error reaches LoginComponent's err callback. So the failure does surface, just as a failed login. I'm not adding a separate error state for the bootstrap here, because it would change the login component's UX, which is outside this ticket.

}

}
22 changes: 22 additions & 0 deletions AdminPortal/src/app/xsrf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Headers, RequestOptionsArgs } from '@angular/http';

export const XSRF_COOKIE_NAME = 'XSRF-TOKEN';
export const XSRF_HEADER_NAME = 'X-XSRF-TOKEN';

export function readXsrfToken(): string {
const match = document.cookie.match(new RegExp('(?:^|;\\s*)' + XSRF_COOKIE_NAME + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
}

// The header is only added when the cookie exists: a custom header forces a CORS preflight.
export function xsrfHeaders(headers: Headers = new Headers()): Headers {
const token = readXsrfToken();
if (token) {
headers.set(XSRF_HEADER_NAME, token);
Comment on lines +14 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Existing XSRF cookie blocks admin login

When localhost has an XSRF-TOKEN cookie, xsrfOptions adds a header that triggers a preflight. RequestFilter omits that header from its allowlist, so the browser blocks login.

Learn more

The AdminPortal runs at localhost:4200 and posts credentials to localhost:8080, so its requests are cross-origin. The existing RequestFilter answers OPTIONS requests without permitting x-xsrf-token. A browser sends that preflight as soon as the cookie is present, even when server-side CSRF is disabled. The preflight fails, and the browser never sends the login POST.

Example: A different localhost application previously set XSRF-TOKEN=abc with path /. An admin loads the portal and submits valid credentials. The new header triggers OPTIONS /index; the browser rejects the response and the admin cannot log in.

Recommended fix: Coordinate the header rollout with an update to RequestFilter that permits x-xsrf-token on preflight responses. Verify login with a cookie already present, not just a fresh browser profile.

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 does happen, and it's a known edge case of the design the audit prescribes (COG-1147 item M6). Adding the header only when the cookie exists is what keeps it from firing on a clean browser against the 1.5.4 server. The real fix is the one you point to: allow x-xsrf-token in the preflight. That belongs to COG-1165 (CorsConfig replacing RequestFilter), which is out of scope for this TypeScript-only W1 ticket, and it has to land before or with COG-1166 anyway. Until then, the trigger is a stray XSRF-TOKEN cookie set on localhost by some other app, which doesn't happen in the normal dev or prod setup. Leaving the code as is and flagging it for COG-1165.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Follow-up from browser testing: this already happens on master. Angular 4 HttpModule registers CookieXSRFStrategy('XSRF-TOKEN', 'X-XSRF-TOKEN') by default (@angular/http XHRConnection → configureRequest). So any AdminPortal request already carries X-XSRF-TOKEN whenever that cookie exists, and hits the same preflight rejection. Testing with a synthetic cookie confirmed it: the bootstrap GET, which doesn't use xsrfOptions, was preflighted and blocked the same way. The fix is still the COG-1165 allow-list.

}
return headers;
}

export function xsrfOptions(headers?: Headers): RequestOptionsArgs {
return { headers: xsrfHeaders(headers), withCredentials: true };
}