check-authentication
Analyzes PHP code for authentication issues. Detects weak password handling, insecure sessions, missing auth checks, token vulnerabilities.
Works with
---
name: check-authentication
description: Analyzes PHP code for authentication issues. Detects weak password handling, insecure sessions, missing auth checks, token vulnerabilities.
license: MIT
---
# Authentication Security Check
Analyze PHP code for authentication vulnerabilities.
## Detection Patterns
### 1. Weak Password Handling
```php
// CRITICAL: Plain text password storage
$user->setPassword($_POST['password']);
// CRITICAL: Weak hashing (MD5, SHA1)
$hash = md5($password);
$hash = sha1($password);
$hash = hash('sha256', $password);
// VULNERABLE: No salt
$hash = password_hash($password, PASSWORD_DEFAULT); // OK, but check algo
// CRITICAL: Password in logs
$this->logger->info('Login attempt', ['password' => $password]);
```
### 2. Insecure Session Management
```php
// VULNERABLE: Predictable session ID
session_id('user_' . $userId);
// VULNERABLE: Session fixation
session_start();
$_SESSION['user'] = $userId; // No regenerate_id
// VULNERABLE: Session in URL
session_start();
echo '<a href="page.php?' . SID . '">'; // Session ID in URL
// CORRECT: Regenerate on privilege change
session_regenerate_id(true);
$_SESSION['user'] = $userId;
```
### 3. Missing Authentication Checks
```php
// CRITICAL: No auth check in controller
public function deleteUser(int $id): Response
{
$this->userService->delete($id); // Who can call this?
}
// CRITICAL: Auth bypass via parameter
if ($_GET['admin'] === 'true') {
$this->grantAdminAccess();
}
// VULNERABLE: Relying only on hidden field
if ($_POST['is_admin'] === '1') { }
```
### 4. Token Vulnerabilities
```php
// CRITICAL: Weak token generation
$token = md5(time()); // Predictable
$token = rand(); // Not cryptographically secure
$token = uniqid(); // Not secure
// CRITICAL: Token without expiry
$token = $this->generateToken();
$user->setResetToken($token); // No expiry time
// CRITICAL: Timing attack on comparison
if ($token === $storedToken) { } // Use hash_equals
// CORRECT:
$token = bin2hex(random_bytes(32));
if (hash_equals($storedToken, $token)) { }
```
### 5. Credential Exposure
```php
// CRITICAL: Password in URL
$url = "/login?password=" . urlencode($password);
// CRITICAL: Credentials in error message
throw new AuthException("Invalid password: $password");
// CRITICAL: Auth token in logs
$this->logger->debug('API call', ['token' => $apiToken]);
```
### 6. Remember Me Issues
```php
// CRITICAL: Predictable remember token
$token = md5($userId . time());
setcookie('remember', $token);
// VULNERABLE: No secure flag
setcookie('session_id', $sessionId); // Missing secure, httponly
// CORRECT:
setcookie('remember', $token, [
'expires' => time() + 86400 * 30,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict'
]);
```
### 7. Brute Force Vulnerability
```php
// VULNERABLE: No rate limiting
public function login(string $email, string $password): bool
{
return $this->auth->attempt($email, $password);
// No lockout, no rate limit
}
// VULNERABLE: User enumeration
if (!$user = $this->findByEmail($email)) {
throw new Exception('User not found'); // Different from wrong password
}
```
### 8. OAuth/Social Login Issues
```php
// VULNERABLE: State parameter not validated
$code = $_GET['code'];
$token = $this->oauth->getToken($code); // CSRF possible
// VULNERABLE: Trusting social provider email
$email = $oauthUser->getEmail();
$user = $this->findOrCreateByEmail($email); // Account takeover risk
```
## Grep Patterns
```bash
# Weak hashing
Grep: "md5\(\$|sha1\(\$|hash\(['\"]sha" --glob "**/*.php"
# Missing session_regenerate_id
Grep: "session_start" --glob "**/*.php"
Grep: "session_regenerate_id" --glob "**/*.php"
# Weak random
Grep: "rand\(|mt_rand\(|uniqid\(" --glob "**/*.php"
# Cookie without flags
Grep: "setcookie\([^,]+,[^,]+\)" --glob "**/*.php"
```
## Severity Classification
| Pattern | Severity |
|---------|----------|
| Plain text password | π΄ Critical |
| Weak hashing (MD5/SHA1) | π΄ Critical |
| Missing auth check | π΄ Critical |
| Session fixation | π΄ Critical |
| Predictable tokens | π΄ Critical |
| No rate limiting | π Major |
| User enumeration | π Major |
| Cookie without flags | π‘ Minor |
## Best Practices
### Password Hashing
```php
// Hash
$hash = password_hash($password, PASSWORD_ARGON2ID);
// Verify
if (password_verify($password, $hash)) { }
// Rehash on login if needed
if (password_needs_rehash($hash, PASSWORD_ARGON2ID)) {
$newHash = password_hash($password, PASSWORD_ARGON2ID);
$user->setPassword($newHash);
}
```
### Secure Tokens
```php
$token = bin2hex(random_bytes(32));
$hashedToken = hash('sha256', $token);
// Store $hashedToken, send $token to user
// On verify: hash submitted token and compare
```
### Session Security
```php
session_start([
'cookie_lifetime' => 0,
'cookie_secure' => true,
'cookie_httponly' => true,
'cookie_samesite' => 'Strict',
'use_strict_mode' => true,
]);
```
## Output Format
```markdown
### Authentication Issue: [Description]
**Severity:** π΄/π /π‘
**Location:** `file.php:line`
**CWE:** CWE-287 (Improper Authentication)
**Issue:**
[Description of the authentication weakness]
**Attack Vector:**
[How attacker exploits this]
**Code:**
```php
// Vulnerable code
```
**Fix:**
```php
// Secure implementation
```
```More Security skills
azure-cost
microsoft/azure-skills
Azure cost management: query costs, forecast spending, optimize to reduce waste. WHEN: \"Azure costs\", \"Azure bill\", \"cost breakdown\", \"how much am I spending\", \"forecast spending\", \"optimize costs\", \"reduce spending\", \"orphaned resources\", \"rightsize VMs\", \"cost spike\", \"reduce storage costs\", \"AKS cost\". DO NOT USE FOR: deploying resources, provisioning, diagnostics, or security audits.
entra-app-registration
microsoft/azure-skills
Guides Microsoft Entra ID app registration, OAuth 2.0 authentication, and MSAL integration. USE FOR: create app registration, register Azure AD app, configure OAuth, set up authentication, add API permissions, generate service principal, MSAL example, console app auth, Entra ID setup, Azure AD authentication. DO NOT USE FOR: Key Vault secrets (use azure-keyvault-expiration-audit), general Azure resource security guidance.
azure-messaging
microsoft/azure-skills
Troubleshoot and resolve issues with Azure Messaging SDKs for Event Hubs and Service Bus. Covers connection failures, authentication errors, message processing issues, and SDK configuration problems. WHEN: event hub SDK error, service bus SDK issue, messaging connection failure, AMQP error, event processor host issue, message lock lost, message lock expired, lock renewal, lock renewal batch, send timeout, receiver disconnected, SDK troubleshooting, azure messaging SDK, event hub consumer, service bus queue issue, topic subscription error, enable logging event hub, service bus logging, eventhub python, servicebus java, eventhub javascript, servicebus dotnet, event hub checkpoint, event hub not receiving messages, service bus dead letter, batch processing lock, session lock expired, idle timeout, connection inactive, link detach, slow reconnect, session error, duplicate events, offset reset, receive batch.

