搜索
首页后端开发Python教程拒绝服务正则表达式破坏了 FastAPI 安全性

欢迎各位开发者!在这篇博文中,我们将深入研究应用程序安全领域,特别关注可能恶化 FastAPI 安全性的漏洞:由不安全的正则表达式 (regex) 导致的拒绝服务 (DoS)。我们将探讨构造不良的正则表达式如何导致所谓的正则表达式拒绝服务 (ReDoS)(一种 DoS 攻击),以及如何使用强大的开发人员安全工具 Snyk 来识别和缓解这些漏洞。

了解 ReDoS 对 Python 中 FastAPI 安全性的影响

Python 是最流行的编程语言之一,拥有庞大的包和库生态系统。虽然这些软件包让我们作为开发人员的生活变得更轻松,但如果没有得到适当的保护,它们也会带来潜在的风险。随着软件开发的快速发展,软件包经常更新、新版本发布,有时会在不知不觉中引入安全风险。

其中一个风险是潜在的 ReDoS 攻击,这是一种 DoS 攻击,攻击者向需要很长时间才能评估的正则表达式提供恶意输入。这会导致应用程序变得无响应或显着减慢,这可能会产生严重的影响,从用户体验下降到应用程序完全失败。

import re
pattern = re.compile("^(a+)+$")
def check(input):
    return bool(pattern.match(input))
check("a" * 3000 + "!")

在上面的代码中,正则表达式^(a+)+$容易受到ReDoS攻击。如果攻击者提供一串“a”后跟一个非“a”字符,则正则表达式需要很长时间来评估,从而有效地导致 DoS。

Snyk 如何保护您的 FastAPI Python 应用程序

Snyk 是一款开发人员优先的安全工具,可以扫描您的 Python 代码以查找潜在的 ReDoS 漏洞。它提供已识别漏洞的详细报告并推荐最合适的修复方案。

# After installing Snyk and setting up the Snyk CLI
# you can scan your project:
$ snyk test 

此命令将扫描您的第三方依赖项清单(通常在requirements.txt 文件中),并提供所有已识别漏洞的报告,包括潜在的ReDoS 漏洞。立即注册免费的 Snyk 帐户,开始扫描您的 Python 项目是否存在 ReDoS 和其他漏洞。

了解此类漏洞的影响以及如何缓解它们对于维护安全的 Python 应用程序至关重要。这就是 Snyk 这样的工具派上用场的地方。 Snyk Open Source 可以帮助识别和修复 Python 包中的安全漏洞,包括可能导致 ReDoS 攻击的不安全正则表达式。

让我们仔细看看如何使用 Snyk 识别和缓解 FastAPI Python Web 应用程序中的此类漏洞。

FastAPI 安全漏洞 CVE-2024-24762

FastAPI 是一个现代的高性能 Web 框架,用于基于标准 Python 类型提示使用 Python 构建 API。它的主要特点是速度以及快速、轻松地构建强大 API 的能力,使其成为需要构建高性能 RESTful API 的 Python 开发人员的热门选择。

FastAPI 通过提供开箱即用的路由机制、序列化/反序列化和验证来简化构建 API 的过程。它构建在用于 Web 部分的 Python 项目 Starlette 和用于数据部分的 Pydantic 之上。这允许开发人员利用 Python 3.6 及更高版本中提供的异步功能。

作为示例,可以使用以下代码片段来完成使用 FastAPI Python Web 应用程序创建简单的 API:

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
    return {"Hello": "World"}

虽然 FastAPI 是一个强大而敏捷的 API 开发工具,但它并非没有漏洞。其中之一是 CVE-2024-24762 漏洞。这是一个拒绝服务漏洞,源自 Python 包 python-multipart 使用的正则表达式。

python-multipart 依赖项是一个用于解析多部分/表单数据的 Python 库。它通常用作 FastAPI 中的依赖项来管理表单数据。

当攻击者发送恶意字符串,导致 python-multipart 中的正则表达式消耗大量 CPU,从而导致拒绝服务 (DoS) 时,就会出现该漏洞。这也称为正则表达式拒绝服务 (ReDoS)。

Python 开发人员如何缓解此漏洞?第一步是识别项目中的漏洞。这可以使用 Snyk CLI 工具来完成。

$ snyk test

检测此类漏洞需要扫描项目的依赖项,这将提供项目依赖项中所有漏洞的报告。

Snyk 测试命令的输出发现了漏洞:

snyk test

Testing /Users/lirantal/projects/repos/fastapi-vulnerable-redos-app...

