Skip to content

Commit cb3f766

Browse files
authored
Merge pull request #258 from MISP/fix-cors-issues-prod
fix: add CORS_ORIGINS env var to add custom domains
2 parents 429e2e7 + 6da6a46 commit cb3f766

13 files changed

Lines changed: 45 additions & 18 deletions

File tree

.env.dev.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ FLOWER_BASIC_AUTH=flower:flower
5353

5454
# Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
5555
LOG_LEVEL=INFO
56+
57+
# Comma-separated list of extra allowed CORS origins (e.g. https://app.example.com)
58+
CORS_ORIGINS=

.env.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ GARAGE_METRICS_TOKEN=
4747

4848
FLOWER_BASIC_AUTH=flower:flower
4949

50-
LOG_LEVEL=WARNING
50+
LOG_LEVEL=WARNING
51+
52+
# Comma-separated list of extra allowed CORS origins (e.g. https://app.misp-workbench.com)
53+
CORS_ORIGINS=

.env.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ GARAGE_METRICS_TOKEN=garage-test-metrics-token
4848

4949
FLOWER_BASIC_AUTH=flower:flower
5050

51-
LOG_LEVEL=DEBUG
51+
LOG_LEVEL=DEBUG
52+
53+
# Comma-separated list of extra allowed CORS origins (e.g. https://app.example.com)
54+
CORS_ORIGINS=

CLAUDE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ cd api && poetry run pre-commit run --all-files
5454

5555
### Frontend
5656

57+
Copy `frontend/.env.dist` to `frontend/.env` and set `VITE_API_URL` to the API base URL before building:
58+
59+
```bash
60+
cp frontend/.env.dist frontend/.env
61+
# Edit frontend/.env and set VITE_API_URL=https://api.your-domain.com
62+
```
63+
5764
```bash
5865
cd frontend
5966
npm install

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Then start the stack:
4343

4444
```bash
4545
cp .env.dist .env
46-
# edit .env: set OAUTH2_SECRET_KEY, OAUTH2_REFRESH_SECRET_KEY, GARAGE_ADMIN_TOKEN, S3_ACCESS_KEY, S3_SECRET_KEY, etc.
46+
# edit .env: set CORS_ORIGINS, OAUTH2_SECRET_KEY, OAUTH2_REFRESH_SECRET_KEY, GARAGE_ADMIN_TOKEN, S3_ACCESS_KEY, S3_SECRET_KEY, etc.
4747
docker compose --env-file=".env" up --build
4848
...
4949
...

api/app/auth/auth.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,7 @@ def get_scopes_for_user(user: user_schemas.User) -> list[str]:
227227
def get_random_password():
228228
return "".join(
229229
random.choice(
230-
string.ascii_lowercase
231-
+ string.ascii_uppercase
232-
+ string.digits
233-
+ string.punctuation
230+
string.ascii_lowercase + string.ascii_uppercase + string.digits
234231
)
235-
for _ in range(12)
232+
for _ in range(16)
236233
)

api/app/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"http://localhost:3001",
5656
"http://localhost:6274",
5757
]
58+
extra_origins = os.environ.get("CORS_ORIGINS", "")
59+
if extra_origins:
60+
origins += [o.strip() for o in extra_origins.split(",") if o.strip()]
5861
app.add_middleware(
5962
CORSMiddleware,
6063
allow_origins=origins,

api/app/tests/test_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ def test_returns_empty_for_no_scopes(self):
135135

136136

137137
class TestGetRandomPassword:
138-
def test_returns_string_of_length_12(self):
138+
def test_returns_string_of_length_16(self):
139139
pwd = get_random_password()
140140
assert isinstance(pwd, str)
141-
assert len(pwd) == 12
141+
assert len(pwd) == 16
142142

143143
def test_passwords_differ_across_calls(self):
144144
passwords = {get_random_password() for _ in range(10)}

api/entrypoint.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ if [ "$STORAGE_ENGINE" = "s3" ]; then
1010
fi
1111

1212
# create admin org and user (skipped if they already exist)
13-
ADMIN_PASSWORD=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | head -c 24)
13+
ADMIN_PASSWORD=$(openssl rand -hex 16)
1414
poetry run python -m app.cli create-organisation ADMIN
15-
poetry run python -m app.cli create-user admin@admin.local "$ADMIN_PASSWORD" 1 1
15+
CREATE_USER_OUTPUT=$(poetry run python -m app.cli create-user admin@admin.local "$ADMIN_PASSWORD" 1 1)
16+
echo "$CREATE_USER_OUTPUT"
1617

1718
cat <<'EOF'
1819
@@ -25,8 +26,11 @@ cat <<'EOF'
2526
2627
Server is ready!
2728
EOF
28-
echo " Admin credentials: admin@admin.local / $ADMIN_PASSWORD"
29-
echo
29+
if echo "$CREATE_USER_OUTPUT" | grep -q "Created user"; then
30+
echo " Admin credentials: admin@admin.local / $ADMIN_PASSWORD"
31+
echo " Save this password — it will not be shown again."
32+
echo
33+
fi
3034

3135
# OpenSearch credentials (prod security plugin is enabled)
3236
export OPENSEARCH_USERNAME="${OPENSEARCH_USERNAME:-admin}"
@@ -36,4 +40,4 @@ export OPENSEARCH_PASSWORD="${OPENSEARCH_PASSWORD:-${OPENSEARCH_INITIAL_ADMIN_PA
3640
poetry run python -m app.opensearch_setup
3741

3842
# start API
39-
poetry run uvicorn app.main:app --host 0.0.0.0 --port 80
43+
poetry run uvicorn app.main:app --host 0.0.0.0 --port 80 --proxy-headers --forwarded-allow-ips='*'

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ x-environment: &default-env
3838
MAIL_PORT: ${MAIL_PORT}
3939
MAIL_SERVER: ${MAIL_SERVER}
4040
LOG_LEVEL: ${LOG_LEVEL:-INFO}
41+
CORS_ORIGINS: ${CORS_ORIGINS:-}
4142

4243
services:
4344
postgres:

0 commit comments

Comments
 (0)