cvat: 「Forbidden (403) CSRF verification failed. Request aborted」in Django administration page

My actions before raising this issue

I used K8S to deploy all the services that cvat depends on in the cluster. The deployment is as follows: image

Then, I went to the backend-server pod and created a superuser account with the command python3 ~/manage.py createsuperuser

Next, I went back to the Web page, logged in the superuser account with the browser, and entered the background management page, as shown below: image

Steps to Reproduce (for bugs)

In the Django administration page, all my Post requests are getting errors like this: image

Possible Solution

After Google, I found a workable solution: https://github.com/doccano/doccano/issues/1820

Your Environment

As mentioned above, I used the official ymal file to deploy the various services in the K8S cluster. We haven’t used Django before, so we don’t know how to add annotations to our code. I was looking for a way to fix this by adding an environment variable to the backend-server deploy file.

Thanks for thinking about this!!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 17

Most upvoted comments

@kelbstf This is the solution I came up with. Although it may not be perfect, it’s functional.

  1. Clone the CVAT repository
  2. Create a docker-compose.override.yml file. This file allows you to extend or override settings from the original docker-compose.yml.
services:
  cvat_server:
    volumes:
       # Resolve CRSF token error by mounting production.py
      - ./production.py:/home/django/cvat/settings/production.py:ro
    environment:
      # Make CVAT_HOST accessible INSIDE the Docker container
      CVAT_HOST: ${CVAT_HOST:-localhost}
  1. Duplicate the production.py file located at ./cvat/settings/production.py into a new file named ./production.py.
  2. Append the following line to the end of the ./production.py file:
CSRF_TRUSTED_ORIGINS = [origin.rstrip('/') for origin in os.getenv('CSRF_TRUSTED_ORIGINS', f'http://{CVAT_HOST},https://{CVAT_HOST}').split(',')]
  1. Once everything is set up, export the CVAT_HOST variable with your desired domain and bring up the Docker containers:
export CVAT_HOST="mydomain.com"
docker compose up -d

@kelbstf This is the solution I came up with. Although it may not be perfect, it’s functional.

  1. Clone the CVAT repository
  2. Create a docker-compose.override.yml file. This file allows you to extend or override settings from the original docker-compose.yml.
services:
  cvat_server:
    volumes:
       # Resolve CRSF token error by mounting production.py
      - ./production.py:/home/django/cvat/settings/production.py:ro
    environment:
      # Make CVAT_HOST accessible INSIDE the Docker container
      CVAT_HOST: ${CVAT_HOST:-localhost}
  1. Duplicate the production.py file located at ./cvat/settings/production.py into a new file named ./production.py.
  2. Append the following line to the end of the ./production.py file:
CSRF_TRUSTED_ORIGINS = [origin.rstrip('/') for origin in os.getenv('CSRF_TRUSTED_ORIGINS', f'http://{CVAT_HOST},https://{CVAT_HOST}').split(',')]
  1. Once everything is set up, export the CVAT_HOST variable with your desired domain and bring up the Docker containers:
export CVAT_HOST="mydomain.com"
docker compose up -d

Thank you so much for this solution!

As far as i know those settings are only applied when you use the development mode, not the production mode? Assuming a normal “vanilla” helm install:

  1. You run helm install, it uses the normal cvat docker image, this one has the DJANGO_CONFIGURATION env variable set to production, not development. https://github.com/opencv/cvat/blob/a43477af8eea946cf6498a6b1d12888840925bcc/Dockerfile#L76
  2. The manage.py file then uses “production” since the DJANGO_CONFIGURATION variable is set (otherwise if it wasnt it would use “development”) https://github.com/opencv/cvat/blob/a43477af8eea946cf6498a6b1d12888840925bcc/manage.py#L12
  3. You then use the “production.py” file and not the “development.py” file that you linked, this one has no such configuration options ( https://github.com/opencv/cvat/blob/develop/cvat/settings/production.py )

So if you would like to use the development.py options, you have to add a ENV variable to your helm chart called DJANGO_CONFIGURATION with the value of “development”.

@kelbstf I did a quick deep dive and couldn’t get it figured out. My situation was using cloudflared proxy for public access and I could not get it to work.

My guess is that when the tus or whatever lib it is gets init’d, it needs an option to disable CORS or configure it for CVAT_HOST. At the time I was super busy so couldn’t take the time to fix the issue and now I’ve moved on to other software.

IIRC, it was issue after issue after issue and I had gotten fed up. Cvat team doest reply to non trivial issues and when they do, they usually say “it works on our cloud hosted service, you should stop self hosting and subscribe to our cloud service” instead of doing any meaningful troubleshooting and fixing.

I have CVAT behind a cloudflared tunnel and am getting the 403 forbidden CSRF on django admin page POST. The provided fix says to use the CSRF_TRUSTED_ORIGINS env var but, Adding it to all the containers doesnt seem to solve the issue.

export CSRF_TRUSTED_ORIGINS="htttp://myserversIP,http://some other ip"

in the docker-compose.yml where do I add the ENV var?

    environment:
      CSRF_TRUSTED_ORIGINS: ${CSRF_TRUSTED_ORIGINS:-}

I put it in cvat-server, cvat-ui, cvat-utils and I am still getting CSRF 403 forbidden