Tested 13 dependencies for known issues, found 1 issue, 1 vulnerable path.

Issues to fix by upgrading dependencies:

  Upgrade fastapi@0.109.0 to fastapi@0.109.1 to fix
  ✗ Regular Expression Denial of Service (ReDoS) (new) [High Severity][https://security.snyk.io/vuln/SNYK-PYTHON-FASTAPI-6228055] in fastapi@0.109.0
    introduced by fastapi@0.109.0

Organization:      liran.tal
Package manager:   pip
Target file:       requirements.txt
Project name:      fastapi-vulnerable-redos-app

要修复该漏洞,您可以升级到已修复该漏洞的 python-multipart 包和 fastapi 的较新版本,这些版本是 Snyk 建议的。

Building and breaking FastAPI security: A step-by-step guide

Our first step is to set up a new Python project. We'll need to install FastAPI, along with a server to host it on. Uvicorn is a good choice for a server because it is lightweight and works well with FastAPI.

Start by installing FastAPI, python-multipart, and Uvicorn with pip:

pip install fastapi==0.109.0 uvicorn python-multipart==0.0.6

Next, create a new directory for your project, and inside that directory, create a new file for your FastAPI application. You can call it main.py.

Writing the FastAPI Python code

Now we're ready to write our FastAPI application code. Open main.py and add the following Python code:

from typing import Annotated
from fastapi.responses import HTMLResponse
from fastapi import FastAPI,Form
from pydantic import BaseModel

class Item(BaseModel):
    username: str

app = FastAPI()

@app.get("/", response_class=HTMLResponse)
async def index():
    return HTMLResponse("Test", status_code=200)

@app.post("/submit/")
async def submit(username: Annotated[str, Form()]):
    return {"username": username}

@app.post("/submit_json/")
async def submit_json(item: Item):
    return {"username": item.username}

This simple FastAPI application has several routes (/), including /submit, which uses a multipart form. When a POST request is received, the submit route returns the username that was submitted.

Starting the server and running the application

With our FastAPI application code written, we can now start the Uvicorn server and run our application.

Use the following command to start the server:

uvicorn main:app --reload

You should see an output indicating that the server is running. You can test your application by navigating to http://localhost:8000 in your web browser. The message "Test" should be displayed on the page.

Breaking FastAPI security with a ReDoS attack

Now that our FastAPI application is running, we can test it for vulnerabilities. We'll use a ReDoS attack payload in the HTTP request to exploit the vulnerability in the python-multipart package that parses the content-type header value.

If you have the curl program installed, run the following command in your terminal:

curl -v -X 'POST' -H $'Content-Type: application/x-www-form-urlencoded; !=\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' --data-binary 'input=1' 'http://localhost:8000/submit/'

Securing your FastAPI application with Snyk

As you saw by now, open source dependencies play a key role in building Python applications. However, these third-party dependencies can sometimes be a breeding ground for vulnerabilities, thus posing significant security threats. In this context, Snyk Open Source emerges as a robust tool that helps developers identify and fix security issues effectively.

Imagine you could quickly find FastAPI security vulnerabilities already in the IDE panel when you write Python code instead of waiting until security scanners pick this up at a later stage.

The Snyk IDE extension is free, and if you’re using PyCharm, you can search for Snyk in the Plugins view and download it directly from there. If you’re using VS Code you can similarly find it in the Extensions marketplace right from the IDE.

A denial of service Regex breaks FastAPI security

Introduction to Snyk Open Source and its capabilities

Snyk Open Source is a powerful tool used for uncovering and addressing vulnerabilities in open source dependencies and container images. It is designed to integrate easily with the existing codebase and CI/CD systems, making it a handy tool for developers. It provides a comprehensive database of known vulnerabilities, enabling developers to proactively address potential breaches in security.

Step-by-step guide on how to scan Python dependencies for vulnerabilities with Snyk

To scan Python dependencies for vulnerabilities with Snyk, you first need to install the Snyk CLI. You can do this using one of the methods in the guide, or if you have a Node.js environment, you can quickly install Snyk with npm install -g snyk and then run snyk auth to authenticate.

Once installed, you can use the snyk test command to check your Python project for vulnerabilities:

snyk test --all-projects

Snyk will then scan all your dependencies and compare them against its vulnerability database. If any issues are found, Snyk will provide a detailed report with information about the vulnerability, its severity, and possible fixes.

Monitoring your projects with Snyk is crucial to maintain the security of your application. With Snyk, not only can you detect vulnerabilities, but you can also apply automated fixes, which can save you time and resources.

In addition, Snyk offers vulnerability alerts that notify you about new vulnerabilities that may affect your projects. This allows you to stay one step ahead and fix security issues before they can be exploited.

With the snyk monitor command, you can take a snapshot of your current project dependencies and monitor them for vulnerabilities:

snyk monitor

How to integrate Snyk with Git repositories

Integrating Snyk with your Git repositories allows you to automatically scan every commit for vulnerabilities. This can be done by adding Snyk as a webhook in your repository settings.

A denial of service Regex breaks FastAPI security
完成此操作后,每次推送到您的存储库都会触发 Snyk 扫描,帮助您尽早捕获并修复漏洞。

总而言之,Snyk Open Source 是维护 Python 项目安全性的宝贵工具。通过扫描漏洞、监控项目以及与 Git 存储库集成,Snyk 使您能够维护强大、安全的代码库。如果您还没有注册一个免费的 Snyk 帐户,请立即开始保护您的应用程序。

以上是拒绝服务正则表达式破坏了 FastAPI 安全性的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
学习Python:2小时的每日学习是否足够?学习Python:2小时的每日学习是否足够?Apr 18, 2025 am 12:22 AM

每天学习Python两个小时是否足够?这取决于你的目标和学习方法。1)制定清晰的学习计划,2)选择合适的学习资源和方法,3)动手实践和复习巩固,可以在这段时间内逐步掌握Python的基本知识和高级功能。

