Pythonにおけるassertの危険性
Dhruv Patel
2022年8月18日
13 分で読めますPythonコードのtry/catch文、if/else文、assert statements、print()文を
assert文をprint文をprint文とは
このassertを
Pythonのassert文を使う
assertの
assert文の
assert expression上記のTrueである
式がFalseの
>>> assert 'hi' == 'there'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionErrorスタックトレースをAssertionErrorに
>>> assert 'hi' == 'there', 'Values do not match!'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: Values do not match!assert文を__debug__変数はTrueにFalseに-Oを__debug__変数がFalseに
assertは
要約 — assert文は
例外を
発生させて アプリケーションを 停止し、 本番環境の 障害を 引き起こす 可能性が ある。 デバッグモードが
無効で、 Pythonランタイムが assert文を 黙って スキップした 場合、 意図しない 副作用 (脆弱性を 含む 可能性が あります)が アプリケーションに 発生する。
assertを安全に使う
単利を
まず、安定版のsafe_assert_example.pyと
次のsafe_assert_example.pyに
def simpleInterest(p, t, r):
print('The principal is', p)
assert isinstance(p, int) and p > 0, "Principal must be a positive integer"
print('The time period is', t)
assert isinstance(t, int) and t > 0, "Time must be a positive integer"
print('The rate of interest is', r)
assert isinstance(r, int) and r > 0, "Rate of interest must be a positive integer"
simpleInterest = (p * t * r) / 100
print('The Simple Interest is', simpleInterest)
return simpleInterest
simpleInterest(3, 5, 8)このsimpleInterest関数を
利息をp、t、rの
実際に
python safe_assert_example.py以下のように、
PS E:\Sample\Extra\Assert Demo> python safe_assert_example.py
The principal is 3
The time period is 5
The rate of interest is 8
The Simple Interest is 1.2このように、
では、tの5からtime」に
simpleInterest(3, "time", 8)次に、
python safe_assert_example.py今度は、
PS E: \Sample\Extra\Assert Demo» python safe assert example.py
The principal is 3
The time period is time
Traceback (most recent call last):
File "E: \Sample\Extra\Assert Demo\safe assert example.py", line 12, in <module> simpleInterest(3, "time", 8)
File "E: \Sample\Extra\Assert Demo\safe assert example.py", line 5, in simpleInterest assert isinstance(t, int) and t > 0, "Time must be a positive integer"
AssertionError: Time must be a positive integerこのAssertionErrorが
この
最適化モードでPYTHONOPTIMIZEコマンドラインフラグ-Oを
tの=」の
python -O safe_assert_example.py次の
PS E:\Sample\Extra\Assert Demo› python -0 safe assert example.py
The principal is 3
The time period is time
The rate of interest is 8
Traceback (most recent call last):
File "E:\Sample\Extra\Assert Demo\safe assert example.py", line 12, in <module> simpleInterest(3, "time", 8)
File "E:\Sample\Extra\Assert Demo\safe assert example.py", line 8, in simpleInterest simpleInterest = (p * t * r)/100
TypeError: unsupported operand type(s) for /: 'str' and 'int'関数のAssertionErrorがTypeErrorを
型ヒントを
追加して、 コードの 実行前に simpleInterestへの不正な 呼び出しを 検出しやすくする。 実行時に
型を 確認するには、 アサーションの 代わりに if文を使う。
assertを危険な方法で使う
assertは__debug__変数がTrueの
上の-Oで__debug__がFalseに
その
本番環境で
次に、unsafe_assert_example.pyと
def authorize_admin_user(user_roles):
assert isinstance(user_roles,list) and user_roles != [], "No user roles found"
assert 'admin' in user_roles, "No admin role found."
print("You have full access to the application.")
authorize_admin_user(['admin','user'])authorize_admin_user関数は
ファイルのauthorize_admin_user関数を
Run python unsafe_assert_example.py:
$ python unsafe_assert_example.py
You have full access to the application予想どおり、
次に、unsafe_assert_example.pyの
authorize_admin_user(['user'])プログラムを
$ python unsafe_assert_example.py
Traceback (most recent call last):
File "C:\Projects\ContentLabEditing\python\unsafe_assert_example.py", line 6, in <module>
authorize_admin_user(['user'])
File "C:\Projects\ContentLabEditing\python\unsafe_assert_example.py", line 3, in authorize_admin_user
assert 'admin' in user_roles, "No admin role found."
AssertionError: No admin role found.予想どおり、AssertionErrorを
次に、
python -O unsafe_assert_example.py出力はadminロールが
PS E:\Sample\Extra\Assert Demo> python -0 unsafe_assert_example.py
You have full access to the application
You have full access to the application
PS E:\Sample\Extra\Assert Demo>さらに、
関数authorize_admin_user([1,2])にpython -O unsafe_assert_example.pyで
プログラムの
単利計算の
この
これらの
assertの使用上の注意点
assertを
本番環境で
assertの
assertを
Pythonの開発とセキュリティに関するその他のリソース
無料の
Capture the Flagを 始めよう
オンデマンドの



