Skip to content

Headers assertValidHeaders method breaks local environments. #49353

@danilobassi8

Description

@danilobassi8

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

Yes

Description

I was having an error locally that I was not able to reproduce it in production.
I am in a big project with a lot of dependencies, and was Migrating from Angular 13 to 14.

This is the error:

app.module.ts:71 ERROR Error: Unexpected value of the `Content-Length` header provided. Expecting either a string or an array, but got: `281`.
    at assertValidHeaders (http.mjs:287:19)
    at HttpHeaders.lazyInit (http.mjs:100:21)
    at HttpHeaders.init (http.mjs:207:22)
    at HttpHeaders.copyFrom (http.mjs:217:15)
    at HttpHeaders.init (http.mjs:204:22)
    at HttpHeaders.has (http.mjs:125:14)
    at HttpXSRFInterceptor.intercept (http-xsrf-interceptor.ts:36:40)

After some hours debugging, I started reading the source code to see what's happening and I found this lines in headers.ts

/**
 * Verifies that the headers object has the right shape: the values
 * must be either strings or arrays. Throws an error if an invalid
 * header value is present.
 */
function assertValidHeaders(headers) {
    for (const [key, value] of Object.entries(headers)) {
        if (typeof value !== 'string' && !Array.isArray(value)) {
            throw new Error(`Unexpected value of the \`${key}\` header provided. ` +
                `Expecting either a string or an array, but got: \`${value}\`.`);
        }
    }
}

As far as i understood, this is only being called if we are in devMode. Check this lines:

if (typeof ngDevMode === 'undefined' || ngDevMode) { 
      assertValidHeaders(headers);
}

I've got 3 questions:

  • Is it correct that we're doing those kinds of evaluations in dev Environment and not in production envs? asking this because I'm running on dockers and my local is like my production environment and this is breaking my entire application, since its trowing an error, and some libraries methods are failing cause of this.
  • Wouldn't be better if instead of trowing an error we just inform to the user using a console.error, or something like this? Those kind of lines are being evaluated when I do something like request.headers.has('OtherHeaderName').
  • Is there a way to fix this? this is the only thing preventing me to migrate to Angular 14. Didn't find a solution yet. What surpriced me the most is that I'm not seeing any request on the network tab, and I also have this line in the interceptor that's being executed:
    // Setting a `host` header is being refused because it's unsafe. So let's just drop it.
   let httpHeaders = new HttpHeaders(headers);
    httpHeaders = httpHeaders.delete('Host');
    httpHeaders = httpHeaders.delete('Content-Length');

    request = request.clone({
      headers: httpHeaders,
    });

    return next.handle(request);

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

app.module.ts:71 ERROR Error: Unexpected value of the `Content-Length` header provided. Expecting either a string or an array, but got: `281`.
    at assertValidHeaders (http.mjs:287:19)
    at HttpHeaders.lazyInit (http.mjs:100:21)
    at HttpHeaders.init (http.mjs:207:22)
    at HttpHeaders.copyFrom (http.mjs:217:15)
    at HttpHeaders.init (http.mjs:204:22)
    at HttpHeaders.has (http.mjs:125:14)
    at HttpXSRFInterceptor.intercept (http-xsrf-interceptor.ts:36:40)

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.2.10
Node: 16.13.2
Package Manager: npm 8.1.2 
OS: linux x64

Angular: 14.2.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.10
@angular-devkit/build-angular   14.2.10
@angular-devkit/core            14.2.10
@angular-devkit/schematics      14.2.10
@angular/cdk                    14.2.7
@angular/cli                    14.2.10
@schematics/angular             14.2.10
rxjs                            6.6.7
typescript                      4.6.4

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/httpIssues related to HTTP and HTTP Client

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions