Skip to content

Node.js: Cannot read properties of undefined (reading 'headers_len') #1621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skokalin opened this issue May 26, 2025 · 0 comments · Fixed by #1622
Closed

Node.js: Cannot read properties of undefined (reading 'headers_len') #1621

skokalin opened this issue May 26, 2025 · 0 comments · Fixed by #1622

Comments

@skokalin
Copy link
Contributor

skokalin commented May 26, 2025

Bug Overview

The error appears when tries to set more than 3 cookies per request. It loses context during the execution
Stacktrace:

TypeError: Cannot read properties of undefined (reading 'headers_len')
    at /usr/local/lib/node_modules/unit-http/http_server.js:157:13
    at Array.forEach (<anonymous>)
    at ServerResponse._removeHeader (/usr/local/lib/node_modules/unit-http/http_server.js:156:15)
    at ServerResponse.setHeader (/usr/local/lib/node_modules/unit-http/http_server.js:89:14)
    at ServerResponse.header (/app/node_modules/express/lib/response.js:684:10)
    at ServerResponse.append (/app/node_modules/express/lib/response.js:645:15)
    at res.cookie (/app/node_modules/express/lib/response.js:777:8)
    at file:///app/index.mjs:7:9
    at Layer.handleRequest (/app/node_modules/router/lib/layer.js:152:17)
    at next (/app/node_modules/router/lib/route.js:157:13)

Expected Behavior

All cookies should be set without any issues

Steps to Reproduce the Bug

The standard guidelines is enough to reproduce that bug
index.mjs

import express from 'express'
const app = express();

app.get('/', (req, res) => {
    res.cookie('first', 'cookie', { maxAge: 900000, httpOnly: true })
    res.cookie('second', 'cookie', { maxAge: 900000, httpOnly: true })
    res.cookie('third', 'cookie', { maxAge: 900000, httpOnly: true })
    // ^^^^^ error appears here
    res.send('Hello, Express on Unit!')
})

app.listen(8080, () => {
    console.log(`Node server is listening on port 8080!`);
});

But just in case i'll provide other files as well
config.json

{
  "listeners": {
    "*:8080": {
      "pass": "applications/ssr"
    }
  },
  "applications": {
    "ssr": {
      "type": "external",
      "processes": {
        "max": 50,
        "spare": 1
      },
      "working_directory": "/app",
      "executable": "/usr/bin/env",
      "environment": {
        "version": "{{ '%Y-%m-%d_%H:%M:%S' | strftime }}"
      },
      "arguments": [
        "node",
        "--loader",
        "unit-http/loader.mjs",
        "--require",
        "unit-http/loader",
        "./index.mjs"
      ]
    }
  },
  "access_log": {
    "path": "/dev/stdout",
    "format": "$remote_addr - - [$time_local] \"$request_line\" $status $body_bytes_sent \"$header_referer\" \"$header_user_agent\""
  }
}

package.json

{
  "name": "ngnixUnitBug",
  "version": "1.0.0",
  "description": "",
  "main": "index.mjs",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "private": true,
  "dependencies": {
    "express": "^5.1.0"
  }
}

docker-compose.yml

services:
  ssr:
    image: unit:node20
    container_name: bug_ssr
    working_dir: /app
    ports:
      - 8081:8080
    volumes:
      - ./index.mjs:/app/index.mjs
      - ./node_modules:/app/node_modules
      - ./config.json:/docker-entrypoint.d/config.json
    entrypoint: >
      /bin/sh -c "
      npm link unit-http && \
          chown -R unit:unit /run && \
          chown -R unit:unit /var/lib/unit && \
          chown -R unit:unit /app
      /usr/local/bin/docker-entrypoint.sh unitd --no-daemon --control unix:/var/run/control.unit.sock
      "

then exec:
docker compose up

open browser: http://localhost:8081

Environment Details

  • Target deployment platform: GCP, but it doesnt matter
  • Target OS: Mac OS Sequoia v15.5
  • Version of this project or specific commit: unit-http: 1.34.2
  • Version of any relevant project languages: Kubernetes (version unkown)

Additional Context

No response

skokalin added a commit to skokalin/unit that referenced this issue May 26, 2025
It fixes losing context in response in case when here 3 or more Set-Cookie headers.
Closes: nginx#1621
skokalin added a commit to skokalin/unit that referenced this issue May 28, 2025
It fixes losing context in response in case when here 3 or more headers with the same name.
The prev implementation used to use foreach function which uses local lexical environment and did not find this.headers_len localy, which causes crash of the http server module. It was replaced to for loop in order to make access for this.headers_len variable and improve performance of calculation
Closes: nginx#1621
ac000 pushed a commit to skokalin/unit that referenced this issue May 28, 2025
It fixes losing context in response in cases when there are 2 or more
headers with the same name.  The prev implementation used to use foreach
function which uses local lexical environment and did not find
this.headers_len locally, which causes crash of the http server module.

It was replaced with a for loop in order to make access for this.headers_len
variable and improve performance of calculation.

Closes: nginx#1621
Signed-off-by: Andrew Clayton <[email protected]>
@ac000 ac000 closed this as completed in 9ae1067 May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant