Skip to content

Commit 4a75ecb

Browse files
committed
Fix ReDoS vulnerability in name parsing
Thanks to @ooooooo_q for the patch! [CVE-2023-22799]
1 parent 42f5ea6 commit 4a75ecb

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

lib/global_id/uri/gid.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ def set_params(params)
123123
private
124124
COMPONENT = [ :scheme, :app, :model_name, :model_id, :params ].freeze
125125

126-
# Extracts model_name and model_id from the URI path.
127-
PATH_REGEXP = %r(\A/([^/]+)/?([^/]+)?\z)
128-
129126
def check_host(host)
130127
validate_component(host)
131128
super
@@ -145,11 +142,11 @@ def check_scheme(scheme)
145142
end
146143

147144
def set_model_components(path, validate = false)
148-
_, model_name, model_id = path.match(PATH_REGEXP).to_a
149-
model_id = CGI.unescape(model_id) if model_id
150-
145+
_, model_name, model_id = path.split('/', 3)
151146
validate_component(model_name) && validate_model_id(model_id, model_name) if validate
152147

148+
model_id = CGI.unescape(model_id) if model_id
149+
153150
@model_name = model_name
154151
@model_id = model_id
155152
end
@@ -162,7 +159,7 @@ def validate_component(component)
162159
end
163160

164161
def validate_model_id(model_id, model_name)
165-
return model_id unless model_id.blank?
162+
return model_id unless model_id.blank? || model_id.include?('/')
166163

167164
raise MissingModelIdError, "Unable to create a Global ID for " \
168165
"#{model_name} without a model id."

0 commit comments

Comments
 (0)