0

I'm getting a browser error when using SustainSys.Saml2 library with my app:

400 Bad Request
Request Header Or Cookie Too Large
nginx/1.14.0

I think that reducing my cookie size might help and I only really need the email from the claim data, so I thought that if I could just save the email claim and remove the other claims, that it might reduce my cookie size and fix this error.

I read the response to a similar question (SustainSys.Saml2 Request length header too long) and looked for some information on how to implement AcsCommandResultCreated to remove unused claims (and hopefully reduce cookie size). I didn't find a lot of documentation, but did piece together some ideas and code to try and take a stab at it.

I've tried this code in my global.asax as well as in a controller action (that I made the "returnUrl" after Saml2/Acs). It doesn't look like my FedAuth cookie (set by Saml2/Acs) is any smaller. Any comments or suggestions? Thank you.

// Check if email claim exists
var principal = ClaimsPrincipal.Current;
var userEmail = principal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Email)?.Value;

// Create new command result that only contains the email claim
if (userEmail != null)
{
      var emailClaim = principal.Claims.FirstOrDefault(claim => claim.Type == ClaimTypes.Email);
                
      Sustainsys.Saml2.Configuration.Options.FromConfiguration.Notifications.AcsCommandResultCreated =
      (commandResult, response) =>
      {
           var newCommandResult = new Sustainsys.Saml2.WebSso.CommandResult();
           newCommandResult.Principal.Claims.Append(emailClaim);
           commandResult = newCommandResult;
      };
}

UPDATE: It turned out that the test environment that I was using (which used nginx) needed to increase the request header buffer size. Adding these cookies increased the size to around 9500 bytes and nginx by default has a request header buffer size that is lower than that (I think 8000). Contacting the code owners of the test server running nginx, and increasing this solved my problem, without me having to reduce my cookie size.

hemac
  • 1
  • 2

1 Answers1

0

Do you have a lot of failed authentication attempts? That can leave a lot of Saml2.XYZ correlation cookies around on the domain. Try checking the browser dev tools and clean those up.

The "headers too large" is usually something that happens when a user has tried signing in several times with a failure and those cookies get stuck. The real issue is usually something else - causing the authentication to fail and those correlation cookies to be accumulating.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • I have not had failed authentication attempts. Some more info: I am using my app as a proxy to an external app. I'm using IIS with the extensions Application Request Routing and URL Rewrite to rewrite urls and pass requests from the browser through to the external app. I've only seen this error occur on /logout and /refresh. Doing requests to the external app directly (without my proxy app), the /logout endpoint is responding with Set-Cookie on five cookies to expire them. So maybe the Sustainsys library is not liking that operation when going through a proxy? – hemac Mar 08 '21 at 21:56