Skip to content

Conversation

merusso
Copy link

@merusso merusso commented Sep 6, 2025

Summary

When configuring the IP Restriction plugin for TCP ingress, and this plugin rejects a connection due to the IP not being allowed, it writes the following error in Kong server logs:

proxy 2023/08/11 14:46:49 [warn] 2190#0: *3416565 [kong] handler.lua:33 IP address not allowed: 10.76.130.208 while prereading client data, client: 10.76.130.208, server: 0.0.0.0:2000
proxy 2023/08/11 14:46:49 [error] 2190#0: *3416565 lua entry thread aborted: runtime error: /usr/local/share/lua/5.1/kong/pdk/private/phases.lua:97: function cannot be called in preread phase (only in: rewrite, access, response, header_filter, error, admin_api)
proxy stack traceback:
proxy coroutine 0:
proxy     [C]: in function 'error'
proxy     /usr/local/share/lua/5.1/kong/pdk/private/phases.lua:97: in function 'check_phase'
proxy     /usr/local/share/lua/5.1/kong/pdk/response.lua:1101: in function </usr/local/share/lua/5.1/kong/pdk/response.lua:1096>
proxy     /usr/local/share/lua/5.1/kong/init.lua:352: in function 'execute_plugins_iterator'
proxy     /usr/local/share/lua/5.1/kong/init.lua:868: in function 'preread'
proxy     preread_by_lua(nginx-kong-stream.conf:104):2: in main chunk while prereading client data, client: 10.76.130.208, server: 0.0.0.0:2000

The error is caused by a bug in the IP restriction plugin when handling TCP connections. The plugin calls function kong.response.error regardless of connection type, but this function isn't applicable to TCP.

To resolve the issue, this change alters the do_exit function to use ngx.exit instead of kong.response.error for TCP connections.

Checklist

Issue reference

Fix #14749

@team-eng-enablement team-eng-enablement added the author/community PRs from the open-source community (not Kong Inc) label Sep 6, 2025
@chronolaw chronolaw linked an issue Sep 7, 2025 that may be closed by this pull request
1 task
@chronolaw
Copy link
Contributor

Could we add some tests for this fix? thanks.

@chronolaw chronolaw changed the title fix(ip-restriction): error: function cannot be called in preread phase fix(plugins/ip-restriction): error: function cannot be called in preread phase Sep 8, 2025
@chronolaw chronolaw changed the title fix(plugins/ip-restriction): error: function cannot be called in preread phase fix(plugins/ip-restriction): function cannot be called in preread phase Sep 8, 2025
@chronolaw
Copy link
Contributor

@merusso could you change the merge target to "master" branch? thanks.

@merusso
Copy link
Author

merusso commented Sep 8, 2025

@merusso could you change the merge target to "master" branch? thanks.

I assumed if I did this that the fix would only be in the next release. Is that correct? What is the process for applying a fix to prior releases?

Edit: Changed merge target to master.

When configuring the IP Restriction plugin for TCP ingress, and this
plugin rejects a connection due to the IP not being allowed, it writes
the following error in Kong server logs:

```
proxy 2023/08/11 14:46:49 [warn] 2190#0: *3416565 [kong] handler.lua:33 IP address not allowed: 10.76.130.208 while prereading client data, client: 10.76.130.208, server: 0.0.0.0:2000
proxy 2023/08/11 14:46:49 [error] 2190#0: *3416565 lua entry thread aborted: runtime error: /usr/local/share/lua/5.1/kong/pdk/private/phases.lua:97: function cannot be called in preread phase (only in: rewrite, access, response, header_filter, error, admin_api)
proxy stack traceback:
proxy coroutine 0:
proxy     [C]: in function 'error'
proxy     /usr/local/share/lua/5.1/kong/pdk/private/phases.lua:97: in function 'check_phase'
proxy     /usr/local/share/lua/5.1/kong/pdk/response.lua:1101: in function </usr/local/share/lua/5.1/kong/pdk/response.lua:1096>
proxy     /usr/local/share/lua/5.1/kong/init.lua:352: in function 'execute_plugins_iterator'
proxy     /usr/local/share/lua/5.1/kong/init.lua:868: in function 'preread'
proxy     preread_by_lua(nginx-kong-stream.conf:104):2: in main chunk while prereading client data, client: 10.76.130.208, server: 0.0.0.0:2000
```

The error is caused by a bug in the IP restriction plugin when handling
TCP connections. The plugin calls function kong.response.error
regardless of connection type, but this function isn't applicable to
TCP.

To resolve the issue, this change alters the do_exit function to use
ngx.exit instead of kong.response.error for TCP connections.

Fix Kong#14749
@merusso merusso force-pushed the fix/14749-iprestriction-plugin-error branch from d969ed4 to 4cc5b5e Compare September 9, 2025 02:19
@merusso merusso changed the base branch from release/3.9.x to master September 9, 2025 02:20
@chronolaw
Copy link
Contributor

We also need a change log YAML, thanks.

@merusso
Copy link
Author

merusso commented Sep 9, 2025

Could we add some tests for this fix? thanks.

I added some asserts to existing tests to check for the logger error, but I'm unable to run the tests. Can you please verify?

I can't set up the environment correctly, getting an error attempting to use the dev container.

# make dev-legacy
Makefile:201: 'remove' target is deprecated, please use `make dev` instead

Error: Could not find rock 'kong' in /usr/local
Makefile:201: recipe for target 'remove' failed
make: [remove] Error 1 (ignored)

Error: Expected filename in format 'name-version-revision.rockspec'.
Makefile:218: recipe for target 'install-legacy' failed
make: *** [install-legacy] Error 1

@merusso
Copy link
Author

merusso commented Sep 9, 2025

We also need a change log YAML, thanks.

I think there's an issue with the changelog util. Was it moved? Should I create this YAML manually?

$ go install github.com/Kong/gateway-changelog@latest
go: github.com/Kong/gateway-changelog@latest: version constraints conflict:
        github.com/Kong/[email protected]: parsing go.mod:
        module declares its path as: github.com/Kong/changelog
                but was required as: github.com/Kong/gateway-changelog

Edit: I added it manually. IMO, the contributor docs here are confusing, maybe out of date?

@chronolaw
Copy link
Contributor

@chobits could you take a look too? thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author/community PRs from the open-source community (not Kong Inc) plugins/ip-restriction size/S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IP Restriction plugin error: function cannot be called in preread phase
3 participants