Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow parent dir path in sanitize_params #1828

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def authorization(permission, target_name: nil, perform_render: true)
end
end

def sanitize_params(param_list, require_params: true, allow_forward_slash: false)
def sanitize_params(param_list, require_params: true, allow_forward_slash: false, allow_parent_dir: false)
if require_params
result = params.require(param_list)
else
Expand All @@ -78,6 +78,9 @@ def sanitize_params(param_list, require_params: true, allow_forward_slash: false
else
value = arg.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
end
if not allow_parent_dir
value = value.gsub(/(\.|%2e){2}/i, "-")
end
if value != arg
render json: { status: 'error', message: "Invalid #{param_list[index]}: #{arg}" }, status: 400
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def authorization(permission, target_name: nil)
return true
end

def sanitize_params(param_list, require_params: true, allow_forward_slash: false)
def sanitize_params(param_list, require_params: true, allow_forward_slash: false, allow_parent_dir: false)
if require_params
result = params.require(param_list)
else
Expand All @@ -89,6 +89,9 @@ def sanitize_params(param_list, require_params: true, allow_forward_slash: false
else
value = arg.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/\t\r\n\\", "-")
end
if not allow_parent_dir
value = value.gsub(/(\.|%2e){2}/i, "-")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to replace with - vs just remove completely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason, just matching the tr() above

end
if value != arg
render json: { status: 'error', message: "Invalid #{param_list[index]}: #{arg}" }, status: 400
return false
Expand Down
Loading