Skip to content

Headers assertValidHeaders method breaks local environments. #49353

Closed

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

Activity

  1. changed the title [-]Headers assertValidHeaders is breacking my local environment.[/-] [+]Headers assertValidHeaders is breaking my local environment.[/+] on Mar 7, 2023
  2. changed the title [-]Headers assertValidHeaders is breaking my local environment.[/-] [+]Headers assertValidHeaders breaks local environments.[/+] on Mar 7, 2023
  3. changed the title [-]Headers assertValidHeaders breaks local environments.[/-] [+]Headers assertValidHeaders method breaks local environments.[/+] on Mar 7, 2023
  4. JeanMeche commented on Mar 7, 2023

    @JeanMeche
    Member

    Hi,

    To answer the first 2 points : to reduce and optimize the payload size, multiple errors and validations/assertions from the Angular framework are tree shaken at build time by the optimizer.

    To get to the bottom of you problem. I looks like #46059 has introduced this regression. assertValidHeaders should be accepting numbers.

    But since the Angular and the TS typings only allow strings in header values, how did you even get there ? Could you maybe create a repro on stackblitz ?

  5. danilobassi8 commented on Mar 7, 2023

    @danilobassi8
    Author

    Could you maybe create a repro on stackblitz ?

    I will try, but I dont even know why this is happening. I am using the debugger, and when I console log my headers they look like this:

    {
        "normalizedNames": {},
        "lazyUpdate": [
            {
                "name": "Host",
                "op": "d"
            },
            {
                "name": "Content-Length",
                "op": "d"
            }
        ],
        "headers": {},
        "lazyInit": {
            "normalizedNames": {},
            "lazyUpdate": null
        }
    }

    help

  6. added
    area: common
    on Mar 7, 2023
  7. added this to the needsTriage milestone on Mar 7, 2023
  8. added 3 commits that reference this issue on Mar 9, 2023
    fb79ed3
  9. danilobassi8 commented on Mar 9, 2023

    @danilobassi8
    Author

    Hi, I don't know how the usual flow is. But I opened a PR to the main branch with some changes to fix that issue.
    Thanks in advance.

  10. AurelienLoyer commented on Mar 9, 2023

    @AurelienLoyer

    Same issue on our side with a simple interceptor getting the header... 😭

    Error: Unexpected value of the `normalizedNames` header provided. Expecting either a string or an array, but got: `[object Map]`.
        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.get (http.mjs:136:14)
        at InspectionInterceptor.intercept (inspection.interceptor.ts:259:32)
        at HttpInterceptorHandler.handle (http.mjs:1443:33)
        at ErrorHandlerInterceptor.intercept (error-handler.interceptor.ts:28:17)
        at HttpInterceptorHandler.handle (http.mjs:1443:33)
    

    And the famous interceptor 👇

    public intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
        if (!this._accountService.isInspector() || request.headers.has(INTERCEPTOR_SKIP_HEADER)) {
          return next.handle(request);
        }
    
        // ...
    }
  11. jbmgrtn commented on Mar 9, 2023

    @jbmgrtn

    Same issue on our side with a simple interceptor getting the header... 😭

    Error: Unexpected value of the `normalizedNames` header provided. Expecting either a string or an array, but got: `[object Map]`.
        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.get (http.mjs:136:14)
        at InspectionInterceptor.intercept (inspection.interceptor.ts:259:32)
        at HttpInterceptorHandler.handle (http.mjs:1443:33)
        at ErrorHandlerInterceptor.intercept (error-handler.interceptor.ts:28:17)
        at HttpInterceptorHandler.handle (http.mjs:1443:33)
    

    And the famous interceptor 👇

    public intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
        if (!this._accountService.isInspector() || request.headers.has(INTERCEPTOR_SKIP_HEADER)) {
          return next.handle(request);
        }
    
        // ...
    }

    According to the code I see in my browser

    /**
     * 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}\`.`);
            }
        }
    }

    shouldn't this method be called on headers.headers instead because headers is

    {
        "normalizedNames": [],
        "lazyUpdate": [
            {
                "name": "authorization",
                "op": "d"
            }
        ],
        "headers": [],
        "lazyInit": {
            "normalizedNames": [
                [
                    "authorization",
                    "Authorization"
                ]
            ],
            "lazyUpdate": null,
            "headers": [
                [
                    "authorization",
                    [
                        "REDACTED"
                    ]
                ]
            ],
            "lazyInit": null
        }
    }
  12. danilobassi8 commented on Mar 9, 2023

    @danilobassi8
    Author

    oh.. @AurelienLoyer my PR wont cover that case because you are sending a Map.

  13. added
    area: common/http
    and removed
    area: common
    on Apr 5, 2023
  14. added 2 commits that reference this issue on Apr 13, 2023
    95bc7e2
    05a0225
  15. angular-automatic-lock-bot commented on May 14, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  16. locked and limited conversation to collaborators on May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/http

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions