This rule helps prevent Server-Side Request Forgery (SSRF) attacks. SSRF attacks manipulate the server to make HTTP requests to an arbitrary domain of the attacker’s choosing. This can lead to unauthorized actions or access to data within the server, potentially exposing sensitive information.
This rule is important to enforce because SSRF attacks can cause significant damage, allowing attackers to bypass firewall protections, perform actions on behalf of the server, or gain unauthorized access to data. The impact of such an attack can be severe, leading to data breaches or server control takeover.
Good coding practices to avoid SSRF attacks include validating and sanitizing user inputs and limiting the server’s ability to initiate outbound requests.
@app.route("/cmd",methods=['POST'])defcmd():filename=request.form['filename']try:if"http"notinstr(urlparse(filename).scheme):host=request.url[:-4]filename=host+"/static/"+filenameresult=eval(requests.get(filename).text)returnrender_template("index.html",result=result)else:result=eval(requests.get(filename).text)returnrender_template("index.html",result=result)exceptException:returnrender_template("index.html",result="Unexpected error during the execution of the predefined command.")
Compliant Code Examples
importrequestsfromflaskimportrequest,render_templatefromurllib.parseimporturlparse@app.route("/cmd",methods=['POST'])defcmd():filename=request.form.get('filename')try:ifnotfilenameorlen(filename)>255:raiseValueError("Invalid filename")ifany(cinfilenameforcin[';','&','|','$','<','>','`']):raiseValueError("Invalid filename")parsed_url=urlparse(filename)ifparsed_url.schemenotin["http","https"]:host=request.url_root.rstrip('/')filename=f"{host}/static/{filename}"response=requests.get(filename)response.raise_for_status()result=response.textreturnrender_template("index.html",result=result)exceptExceptionase:returnrender_template("index.html",result="Unexpected error during the execution of the predefined command.")
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- python-flask # Rules to enforce Python flask.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다