wasAsked.

Explain CORS, CSRF, and XSS for a React and cookie-authenticated ASP.NET Core application.

Paytm · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, React, Authentication & Security

Community answers

Posted by wasAsked editorial collection

CORS controls which browser origins may read responses; it is not authentication. CSRF tricks a browser into sending an authenticated request, typically with cookies. XSS executes attacker-controlled script in your origin. Allow only known CORS origins, methods, and headers; never combine wildcard origins with credentials. For cookie authentication, use SameSite plus an antiforgery token on state-changing requests. Prevent XSS with contextual output encoding, safe DOM APIs, a restrictive Content-Security-Policy, and sanitized HTML only when rich text is necessary. Mark session cookies Secure and HttpOnly. Validate authorization on the server because none of these browser controls prevents direct API calls.

Backbencher Developer · · 0 likes

These three are often confused, but they protect against different attacks. CORS (Cross-Origin Resource Sharing) - A browser rule that controls which other origins may read responses from your API. - It is not an authentication mechanism and does not stop requests from being sent; it controls whether the browser exposes the response to the calling script. - In ASP.NET Core, allow only known front-end origins: policy.WithOrigins("https://app.example.com").AllowCredentials(). Never combine AllowAnyOrigin with credentials. - If React is served from the same origin as the API (for example through a reverse proxy), you may not need CORS at all. CSRF (Cross-Site Request Forgery) - With cookie authentication, the browser attaches cookies automatically, so a malicious site can trigger a state-changing request to your API. - Defences: - SameSite=Lax or Strict cookies (and Secure, HttpOnly). - Anti-forgery tokens: the API issues a token (ASP.NET Core IAntiforgery), React sends it in a header such as X-XSRF-TOKEN on POST/PUT/DELETE, and the server validates it. - Never change state on GET requests. XSS (Cross-Site Scripting) - An attacker injects script that runs in your page and can act as the logged-in user. - React escapes values rendered in JSX by default; the risk comes from dangerouslySetInnerHTML, unsafe URLs (javascript:), and third-party scripts. - Sanitise any HTML you must render (DOMPurify), validate URLs, and add a strict Content-Security-Policy header. - HttpOnly cookies prevent scripts from reading the session cookie, which limits the damage — but XSS can still send requests as the user, so preventing it matters most. How they fit together: CORS controls who can read your API from other sites, CSRF protection stops other sites making your user perform actions, and XSS prevention stops attackers running code inside your own site, where CORS and CSRF protections no longer help.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics