본문 바로가기

개발하자

파이썬 밴디트 보안 문제 보고서에서 일부 문제를 무시하거나 건너뛰는 방법은 무엇입니까?

반응형

파이썬 밴디트 보안 문제 보고서에서 일부 문제를 무시하거나 건너뛰는 방법은 무엇입니까?

나는 많은 오류를 가지고 있다.

>> Issue: [B703:django_mark_safe] Potential XSS on mark_safe function.
   Severity: Medium   Confidence: High
   Location: ...
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b703_django_mark_safe.html
54 return mark_safe(f'<a href="{url}" target="_blank">{title}</a>')

>> Issue: [B308:blacklist] Use of mark_safe() may expose cross-site scripting vulnerabilities and should be reviewed.
   Severity: Medium   Confidence: High
   Location: ...
   More Info: https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b308-mark-safe
54 return mark_safe(f'<a href="{url}" target="_blank">{title}</a>')

그리고 그런 대사를 건너뛰거나 무시할 수 있는 방법이 있는지 궁금합니다. 사용하는 것이 위험할 수 있다는 것은 알지만, 위험을 감수하고 싶다면 어떻게 해야 할까요? 예를 들어, 이 방법은 장고 관리자에서 사용자 지정 링크를 표시하는 유일한 방법이기 때문에, 나는 그것을 하지 않고 하는 다른 방법을 모른다.




답이 있어요.

두 가지 방법:

  1. 명령줄에 --skip 인수를 사용하여 B703 및 B308을 건너뛸 수 있습니다.
  2. 또는 생략할 줄에 주석을 붙일 수 있습니다.

https://bandit.readthedocs.io/en/latest/config.html#exclusions




다음을 사용하여 여러 줄에 주석을 달기 위한 미리 보기:

주어진 경우:

li_without_nosec = [
    "select * from %s where 1 = 1 "
    % "foo"
]

li_nosec_at_start_works = [  # nosec - ✅ and you can put a comment
    "select * from %s where 1 = 1 "
    % "foo"
]  

# nosec - there's an enhancement request to marker above line
li_nosec_on_top_doesntwork = [  
    "select * from %s where 1 = 1 "
    % "foo"
]  

li_nosec_at_end_doesntwork = [
    "select * from %s where 1 = 1 "
    % "foo"
]  # nosec 

출력:

>> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
   Severity: Medium   Confidence: Low
   Location: test.py:3
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b608_hardcoded_sql_expressions.html
2   li_without_nosec = [
3       "select * from %s where 1 = 1 "
4       % "foo"
5   ]

--------------------------------------------------
>> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
   Severity: Medium   Confidence: Low
   Location: test.py:15
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b608_hardcoded_sql_expressions.html
14  li_nosec_on_top_doesntwork = [
15      "select * from %s where 1 = 1 "
16      % "foo"
17  ]

--------------------------------------------------
>> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
   Severity: Medium   Confidence: Low
   Location: test.py:21
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b608_hardcoded_sql_expressions.html
20  li_nosec_at_end_doesntwork = [
21      "select * from %s where 1 = 1 "
22      % "foo"
23  ]  # nosec

블랙입니다.

여기서는 그것이 개입되지 않고 라인을 재구성하고 주변을 이동하지 않기를 바랍니다.

희망은 여기까지... 줄 길이가 너무 길어질 때마다 파일린트 지시어를 사용하는 것처럼 물건을 이리저리 옮깁니다. 그 시점에서 마지막에 끝난다.

당신은 선제적으로 라인과 첫 번째 라인의 위치를 끊을 수 있습니다. 아니면 블랙아웃을 기다렸다가 필요하면 조정할 수도 있습니다.




단지 주제를 완성하기 위해 - 나의 경우에는 규칙을 없애야 했고, 코드에서 이 문제를 발견할 때마다 쓰기를 원하지 않았고, 항상 깃발로 밴디트를 실행하고 싶지 않았다.

따라서 전체 솔루션에 대해 특정 규칙을 생략하려는 경우 프로젝트의 루트에 파일을 생성할 수 있습니다. 그런 다음 다음과 같이 매번 건너뛸 규칙을 작성할 수 있습니다.

[bandit]
skips: B322

그런 다음 밴디트는 코드에 추가적인 설명을 할 필요 없이 기본적으로 이 검사를 건너뜁니다.




INI 파일을 사용하여 밴디트를 구성할 수 있습니다(옵션과 함께 호출되는 경우에만 해당).

[bandit]
tests = B101,B102,B301

또는 파일 포함:

[tool.bandit]
tests = ["B201", "B301"]
skips = ["B101", "B601"]

또는 yaml 파일로:

skips: ['B101', 'B601']
assert_used:
  skips: ["*/test_*.py", "*/test_*.py"]




코드의 로컬 부분에 규칙을 적용하려는 경우 다음과 같은 작업을 수행할 수 있습니다.

# this is my very basic python script

def foo():
   do_something_unsecure()  # nosec B703, B308

그런 식으로 당신은 그 행에서만 유효성 검사를 건너뛸 것이다. 실제 작업에서는 몇 가지 규칙을 건너뛰는 것이 가장 적절한 방법일 수 있습니다.


반응형