Skip to content

bug: environment variable values mixed in proxy-rewrite plugin #11121

Description

@jompu

Current Behavior

The proxy-rewrite plugin uses value from wrong environment variable, when configured with standalone configuration.

The problem is that some times the values at step 2 are like this (which is wrong):
X-Some-String-Value: "astringvalue"
X-Some-String-Value-But-Different: "Different astringvalue"

Expected Behavior

Header values should be like this:
X-Some-String-Value: "astringvalue"
X-Some-String-Value-But-Different: "Different astringvaluebutdifferent"

Error Logs

No response

Steps to Reproduce

config.yaml

apisix:
  node_listen: 9999
  enable_ipv6: false
  enable_admin: false
  ssl:
    enable: false

deployment:
  role: data_plane
  role_data_plane:
    config_provider: yaml

apisix.yaml

upstreams:
  - id: echoserver
    pass_host: rewrite
    upstream_host: echo.free.beeceptor.com
    scheme: https
    nodes:
      - host: echo.free.beeceptor.com
        port: 443
        weight: 1
plugin_configs:
  - id: validaterequest
    plugins:
      request-validation:
        header_schema:
          type: object
          required: 
            - X-random-request-header
          properties:
            X-random-request-header:
              type: string
              pattern: '^${{SOME_STRING_VALUE}}$'
routes:
  - name: test
    uri: /test
    plugin_config_id: validaterequest
    plugins:
      proxy-rewrite:
        uri: /some/api/endpoint
        headers:
          set:
            X-Some-String-Value-But-Different: "Different ${{SOME_STRING_VALUE_BUT_DIFFERENT}}"
            X-Some-String-Value: ${{SOME_STRING_VALUE}}
    upstream_id: echoserver
#END

The issue does not happen always, anyway try below steps multiple times and it happens at some point. Quite often it happens at the first time though. The apisix.yaml is just an example that I have used to reproduce the problem.

  1. Run container
docker run --env SOME_STRING_VALUE_BUT_DIFFERENT=astringvaluebutdifferent \
  --env SOME_STRING_VALUE=astringvalue \
  -v $(pwd)/config.yaml:/usr/local/apisix/conf/config.yaml:ro \
  -v $(pwd)/apisix.yaml:/usr/local/apisix/conf/apisix.yaml:ro \
  -p 9999:9999/tcp apache/apisix:3.9.0-debian
  1. Run curl: curl -H "X-random-request-header: astringvalue" http://localhost:9999/test
  • You should receive a json that shows headers set by the proxy-rewrite plugin.
  • Header values should be like this:
    X-Some-String-Value: "astringvalue"
    X-Some-String-Value-But-Different: "Different astringvaluebutdifferent"
    • if this is the case, stop the container and go back to step 1

The problem is that some times the values at step 2 are like this (which is wrong):
X-Some-String-Value: "astringvalue"
X-Some-String-Value-But-Different: "Different astringvalue"

Restarting the pod or just reloading the configuration (e.g. by editing apisix.yaml file) will fix the problem.

A script to run steps repeatedly and stop when the issue happens:

#!/bin/bash

SOME_STRING_VALUE_BUT_DIFFERENT=astringvaluebutdifferent
SOME_STRING_VALUE=astringvalue

start_container () {
  docker run -d --env SOME_STRING_VALUE_BUT_DIFFERENT=$SOME_STRING_VALUE_BUT_DIFFERENT \
    --env SOME_STRING_VALUE=$SOME_STRING_VALUE \
    -v $(pwd)/config.yaml:/usr/local/apisix/conf/config.yaml:ro \
    -v $(pwd)/apisix.yaml:/usr/local/apisix/conf/apisix.yaml:ro \
    -p 9999:9999/tcp apache/apisix:3.9.0-debian
}

run_curl () {
  curl -s -H "X-random-request-header: astringvalue" http://localhost:9999/test
}

run_curl_get_header () {
  run_curl | jq -r ".headers[\"$1\"]"
}

counter=1
while :; do
  container_id=$(start_container)
  sleep 5

  butdifferentvalue=$(run_curl_get_header "X-Some-String-Value-But-Different")

  if [ "$butdifferentvalue" != "Different $SOME_STRING_VALUE_BUT_DIFFERENT" ]; then
    echo
    echo "X-Some-String-Value-But-Different header was set with value 'Different $SOME_STRING_VALUE', when it should be 'Different $SOME_STRING_VALUE_BUT_DIFFERENT'."
    echo "We had to repeate the test $counter times."
    echo; echo
    echo "Here is the wrong echoserver response:"
    echo
    run_curl
    
    docker rm -f $container_id > /dev/null
    break
  fi

  docker rm -f $container_id > /dev/null

  echo "Test succeeded, let's try again!"
  sleep 1
  counter=$((counter+1))
done

Environment

  • Docker image: apache/apisix:3.9.0-debian

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions