Python3.13 で VERIFY_X509_STRICT により Basic Constraints が Critical でない場合の自己署名証明書を使った時にエラーを出さない方法

Python
2025-05-21 11:50 (2日前) ytyng
View in English

注意: この記事は、本来デフォルトで設定されているセキュリティ水準を意図的に下げる内容が書かれています。リスクを理解し、リスクを許容できる環境下でのみ実施してください。

自己署名のSSL証明書を使ったHTTPサーバーに対して、 Python の Requests や httpx でリクエストしようとした場合、

import httpx

ca_file_path = os.path.join(os.path.dirname(__file__), 'my-ca.crt')
content = httpx.get('https://my-internal-server.example.com/', verify=ca_file_path)
import requests

ca_file_path = os.path.join(os.path.dirname(__file__), 'my-ca.crt')

response = requests.get('https://my-internal-server.example.com/', verify=ca_file_path)

このようなコードになります。

Python3.12 までは問題ありませんが、 Python3.13 の場合、CA 証明書の X509v3 Basic Constraints が Critical になっていない場合は

httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Basic Constraints of CA cert not marked critical (_ssl.c:1020)

このエラーが出てしまいます。

Python3.13 で、かつ CA 証明書を修正せず、とりいそぎエラーを回避したい場合は

import httpx
import ssl

ca_file_path = os.path.join(os.path.dirname(__file__), 'my-ca.crt')

context = ssl.create_default_context()
context.verify_flags &= ~ssl.VERIFY_X509_STRICT
context.load_verify_locations(ca_file_path)

response = httpx.get('https://my-internal-server.example.com/', verify=context)

これで回避できます。

※ 脆弱です

現在未評価
タイトルとURLをコピー
著者は、アプリケーション開発会社 Cyberneura を運営しています。
開発相談をお待ちしています。

アーカイブ

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011