apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: String environment variable value is casted tonumber if contains a number in standalone mode

Open ravillarreal opened this issue 10 months ago • 11 comments

Current Behavior

Im using apisix, with standalone mode, in which ive noticed that when a env variable is set in apisix.yaml file, if the value is a number, it will automatically convert it to number, and this causes an error if the number is bigger than Lua's double, for example (extracted from my apisix.yaml file):

plugin_configs:
  - id: 1
    plugins:
      openid-connect:
        client_id: "${{APISIX_OIDC_CLIENT_ID}}"

Note the double " in the env var, still it doesnt treat it like an string

The APISIX_OIDC_CLIENT_ID is set to "356002209726529540" but the value is casted to "client_id":3.5600220972653e+17

This is because this code in apisix/cli/file.lua

https://github.com/apache/apisix/blob/a269d61a5b115f9350293c47c2551597efc5487c/apisix/cli/file.lua#L127

Expected Behavior

I expect that the value passed in as "${{var}}" works like an string

Error Logs

2026/01/22 06:11:11 [error] 57#57: *321192 [lua] config_yaml.lua:333: failed to check item data of [plugin_configs] err:failed to check the configuration of plugin openid-connect err: property "client_id" validation failed: wrong type: expected string, got number ,val:

Steps to Reproduce

  1. Run APISIX via the docker image
  2. Configure it as standalone mode with env variable - APISIX_DEPLOYMENT_MODE=standalone
  3. Configure env variable with number inside bigger than lua's double eg "356002209726529540"
  4. Set the variable in apisix.yaml in "${{}}" format
  5. Get the error, in this case im using openid-connect plugin

Environment

  • APISIX version (run apisix version): image: apache/apisix:3.14.1-debian
  • Operating system (run uname -a): fedora 43 workstation
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info): standalone mode
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

ravillarreal avatar Jan 22 '26 06:01 ravillarreal

Hii, I’d like to confirm my understanding of this issue before starting any work. From what I see, the problem is that environment variable substitution currently performs automatic type casting even when the value is explicitly quoted in YAML.

For example: abc: 10 xyz: "${{ABC}}" with: ABC=10

Observed behavior: abc becomes number 10 and xyz also becomes number 10

my understanding: abc should remain number 10 and xyz should remain string "10", since it is explicitly quoted

This automatic casting can cause schema validation errors in plugins such as openid-connect, especially when large numeric values are provided via environment variables (values exceeding Lua number precision).

Before proceeding, I’d appreciate confirmation that Should quoted environment variable substitutions always be treated as strings? and Is limiting automatic type casting only to unquoted substitutions the intended direction? Once confirmed, I’ll be happy to work on a fix. Thanks for your time and guidance.

suryaparua-official avatar Jan 22 '26 16:01 suryaparua-official

That's correct i havent found official documentation in apisix saying that double quotes must handle values like string, but its more part of the YAML specification i think https://stackoverflow.com/a/69850618, but the real problem is that it is parsing to a numeric value and that is making trouble for numbers greater than lua's limit or if the number must be treated as an string, because it will convert it to scientific notation as in the example

ravillarreal avatar Jan 22 '26 17:01 ravillarreal

Yes, agreed. This is expected behaviour caused by YAML type inference and Lua's use of double precision numbers, so its not a lua or APISIX issue. Since the values are identifiers and not intended for arithmetic, the correct solution is to define them as string at the configuration level by quoted them in YMAL. This prevents numeric parsing, precision loss, and scientific notation when the value reaches Lua.

suryaparua-official avatar Jan 23 '26 03:01 suryaparua-official

This automatic type conversion was initially designed in #2743 to enable users to configure numeric system parameters (e.g., worker_rlimit_nofile) and boolean parameters (e.g., enable_admin) via environment variables. This design targeted core APISIX configuration items within config.yaml.

In a subsequent extension (#6505), to address user requests (#5244), the same resolve_conf_var function was directly reused in the parsing of apisix.yaml for standalone mode. However, this failed to account for the differing usage scenarios between the two contexts. This resulted in environment variables like “${{VAR}}” in standalone mode apisix.yaml being automatically converted to numeric types when their values were pure numeric strings (e.g., “356002209726529540”).

To address this issue, I recommend adding an optional parameter to resolve_conf_var to control whether automatic type conversion occurs, and disabling automatic type conversion when processing apisix.yaml.

Baoyuantop avatar Jan 26 '26 06:01 Baoyuantop

Hi, I would like to work on this issue. Based on @Baoyuantop's suggestion, I plan to add an optional parameter to resolve_conf_var to control automatic type conversion and disable it for apisix.yaml processing. I will submit a PR soon.

lxbme avatar Mar 06 '26 06:03 lxbme

Hi, I've submitted PR #13078 to address this issue.

Following @Baoyuantop's suggestion, I've added an optional parameter to control type conversion and disabled it for apisix.yaml while maintaining backward compatibility.

Please review when you have time. Thanks!

lxbme avatar Mar 06 '26 09:03 lxbme

I took a deeper look at this issue and #13078. The current fix (exceeds_lua_precision — only skip tonumber() for integers >15 digits) doesn't actually solve the original problem. Even small numeric strings like client_id: "${{VAR}}" with VAR=12345 would still be converted to number 12345, failing schema validation when the field expects a string.

The root cause is that resolve_conf_var() does env var substitution after YAML parsing. At that point, YAML quotes are already gone — both "${{VAR}}" and ${{VAR}} become the same Lua string ${{VAR}}. So there's no way to tell whether the user intended a string or a number.

I think the right fix is to do the substitution before YAML parsing — replace ${{VAR}} in the raw YAML text first, then let yaml.load() handle types naturally:

client_id: "${{VAR}}"    # VAR=12345 → "12345" → string ✓
retries: ${{RETRIES}}     # RETRIES=3 → 3       → number ✓
http_to_https: ${{FLAG}}  # FLAG=true → true    → boolean ✓

Quoted = string, unquoted = YAML decides. Same approach Helm uses. Only applies to apisix.yaml, config.yaml stays unchanged. The change is small — ~25 lines for a new function + 2 call sites.

Happy to submit a PR if this direction makes sense.

nic-6443 avatar Apr 10 '26 07:04 nic-6443

Hi @lxbme, could you update your PR based on the proposal at https://github.com/apache/apisix/issues/12932#issuecomment-4222017590?

Baoyuantop avatar Apr 10 '26 07:04 Baoyuantop

@Baoyuantop. Fair point. I'll submit a new commit shortly.

lxbme avatar Apr 10 '26 11:04 lxbme

Hi @lxbme, any updates?

Baoyuantop avatar Apr 13 '26 08:04 Baoyuantop

Hi @Baoyuantop, please review the latest commit in pr.

lxbme avatar Apr 13 '26 08:04 lxbme