mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-22 14:29:36 +02:00
feat: Enhance session cookie domain handling for IP addresses and single-label hostnames
This commit is contained in:
parent
7cea432353
commit
1c15e85986
4 changed files with 18 additions and 16 deletions
|
@ -135,17 +135,14 @@ SESSION_COOKIE_SAMESITE = 'Lax'
|
||||||
|
|
||||||
SESSION_COOKIE_SECURE = FRONTEND_URL.startswith('https')
|
SESSION_COOKIE_SECURE = FRONTEND_URL.startswith('https')
|
||||||
|
|
||||||
# Parse the FRONTEND_URL
|
hostname = urlparse(FRONTEND_URL).hostname
|
||||||
# Remove and ' from the URL
|
|
||||||
|
|
||||||
parsed_url = urlparse(FRONTEND_URL)
|
|
||||||
hostname = parsed_url.hostname
|
|
||||||
|
|
||||||
# Check if the hostname is an IP address
|
|
||||||
is_ip_address = hostname.replace('.', '').isdigit()
|
is_ip_address = hostname.replace('.', '').isdigit()
|
||||||
|
|
||||||
if is_ip_address:
|
# Check if the hostname is single-label (no dots)
|
||||||
# Do not set a domain for IP addresses
|
is_single_label = '.' not in hostname
|
||||||
|
|
||||||
|
if is_ip_address or is_single_label:
|
||||||
|
# Do not set a domain for IP addresses or single-label hostnames
|
||||||
SESSION_COOKIE_DOMAIN = None
|
SESSION_COOKIE_DOMAIN = None
|
||||||
else:
|
else:
|
||||||
# Use publicsuffix2 to calculate the correct cookie domain
|
# Use publicsuffix2 to calculate the correct cookie domain
|
||||||
|
@ -156,6 +153,7 @@ else:
|
||||||
# Fallback to the hostname if parsing fails
|
# Fallback to the hostname if parsing fails
|
||||||
SESSION_COOKIE_DOMAIN = hostname
|
SESSION_COOKIE_DOMAIN = hostname
|
||||||
|
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
services:
|
services:
|
||||||
web:
|
web:
|
||||||
build: ./frontend/
|
#build: ./frontend/
|
||||||
#image: ghcr.io/seanmorley15/adventurelog-frontend:latest
|
image: ghcr.io/seanmorley15/adventurelog-frontend:latest
|
||||||
container_name: adventurelog-frontend
|
container_name: adventurelog-frontend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
|
@ -25,8 +25,8 @@ services:
|
||||||
- postgres_data:/var/lib/postgresql/data/
|
- postgres_data:/var/lib/postgresql/data/
|
||||||
|
|
||||||
server:
|
server:
|
||||||
build: ./backend/
|
#build: ./backend/
|
||||||
#image: ghcr.io/seanmorley15/adventurelog-backend:latest
|
image: ghcr.io/seanmorley15/adventurelog-backend:latest
|
||||||
container_name: adventurelog-backend
|
container_name: adventurelog-backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
|
@ -38,7 +38,7 @@ services:
|
||||||
- DJANGO_ADMIN_USERNAME=admin
|
- DJANGO_ADMIN_USERNAME=admin
|
||||||
- DJANGO_ADMIN_PASSWORD=admin
|
- DJANGO_ADMIN_PASSWORD=admin
|
||||||
- DJANGO_ADMIN_EMAIL=admin@example.com
|
- DJANGO_ADMIN_EMAIL=admin@example.com
|
||||||
- PUBLIC_URL='http://localhost:8016' # Match the outward port, used for the creation of image urls
|
- PUBLIC_URL=http://localhost:8016 # Match the outward port, used for the creation of image urls
|
||||||
- CSRF_TRUSTED_ORIGINS=http://localhost:8016,http://localhost:8015 # Comma separated list of trusted origins for CSRF
|
- CSRF_TRUSTED_ORIGINS=http://localhost:8016,http://localhost:8015 # Comma separated list of trusted origins for CSRF
|
||||||
- DEBUG=False
|
- DEBUG=False
|
||||||
- FRONTEND_URL=http://localhost:8015 # Used for email generation. This should be the url of the frontend
|
- FRONTEND_URL=http://localhost:8015 # Used for email generation. This should be the url of the frontend
|
||||||
|
|
|
@ -58,8 +58,10 @@ export const actions: Actions = {
|
||||||
|
|
||||||
// Check if hostname is an IP address
|
// Check if hostname is an IP address
|
||||||
const isIPAddress = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
const isIPAddress = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
||||||
|
const isLocalhost = hostname === 'localhost';
|
||||||
|
const isSingleLabel = hostname.split('.').length === 1;
|
||||||
|
|
||||||
if (!isIPAddress) {
|
if (!isIPAddress && !isSingleLabel && !isLocalhost) {
|
||||||
const parsed = psl.parse(hostname);
|
const parsed = psl.parse(hostname);
|
||||||
|
|
||||||
if (parsed && parsed.domain) {
|
if (parsed && parsed.domain) {
|
||||||
|
|
|
@ -120,8 +120,10 @@ function handleSuccessfulLogin(event: RequestEvent<RouteParams, '/login'>, respo
|
||||||
|
|
||||||
// Check if hostname is an IP address
|
// Check if hostname is an IP address
|
||||||
const isIPAddress = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
const isIPAddress = /^\d{1,3}(\.\d{1,3}){3}$/.test(hostname);
|
||||||
|
const isLocalhost = hostname === 'localhost';
|
||||||
|
const isSingleLabel = hostname.split('.').length === 1;
|
||||||
|
|
||||||
if (!isIPAddress) {
|
if (!isIPAddress && !isSingleLabel && !isLocalhost) {
|
||||||
const parsed = psl.parse(hostname);
|
const parsed = psl.parse(hostname);
|
||||||
|
|
||||||
if (parsed && parsed.domain) {
|
if (parsed && parsed.domain) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue