Skip to content

Commit

Permalink
Add check/config for HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Oct 9, 2024
1 parent 00b5bd7 commit eb93b15
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions files/nginx/redirector.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ server {
listen 80 default_server reuseport;
listen [::]:80 default_server reuseport;

if ($http_host != ${CNAME}) {
return 421;
}

# Anything requesting this particular URL should be served content from
# Certbot's folder so the HTTP-01 ACME challenges can be completed for the
# HTTPS certificates.
Expand Down
9 changes: 5 additions & 4 deletions files/nginx/setup-odk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ if [ "$SSL_TYPE" = "selfsign" ] && [ ! -s "$SELFSIGN_PATH/privkey.pem" ]; then
-days 3650 -nodes -sha256
fi

CNAME="$( [ "$SSL_TYPE" = "customssl" ] && echo "local" || echo "$DOMAIN")"
export CNAME

# start from fresh templates in case ssl type has changed
echo "writing fresh nginx templates..."
# redirector.conf gets deleted if using upstream SSL so copy it back
cp /usr/share/odk/nginx/redirector.conf /etc/nginx/conf.d/redirector.conf

CNAME=$( [ "$SSL_TYPE" = "customssl" ] && echo "local" || echo "$DOMAIN") \
envsubst '$CNAME' < /usr/share/odk/nginx/redirector.conf > /etc/nginx/conf.d/redirector.conf
envsubst '$SSL_TYPE $CNAME $SENTRY_ORG_SUBDOMAIN $SENTRY_KEY $SENTRY_PROJECT' \
< /usr/share/odk/nginx/odk.conf.template \
> /etc/nginx/conf.d/odk.conf
Expand All @@ -49,7 +50,7 @@ else
echo "starting nginx for upstream ssl..."
else
# remove letsencrypt challenge reply, but keep 80 to 443 redirection
perl -i -ne 'print if $. < 7 || $. > 14' /etc/nginx/conf.d/redirector.conf
perl -i -ne 'print if $. < 11 || $. > 18' /etc/nginx/conf.d/redirector.conf
echo "starting nginx for custom ssl and self-signed certs..."
fi
exec nginx -g "daemon off;"
Expand Down
2 changes: 1 addition & 1 deletion test/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ wait_for_http_response 5 localhost:8383/health 200
log "Waiting for mock enketo..."
wait_for_http_response 5 localhost:8005/health 200
log "Waiting for nginx..."
wait_for_http_response 90 localhost:9000 301
wait_for_http_response 90 localhost:9000 421

npm run test:nginx

Expand Down
10 changes: 10 additions & 0 deletions test/test-nginx.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ describe('nginx config', () => {
assert.equal(body['x-forwarded-proto'], 'https');
});

it('should reject HTTP requests with incorrect host header supplied', async () => {
// when
const res = await fetchHttp('/', { headers:{ host:'bad.example.com' } });

console.log('res.location:', res.headers.get('location'));

// then
assert.equal(res.status, 421);
});

it('should reject HTTPS requests with incorrect host header supplied', async () => {
// when
const res = await fetchHttps('/', { headers:{ host:'bad.example.com' } });
Expand Down

0 comments on commit eb93b15

Please sign in to comment.