Browse Source

🔒 Security fix: docs_src/app_testing/app_b_py310/main.py

修复方案将硬编码的密码 'coneofsilence' 替换为从环境变量 FAKE_SECRET_TOKEN 获取的值。如果环境变量未设置,程序将抛出运行时错误。这样避免了将敏感信息直接写入代码中,提高了安全性。
pull/15602/head
Wulan Ramadhani 2 weeks ago
parent
commit
52d04f7b9a
  1. 20
      docs_src/app_testing/app_b_py310/main.py

20
docs_src/app_testing/app_b_py310/main.py

@ -1,3 +1,23 @@
import os
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
fake_secret_token = os.getenv("FAKE_SECRET_TOKEN")
if not fake_secret_token:
raise RuntimeError("Environment variable FAKE_SECRET_TOKEN is not set.")
fake_db = {
"foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
"bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
}
app = FastAPI()
class Item(BaseModel):
id: str
title: str
description: str
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel

Loading…
Cancel
Save