Skip to content

Beautiful Open-Source Form Builder

Enhance JWT validation configuration and testing (#1140)

Chirag Chhatrala88ebeb7c17c7
diff --git a/api/.env.docker b/api/.env.docker
index 3bfe8ffce..a269f2599 100644
--- a/api/.env.docker
+++ b/api/.env.docker
@@ -31,7 +31,7 @@ AWS_BUCKET=
JWT_TTL=1440
JWT_SECRET=
-JWT_SKIP_IP_UA_VALIDATION=true
+JWT_SKIP_IP_UA_VALIDATION=false
PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE=30
PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR=300
diff --git a/api/.env.example b/api/.env.example
index 627802907..71811de97 100644
--- a/api/.env.example
+++ b/api/.env.example
@@ -55,6 +55,8 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
JWT_TTL=10080
JWT_REMEMBER_TTL=43200
JWT_SECRET=
+# Local development only. Keep false in production/Docker deployments.
+JWT_SKIP_IP_UA_VALIDATION=false
PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE=30
PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR=300
diff --git a/api/tests/Feature/Security/AuthenticateJWTTest.php b/api/tests/Feature/Security/AuthenticateJWTTest.php
new file mode 100644
index 000000000..2540ed8c7
--- /dev/null
+++ b/api/tests/Feature/Security/AuthenticateJWTTest.php
@@ -0,0 +1,72 @@
+<?php
+
+use App\Http\Middleware\AuthenticateJWT;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Hash;
+use Tymon\JWTAuth\Facades\JWTAuth;
+use Tymon\JWTAuth\Payload;
+
+function createJwtMiddlewareRequest(string $userAgent = 'Different Browser'): Request
+{
+ return Request::create('/api/open/forms', 'GET', [], [], [], [
+ 'HTTP_USER_AGENT' => $userAgent,
+ 'HTTP_AUTHORIZATION' => 'Bearer fake-token',
+ ]);
+}
+
+function mockJwtPayloadWithUserAgent(string $userAgent): Payload
+{
+ $payload = Mockery::mock(Payload::class);
+ $payload->shouldReceive('get')->with('impersonating')->andReturn(false);
+ $payload->shouldReceive('get')->with('ua')->andReturn(Hash::make($userAgent));
+
+ JWTAuth::shouldReceive('parseToken->getPayload')->andReturn($payload);
+
+ return $payload;
+}
+
+it('defaults jwt user agent validation to enabled', function () {
+ expect(config('app.jwt_skip_ip_ua_validation'))->toBeFalse();
+});
+
+it('keeps jwt validation enabled in the docker production env template', function () {
+ $envDocker = file_get_contents(base_path('.env.docker'));
+
+ expect($envDocker)->toContain('JWT_SKIP_IP_UA_VALIDATION=false');
+});
+
+it('skips jwt user agent validation before token parsing when configured', function () {
+ config([
+ 'app.jwt_skip_ip_ua_validation' => true,
+ 'app.front_api_secret' => 'front-secret',
+ ]);
+
+ JWTAuth::shouldReceive('parseToken')->never();
+
+ $middleware = new AuthenticateJWT();
+ $request = createJwtMiddlewareRequest('Different Browser');
+
+ $response = $middleware->handle($request, fn () => response('ok'));
+
+ expect($response->getStatusCode())->toBe(200)
+ ->and($response->getContent())->toBe('ok');
+});
+
+it('rejects jwt requests when user agent does not match token claim', function () {
+ config([
+ 'app.jwt_skip_ip_ua_validation' => false,
+ 'app.front_api_secret' => 'front-secret',
+ ]);
+
+ mockJwtPayloadWithUserAgent('Mozilla/5.0 (Valid Browser)');
+ Auth::shouldReceive('invalidate')->once();
+
+ $middleware = new AuthenticateJWT();
+ $request = createJwtMiddlewareRequest('Different Browser');
+
+ $response = $middleware->handle($request, fn () => response('ok'));
+
+ expect($response->getStatusCode())->toBe(401)
+ ->and($response->getData(true)['message'])->toBe('Origin User Agent is invalid');
+});
diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml
index 08aaf46af..388591804 100644
--- a/docker-compose.dev.yml
+++ b/docker-compose.dev.yml
@@ -17,6 +17,7 @@ services:
FRONT_URL: http://localhost:3000
APP_KEY: base64:dD8AFEB45nKZ0VWmIPiW495rDYb+ywRJnCCSdycw0pI=
JWT_SECRET: 38hjE96gYu4ceKSSHNnzgeynMr2m3WkNBN2u7zzpVeiITtxFEloxAjcr7TCKuiiy
+ JWT_SKIP_IP_UA_VALIDATION: "true"
# Database settings
DB_HOST: db
DB_DATABASE: ${DB_DATABASE:-forge}
diff --git a/docs/configuration/environment-variables.mdx b/docs/configuration/environment-variables.mdx
index 533f3d8ca..07c77dbba 100644
--- a/docs/configuration/environment-variables.mdx
+++ b/docs/configuration/environment-variables.mdx
@@ -35,7 +35,7 @@ There are dedicated configuration pages available for more detailed setup instru
| `FRONT_API_SECRET` | Shared secret with the front-end. |
| `JWT_TTL` | Time to live for JSON Web Tokens (JWT). |
| `JWT_SECRET` | Secret key used to sign JWTs. |
-| `JWT_SKIP_IP_UA_VALIDATION` | Set to `true` to disable JWT IP and User Agent validation (defaults to `false`). Useful for dynamic IPs. |
+| `JWT_SKIP_IP_UA_VALIDATION` | Set to `true` to disable JWT User Agent validation (defaults to `false`). Keep this disabled in production and Docker deployments. For local Docker development, `docker-compose.dev.yml` sets this to `true` automatically. |
| `PUBLIC_UPLOADS_RATE_LIMIT_PER_MINUTE` | Maximum public upload requests allowed per client IP or authenticated user per route each minute. Defaults to `30`. |
| `PUBLIC_UPLOADS_RATE_LIMIT_PER_HOUR` | Maximum public upload requests allowed per client IP or authenticated user per route each hour. Defaults to `300`. |
| `OIDC_FORCE_LOGIN` | When set to `true`, password-based authentication is disabled and users must authenticate via OIDC SSO. See [Force Login Mode](../configuration/oidc-sso#7-force-login-mode) for details. |
diff --git a/docs/deployment/docker-development.mdx b/docs/deployment/docker-development.mdx
index 515d21ed1..3a604dc44 100644
--- a/docs/deployment/docker-development.mdx
+++ b/docs/deployment/docker-development.mdx
@@ -41,7 +41,10 @@ OpnForm provides a minimal Docker-based development environment optimized for lo
<Note>
Development mode doesn't require .env files - all configuration is
- embedded in docker-compose.dev.yml for simplicity.
+ embedded in docker-compose.dev.yml for simplicity. This includes
+ `JWT_SKIP_IP_UA_VALIDATION=true`, which disables JWT User Agent checks
+ for easier local testing. Production Docker deployments should keep this
+ setting disabled.
</Note>
3. Access your development environment:
diff --git a/scripts/docker-setup.sh b/scripts/docker-setup.sh
index 201667c70..3701ea842 100755
--- a/scripts/docker-setup.sh
+++ b/scripts/docker-setup.sh
@@ -36,12 +36,54 @@ done
cd "$PROJECT_ROOT"
+is_truthy_env_value() {
+ local value="$1"
+
+ value="${value%%#*}"
+ value="$(printf '%s' "$value" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
+ value="${value#\"}"
+ value="${value%\"}"
+ value="${value#\'}"
+ value="${value%\'}"
+ value="${value#(}"
+ value="${value%)}"
+ value="$(printf '%s' "$value" | tr '[:upper:]' '[:lower:]')"
+
+ case "$value" in
+ true|1|yes|on) return 0 ;;
+ *) return 1 ;;
+ esac
+}
+
+jwt_skip_validation_disabled_in_env() {
+ local env_file="$1"
+ local line value
+
+ if [ ! -f "$env_file" ]; then
+ return 1
+ fi
+
+ line="$(grep -E '^[[:space:]]*JWT_SKIP_IP_UA_VALIDATION[[:space:]]*=' "$env_file" | tail -n 1 || true)"
+ if [ -z "$line" ]; then
+ return 1
+ fi
+
+ value="${line#*=}"
+ is_truthy_env_value "$value"
+}
+
echo -e "${BLUE}Starting OpnForm Docker setup...${NC}"
# Run the environment setup script with --docker flag (only for production)
if [ "$DEV_MODE" = false ]; then
echo -e "${GREEN}Setting up environment files...${NC}"
bash "$SCRIPT_DIR/setup-env.sh" --docker
+
+ if jwt_skip_validation_disabled_in_env "$PROJECT_ROOT/api/.env"; then
+ echo -e "${YELLOW}Warning: JWT User Agent validation is disabled in api/.env.${NC}"
+ echo -e "${YELLOW}Set JWT_SKIP_IP_UA_VALIDATION=false for production Docker deployments.${NC}"
+ exit 1
+ fi
else
echo -e "${GREEN}Development mode - skipping .env generation (using docker-compose environment variables)${NC}"
fi