Fix Weak API Authentication in Symfony
Why weak API auth happens (fast, but fragile)
Quick launches often ship API keys in URLs, no throttling, and permissive CORS. Let’s replace that with JSON login + JWT, rate limiting, and strict CORS—then validate with the Website Vulnerability Scanner online free.
What not to do
// ❌ Token in query string; no identity, no throttling
#[Route('/api/report', methods: ['GET'])]
public function report(Request $r): JsonResponse {
if ($r->query->get('token') !== $_ENV['API_TOKEN']) {
return new JsonResponse(['error'=>'unauthorized'], 401);
}
return new JsonResponse(['ok'=>true]);
}
Secure baseline: JSON login + JWT (stateless)
Install and generate keys:
composer require lexik/jwt-authentication-bundle
php bin/console lexik:jwt:generate-keypair
Minimal security config:
# config/packages/security.yaml
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
providers:
app_user_provider:
entity: { class: App\Entity\User, property: email }
firewalls:
main:
pattern: ^/api
stateless: true
provider: app_user_provider
json_login:
check_path: /api/login
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
login_throttling: { max_attempts: 5, interval: '1 minute' }
access_control:
- { path: ^/api/login, roles: PUBLIC_ACCESS }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
JWT bundle settings:
# config/packages/lexik_jwt_authentication.yaml
lexik_jwt_authentication:
secret_key: '%kernel.project_dir%/config/jwt/private.pem'
public_key: '%kernel.project_dir%/config/jwt/public.pem'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: 3600
Add rate limits (beyond login)
# config/packages/rate_limiter.yaml
framework:
rate_limiter:
api_global:
policy: 'token_bucket'
limit: 60
rate: { interval: '1 minute', amount: 60 }
Use it in controllers for sensitive endpoints.
Lock down CORS
# config/packages/nelmio_cors.yaml
nelmio_cors:
defaults:
allow_origin: ['https://app.example.com']
allow_headers: ['Content-Type','Authorization']
allow_methods: ['GET','POST','PUT','DELETE','OPTIONS']
max_age: 3600
paths:
'^/api/': ~
Quick test flow
# 1) Get JWT
TOKEN=$(curl -sS -X POST https://yourdomain.com/api/login \
-H "Content-Type: application/json" \
-d '{"email":"jane@example.com","password":"secret"}' | jq -r .token)
# 2) Call protected route
curl -H "Authorization: Bearer $TOKEN" https://yourdomain.com/api/me
Insert screenshots (place these inside the body)
๐ธ Our Website Vulnerability Scanner Tool homepage
![]() |
Screenshot of the free tools webpage where you can access security assessment tools. |
๐ธ Sample report from our tool to check Website Vulnerability
![]() |
Sample vulnerability assessment report generated with our free tool, providing insights into possible vulnerabilities. |
Hardening checklist
-
Use JSON login + JWT (short TTL).
-
Enable login throttling and custom rate limits.
-
Keep JWT keys outside web root; store passphrase in secrets.
-
Enforce Authorization: Bearer (no tokens in URLs).
-
Tight CORS with explicit origins/headers.
Run a fresh scan after changes: https://free.pentesttesting.com/
More how-tos on our blog: https://www.pentesttesting.com/blog/
Services from Pentest Testing Corp.
Managed IT Services
Keep auth secure in production with patching, monitoring, and incident response.
https://www.pentesttesting.com/managed-it-services/
AI Application Cybersecurity
Harden AI endpoints, tokens, and data flows.
https://www.pentesttesting.com/ai-application-cybersecurity/
Offer Cybersecurity to Your Clients
Agencies/MSPs: resell or bundle assessments and hardening.
https://www.pentesttesting.com/offer-cybersecurity-service-to-your-client/
Stay updated
Subscribe on LinkedIn https://www.linkedin.com/build-relation/newsletter-follow?entityUrn=7327563980778995713
Comments
Post a Comment