# Bug OP-13340 - DATABASE_URL environment variable is not validated before usage

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-13340

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | Medium |
| Module |  |
| Target versions | 13.0.4 |
| Observed in versions | 13.0.0 |
| Sprint | 13.0.4 |
| Backlog bucket |  |
| Bug found in version (deprecated) | 13.0.x |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers |  |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Description

If the `DATABASE_URL` environment variable exists on the docker instance, even with an empty value, the application will opt to attempt connection to the PostgreSQL server instead of creating its own. Obviously, if the variable is defined but has no actual value *(e.g. null, empty string, or other non-truthy values)* this will fail.

This is primarily an issue for me as a maintainer of the [OpenProject application template](https://forums.unraid.net/topic/145146-support-imskully-openproject/) for the [Unraid](https://unraid.net/) operating system. Essentially, Unraid uses its own ecosystem of templates composed by maintainers which add support for importing docker images that are then made available on the [Unraid Community App](https://unraid.net/community/apps?q=openproject#r) center for all Unraid users to install. The template contains all variables for users to configure which then get passed when creating the docker container, but variables even when intentionally left blank get passed to the container's environment with no value as `DATABASE_URL=` - as a result of this variable not being validated, the container will always fail to start.

I am pursuing a request with Unraid to not pass empty variables to docker, though regardless it is still good practice to always validate environment variables before attempting to use them. I believe the only changes required would be in two places where this variable is used:

1.  [docker/prod/supervisord\#L20](https://github.com/opf/openproject/blob/c9241e61dc938a3def68d758a6de317d75401c84/docker/prod/supervisord#L20)
2.  [docker/prod/supervisord.conf.erb\#L80](https://github.com/opf/openproject/blob/c9241e61dc938a3def68d758a6de317d75401c84/docker/prod/supervisord.conf.erb#L80)

In both cases, the condition `URI(ENV.fetch("DATABASE_URL")).host` needs to resolve to `"127.0.0.1"` when `DATABASE_URL` exists with an empty value.

### Steps to reproduce

1.  Create docker container from latest stable image *(community:13)*
2.  Define environment variables as normal
3.  Add empty entry for `DATABASE_URL` with no value (`DATABASE_URL=`)

<!-- end list -->

``` cmd
docker run
  -d
  --name='OpenProject'
  --net='bridge'
  -e 'SECRET_KEY_BASE'='somesupersecretstring'
  -e 'DATABASE_URL'=''
  -p '8000:8080/tcp'
  -v 'HOST_PGDATA_PATH_HERE':'/var/openproject/pgdata':'rw'
  -v 'HOST_ASSETS_PATH_HERE':'/var/openproject/assets':'rw'
```

### What is the buggy behavior?

  - The container starts and attempts to use the empty `DATABASE_URL` variable, causing it to fail and not start

### What is the expected behavior?

1.  The `DATABASE_URL` environment variable should be validated to ensure it's not empty before attempting to use it
    1.  If it is empty then treat it as undefined and fall back to the default behaviour of creating internal database

### **Logs**

> \-----\> Setting PGVERSION=13 PGBIN=/usr/lib/postgresql/13/bin PGCONF\_FILE=/etc/postgresql/13/main/postgresql.conf  
> \-----\> Starting the all-in-one OpenProject setup at /app/docker/prod/supervisord...  
> \-----\> You're using an external database. Not initializing a local database cluster.  
>       Trying to contact PostgreSQL server instance or waiting for it to come online.  
>       Waiting for postgres server, 10 remaining attempts...  
>       Waiting for postgres server, 9 remaining attempts...  
>       Waiting for postgres server, 8 remaining attempts...  
>       Waiting for postgres server, 7 remaining attempts...  
>       Waiting for postgres server, 6 remaining attempts...  
>       Waiting for postgres server, 5 remaining attempts...  
>       Waiting for postgres server, 4 remaining attempts...  
>       Waiting for postgres server, 3 remaining attempts...  
>       Waiting for postgres server, 2 remaining attempts...  
>       Waiting for postgres server, 1 remaining attempts...  
>       Unable to contact postgres server:  
>       psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory  
>        Is the server running locally and accepting connections on that socket?

### Screenshots and other files

N/A

### Environment information

**OpenProject installation type**

- [ ] Hosted cloud edition
- [ ] Packaged installation
    - What distribution?
- [x] Docker All-in-one container
- [ ] Docker-compose installation
- [ ] Other (please specify)

**OpenProject version**  
[openproject/community:13](https://hub.docker.com/layers/openproject/community/13/images/sha256-eaa7fda5fd949f2e572fcf65a45e67943e36e7303b7d2066b70c410bba284016?context=explore)