Web开发的Python:关键应用程序Web开发的Python:关键应用程序Apr 18, 2025 am 12:20 AM

Python在Web开发中的关键应用包括使用Django和Flask框架、API开发、数据分析与可视化、机器学习与AI、以及性能优化。1.Django和Flask框架:Django适合快速开发复杂应用,Flask适用于小型或高度自定义项目。2.API开发:使用Flask或DjangoRESTFramework构建RESTfulAPI。3.数据分析与可视化:利用Python处理数据并通过Web界面展示。4.机器学习与AI:Python用于构建智能Web应用。5.性能优化:通过异步编程、缓存和代码优

Python vs.C:探索性能和效率Python vs.C:探索性能和效率Apr 18, 2025 am 12:20 AM

Python在开发效率上优于C ,但C 在执行性能上更高。1.Python的简洁语法和丰富库提高开发效率。2.C 的编译型特性和硬件控制提升执行性能。选择时需根据项目需求权衡开发速度与执行效率。

python在行动中:现实世界中的例子python在行动中:现实世界中的例子Apr 18, 2025 am 12:18 AM

Python在现实世界中的应用包括数据分析、Web开发、人工智能和自动化。1)在数据分析中,Python使用Pandas和Matplotlib处理和可视化数据。2)Web开发中,Django和Flask框架简化了Web应用的创建。3)人工智能领域,TensorFlow和PyTorch用于构建和训练模型。4)自动化方面,Python脚本可用于复制文件等任务。

Python的主要用途:综合概述Python的主要用途:综合概述Apr 18, 2025 am 12:18 AM

Python在数据科学、Web开发和自动化脚本领域广泛应用。1)在数据科学中,Python通过NumPy、Pandas等库简化数据处理和分析。2)在Web开发中,Django和Flask框架使开发者能快速构建应用。3)在自动化脚本中,Python的简洁性和标准库使其成为理想选择。

Python的主要目的:灵活性和易用性Python的主要目的:灵活性和易用性Apr 17, 2025 am 12:14 AM

Python的灵活性体现在多范式支持和动态类型系统,易用性则源于语法简洁和丰富的标准库。1.灵活性:支持面向对象、函数式和过程式编程,动态类型系统提高开发效率。2.易用性:语法接近自然语言,标准库涵盖广泛功能,简化开发过程。

Python:多功能编程的力量Python:多功能编程的力量Apr 17, 2025 am 12:09 AM

Python因其简洁与强大而备受青睐,适用于从初学者到高级开发者的各种需求。其多功能性体现在:1)易学易用,语法简单;2)丰富的库和框架,如NumPy、Pandas等;3)跨平台支持,可在多种操作系统上运行;4)适合脚本和自动化任务,提升工作效率。

每天2小时学习Python:实用指南每天2小时学习Python:实用指南Apr 17, 2025 am 12:05 AM

可以,在每天花费两个小时的时间内学会Python。1.制定合理的学习计划,2.选择合适的学习资源,3.通过实践巩固所学知识,这些步骤能帮助你在短时间内掌握Python。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用