This rule aims to prevent path traversal vulnerabilities in Ruby on Rails applications. Path traversal occurs when user input is used to construct file paths without proper validation, allowing attackers to access sensitive files outside the intended directories. This can lead to unauthorized disclosure of system files, application source code, or configuration data.
It is crucial to avoid directly incorporating user-controlled parameters into file paths, especially when using methods like render file: or send_file. Attackers can manipulate these inputs to traverse directories using sequences like ../, potentially exposing critical files. Such vulnerabilities can compromise the security and integrity of your application and underlying system.
To mitigate this risk, developers should avoid interpolating user input into file paths. Instead, use safer alternatives such as render template: with fixed or validated template names. If dynamic file access is necessary, ensure strict validation and sanitization of input parameters, or constrain access to a predefined whitelist of acceptable files. Following these practices helps maintain the application’s security posture against path traversal attacks.
defmyfunction# e.g. params[:page] = "../../etc/passwd"renderfile:Rails.root.join("app/views/pages","#{params[:page]}.html.erb")end
# e.g. params[:page] = "../../etc/passwd"renderfile:Rails.root.join("app/views/pages","#{params[:page]}.html.erb")
Compliant Code Examples
moduleMyModuleclassUser<BaseControllerdefcreate# fetch from database, we assume the data is safeviewer=func(User.find(params[:user_id]))renderjson:{viewer_added:viewer.present?}endendend
rendertemplate:"pages/otherpage"
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- ruby-security # Rules to enforce Ruby security.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다