CVE-2024-6839
Executive Summary
CVE-2024-6839 is an improper regex path matching vulnerability in the corydolphin/flask-cors Python library, version 4.0.1. The flaw arises from the plugin’s regex prioritization logic, which incorrectly favors longer regex patterns over more specific ones when matching URL paths. This can lead to less restrictive CORS policies being applied to sensitive endpoints, potentially allowing unauthorized cross-origin access to confidential data or functionality. The CVSS score is 4.3 (Medium), with an EPSS probability of 0.0% indicating low exploitation likelihood at this time. No public exploit code or patch is currently available.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
This vulnerability affects the flask-cors PyPI package. As a widely used extension for Flask applications, any application using version 4.0.1 is at risk. The lack of a patch means that supply chain security is compromised for all downstream consumers. Organizations should audit their dependencies for this version and consider temporary mitigations until an official fix is released.
CVSS Breakdown & Threat Level
- CVSS Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N
- Base Score: 4.3 (Medium)
- Attack Vector: Network
- Attack Complexity: Low
- Privileges Required: None
- User Interaction: None
- Scope: Unchanged
- Confidentiality Impact: Low
- Integrity Impact: None
- Availability Impact: None
The threat level is Medium due to the potential for unauthorized data exposure, though exploitation requires specific conditions (e.g., a misconfigured CORS policy that exposes sensitive endpoints).
Deep Dive & Mechanics
The vulnerability lies in the flask-cors library’s path matching algorithm. When multiple regex patterns are defined for CORS rules, the library incorrectly prioritizes longer patterns over more specific ones. For example, a pattern like /api/.* (longer) might be matched before a more specific pattern like /api/admin (shorter but more restrictive). This can result in a less restrictive CORS policy (e.g., allowing all origins) being applied to sensitive endpoints like /api/admin, bypassing intended access controls.
The core issue is in the match_path function, which uses a simple length-based priority instead of a specificity-based priority. This is a classic regex precedence bug.
Vulnerable vs. Patched Code
Vulnerable Code (flask-cors 4.0.1):
# In cors.py or similar
def match_path(path, patterns):
# Incorrectly prioritizes longer patterns
matched = None
for pattern in patterns:
if re.match(pattern, path):
if matched is None or len(pattern) > len(matched):
matched = pattern
return matched
Patched Code (Hypothetical Fix):
# Correctly prioritizes more specific patterns
def match_path(path, patterns):
matched = None
for pattern in patterns:
if re.match(pattern, path):
if matched is None or pattern_specificity(pattern) > pattern_specificity(matched):
matched = pattern
return matched
def pattern_specificity(pattern):
# Count non-wildcard characters as a measure of specificity
return len(pattern) - pattern.count('.*') - pattern.count('*')
Exploitation & Lab Setup
Red Team
Prerequisites:
- A Flask application using
flask-corsversion 4.0.1 with multiple CORS patterns. - A sensitive endpoint (e.g.,
/api/admin) that should have a restrictive CORS policy.
Exploitation Steps:
- Identify the target application’s CORS configuration (e.g., via
OPTIONSrequests). - Craft a cross-origin request from a malicious origin to the sensitive endpoint.
- Observe that the less restrictive CORS policy is applied due to the regex prioritization bug.
Example Exploit (JavaScript):
// Malicious origin: https://attacker.com
fetch('https://victim.com/api/admin', {
method: 'GET',
credentials: 'include'
}).then(response => {
// If CORS allows, data is exfiltrated
console.log(response.json());
});
Blue Team
Detection:
- Monitor for unexpected
Originheaders in requests to sensitive endpoints. - Log and alert on CORS policy mismatches.
Mitigation:
- Temporarily disable CORS for sensitive endpoints or use a custom middleware.
- Upgrade to a patched version once available.
- Implement additional authentication/authorization checks.
Indicators of Compromise (IOCs)
- Network: HTTP requests with
Originheaders from unexpected domains to sensitive endpoints. - Application Logs: CORS policy mismatches logged by the application.
- Dependency: Presence of
flask-cors==4.0.1inrequirements.txtorPipfile.
Detection Engineering
YARA Rule
rule flask_cors_vuln_4_0_1 {
meta:
description = "Detects flask-cors version 4.0.1 with regex prioritization bug"
author = "Valters IT"
date = "2025-04-10"
hash = "sha256:examplehash"
strings:
$version = "4.0.1"
$cors_import = "from flask_cors import CORS"
$match_path = "def match_path"
condition:
$version and $cors_import and $match_path
}
Sigma Rule
title: CVE-2024-6839 - Flask-CORS Regex Prioritization Vulnerability
id: 12345678-1234-1234-1234-123456789abc
status: experimental
description: Detects potential exploitation of CVE-2024-6839 via anomalous CORS headers
logsource:
category: webserver
product: flask
detection:
selection:
http.response.header|contains: 'Access-Control-Allow-Origin'
http.request.header|contains: 'Origin'
condition: selection
falsepositives:
- Legitimate cross-origin requests
level: medium
Suricata Rule
alert http any any -> any any (msg:"CVE-2024-6839 - Flask-CORS Regex Prioritization Exploit"; flow:to_server; content:"Origin"; http_header; content:"Access-Control-Allow-Origin"; http_header; sid:1000001; rev:1;)
Remediation & Mitigation
- Immediate: Remove or restrict CORS for sensitive endpoints using a custom middleware.
- Short-term: Monitor for updates from the
flask-corsmaintainer and apply patches as soon as available. - Long-term: Implement a Web Application Firewall (WAF) rule to block unexpected origins.
- Code Review: Audit all CORS configurations for regex specificity issues.
Valters IT
Senior DevSecOps & Cyber Threat Intelligence Expert