Skip to content
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

[Bug]: VirtualServer > Action.Return.type overwritten when URI contains an extension #6943

Open
yaronuliel opened this issue Dec 9, 2024 · 3 comments
Labels
bug An issue reporting a potential bug needs triage An issue that needs to be triaged

Comments

@yaronuliel
Copy link

yaronuliel commented Dec 9, 2024

Version

edge

What Kubernetes platforms are you running on?

Minikube

Description:

When defining a VirtualServer resource - you can set a preconfigured response - one of the fields it supports is type which according to the docs sets "The MIME type of the response"

In fact - it only sets the default_type (nginx docs) (location in source code) - the implications are that despite setting the desired Content-Type value - when the URI ends with an extension - nginx "guesses" the Content-Type based on the map in /etc/nginx/mime.types

Expected Behavior

When defining a type in a route with return action - it should always return that type as Content-Type, and should ignore not use the extension in the URI to assume the content type

Steps To Reproduce:

  1. Apply this following VirtualServer yaml (set host according to your installation):
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: virtual-server-bug-demo
spec:
  host: vs-bug-demo.localhost
  routes:
  - path: /
    action:
        return:
          body: "Not Found"
          code: 404
          type: text/plain
  1. Browse to http://vs-bug-demo.localhost/not-found - All good, response is 404 with content typetext/plain
  2. Browse to http://vs-bug-demo.localhost/not-found/somelinkwith.mp4 - Server response with Content-type: video/mp4 (body still contains the string "Not Found" - which cause the browser to show an invalid video player)
@yaronuliel yaronuliel added bug An issue reporting a potential bug needs triage An issue that needs to be triaged labels Dec 9, 2024
Copy link

github-actions bot commented Dec 9, 2024

Hi @yaronuliel thanks for reporting!

Be sure to check out the docs and the Contributing Guidelines while you wait for a human to take a look at this 🙂

Cheers!

@jjngx
Copy link
Contributor

jjngx commented Dec 16, 2024

@yaronuliel could you please share generated NGINX config? This will help us investigate the issue.

@yaronuliel
Copy link
Author

@jjngx
The applied yaml was

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: virtual-server-bug-demo
spec:
  host: vs-bug-demo.localhost
  routes:
  - path: /
    action:
        return:
          body: "Not Found"
          code: 404
          type: text/plain

The created resource yaml is:

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"k8s.nginx.org/v1","kind":"VirtualServer","metadata":{"annotations":{},"name":"virtual-server-bug-demo","namespace":"vs-demo"},"spec":{"host":"vs-bug-demo.localhost","routes":[{"action":{"return":{"body":"Not Found","code":404,"type":"text/plain"}},"path":"/"}]}}
  creationTimestamp: "2024-12-24T18:48:42Z"
  generation: 1
  name: virtual-server-bug-demo
  namespace: vs-demo
  resourceVersion: "424986"
  uid: ec36b225-364a-4761-a755-6fde9c4ec690
spec:
  host: vs-bug-demo.localhost
  routes:
  - action:
      return:
        body: Not Found
        code: 404
        type: text/plain
    path: /
status:
  externalEndpoints:
  - hostname: localhost
    ports: '[80,443]'
  message: 'Configuration for vs-demo/virtual-server-bug-demo was added or updated '
  reason: AddedOrUpdated
  state: Valid

The config generated in the nginx controller in /etc/nginx/conf.d/vs_vs-demo_virtual-server-bug-demo.conf is:

server {
    listen 80;
    listen [::]:80;


    server_name vs-bug-demo.localhost;

    set $resource_type "virtualserver";
    set $resource_name "virtual-server-bug-demo";
    set $resource_namespace "vs-demo";

    server_tokens "on";


    location @return_0 {
        default_type "text/plain";

        # status code is ignored here, using 0
        return 0 "Not Found";
    }



    location / {
        set $service "";


        error_page 418 =404 "@return_0";
        proxy_intercept_errors on;
        proxy_pass http://unix:/var/lib/nginx/nginx-418-server.sock;
        set $default_connection_header close;
    }
}

The problem caused by using default_type - which means that nginx will "guess" the content type if the request has an extension. and text/plain will only be used as default (when there is no extension, or the extension is not in nginx's mapping)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue reporting a potential bug needs triage An issue that needs to be triaged
Projects
Status: Todo ☑
Development

No branches or pull requests

2 participants