📗
Application Security Manual
  • Intro
    • Start Here
  • Vulnerabilities & Code
    • Injection Vulnerabilities
      • SQL Injection
      • Server-Side Template Injection (SSTI)
      • XPath Injection
      • XML External Entity (XXE) Injection
      • HTTP Request Smuggling
      • Insecure Deserialization
    • Input-based Vulnerabilities
      • Cross-Site Scripting (XSS)
      • Content Security Policy (CSP) Misconfiguration
      • HTTP Parameter Pollution
    • Origin-related Vulnerabilities
      • Cross-Site Request Forgery (CSRF)
      • Server-Side Request Forgery (SSRF)
      • Cross-Origin Resource Sharing (CORS) Misconfiguration
    • Access-related Vulnerabilities
      • Insecure Direct Object References (IDOR)
      • Authorization Code Interception
      • Session Hijacking
      • Insufficient Transport Layer Security (TLS)
      • JSON Web Token (JWT) Vulnerabilities
      • Insecure Client-Side Storage of JWTs
    • Logic & Timing Vulnerabilities
      • Business Logic Flaws
      • Type Juggling (Loose Type Comparison)
      • Timing Attack
  • Design Review
    • Product Security Design Review Framework
    • Security Design Review Checklist
  • Concepts & Culture
    • Implementing SAST Through Security-Driven Developer Culture
    • Security as Code: Baking Security into DevOps
Powered by GitBook
On this page
  • Concept
  • Vulnerable Scenario
  • Explanation
  • Prevention
  • Conclusion
  1. Vulnerabilities & Code
  2. Access-related Vulnerabilities

Authorization Code Interception

Concept

OAuth 2.0 is a widely used authentication and authorization protocol. In the OAuth 2.0 authorization code grant flow, an authorization code is issued by the authorization server and sent back to the client application. This code is then exchanged for an access token. However, if the authorization code is intercepted by an attacker during transmission, it can be used to obtain unauthorized access to the user's resources.

Vulnerable Scenario

Suppose a web application uses OAuth 2.0 for user authentication and authorization. The application redirects the user to the authorization server to obtain an authorization code. After the user grants permission, the authorization server sends the authorization code back to the application via a redirect URI. However, the application uses an insecure channel (HTTP instead of HTTPS) for the redirection.

Example Code (Vulnerable)

# Redirect the user to the authorization server
redirect_uri = "http://example.com/callback"
authorization_url = f"https://auth-server.com/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri={redirect_uri}"
return redirect(authorization_url)

# Receive the authorization code
def callback():
    authorization_code = request.args.get("code")
    # Exchange the authorization code for an access token
    # ...

Explanation

In this example, the application redirects the user to the authorization server using an insecure redirect URI (http://example.com/callback). When the authorization server sends the authorization code back to the application, an attacker can intercept the code by monitoring the network traffic. The attacker can then use the stolen authorization code to exchange it for an access token, gaining unauthorized access to the user's resources.

Prevention

To prevent Authorization Code Interception, consider the following measures:

  1. Always use HTTPS (SSL/TLS) for all communication between the client application, authorization server, and resource server. This ensures that the authorization code is transmitted securely and cannot be intercepted by attackers.

  2. Validate the redirect URI on the server-side to ensure that the authorization code is sent only to the intended client application.

  3. Use the state parameter in the OAuth 2.0 flow to prevent cross-site request forgery (CSRF) attacks.

  4. Implement Proof Key for Code Exchange (PKCE) extension to provide additional security for the authorization code flow.

Example Code (Secure)

# Redirect the user to the authorization server
redirect_uri = "https://example.com/callback"  # Use HTTPS
authorization_url = f"https://auth-server.com/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri={redirect_uri}&state=STATE_VALUE"
return redirect(authorization_url)

# Receive the authorization code
def callback():
    authorization_code = request.args.get("code")
    state = request.args.get("state")
    # Validate the state parameter to prevent CSRF attacks
    # Exchange the authorization code for an access token securely
    # ...

In the secure example, HTTPS is used for the redirect URI to ensure secure transmission of the authorization code. The state parameter is included to prevent CSRF attacks, and it is validated on the server-side. Additionally, PKCE can be implemented to provide an extra layer of security.

Conclusion

Authorization Code Interception is a critical vulnerability in OAuth 2.0 implementations. By intercepting the authorization code during transmission, attackers can gain unauthorized access to user resources. To mitigate this risk, it is essential to use secure communication channels (HTTPS), validate redirect URIs, implement CSRF protection, and consider additional security measures like PKCE. By following best practices and secure coding techniques, developers can ensure the security of their OAuth 2.0 implementations and protect user data from unauthorized access.

Semgrep Rule

This Semgrep rule can be used as a starting point to potentially identify this type of vulnerability.

rules:
  - id: insecure-redirect-uri
    patterns:
      - pattern: |
          $REDIRECT_URI = "=~/^http://"
      - pattern-not: |
          $REDIRECT_URI = "=~/^https://"
    message: "Insecure redirect URI detected. Use HTTPS for secure communication."
    languages:
      - python
    severity: WARNING
PreviousInsecure Direct Object References (IDOR)NextSession Hijacking

Last updated 1 year ago