@@ -154,6 +154,33 @@ function JSON3.write(ctx::FieldContext{FORGE, T}, buf, pos, len, x::T; kw...) wh
154154 return buf, pos, len
155155end
156156
157+ function assert_check_endpoint_url (url:: AbstractString )
158+ # Do not allow path navigation in URLs
159+ # Disallowed pattern: ..
160+ if occursin (r" \.\. " , url)
161+ throw (ArgumentError (" URLs cannot contain path navigation" ))
162+ end
163+
164+ # Additional disallowed patterns:
165+ # ../, ..\, /.., \.., ./, .\, /./, \.\
166+ PATH_TRAVERSAL = r" (?:\. {2,}[\/\\ ]|\. {1,}[\/\\ ]|[\/\\ ]\. {2,}|[\/\\ ]\. {1,}[\/\\ ])"
167+ if occursin (PATH_TRAVERSAL, url)
168+ throw (ArgumentError (" URLs cannot contain path navigation" ))
169+ end
170+
171+ # do not allow new lines or carriage returns in URLs
172+ if occursin (r" \s " , url)
173+ throw (ArgumentError (" URLs cannot contain line breaks" ))
174+ end
175+
176+ # Roundtrip the URL through `URIs.resolvereference()`, and make sure it is unchanged
177+ if url != URIs. resolvereference (" https://example.invalid/" , url). path
178+ throw (ArgumentError (" URLs cannot contain path navigation" ))
179+ end
180+
181+ return nothing
182+ end
183+
157184"""
158185 Endpoint(
159186 method::Symbol,
@@ -187,23 +214,7 @@ struct Endpoint
187214 query:: Dict = Dict (),
188215 allow_404:: Bool = false ,
189216 )
190- # Do not allow path navigation in URLs
191- # Disallowed pattern: ..
192- if occursin (r" \.\. " , url)
193- throw (ArgumentError (" URLs cannot contain path navigation" ))
194- end
195-
196- # Additional disallowed patterns:
197- # ../, ..\, /.., \.., ./, .\, /./, \.\
198- PATH_TRAVERSAL = r" (?:\. {2,}[\/\\ ]|\. {1,}[\/\\ ]|[\/\\ ]\. {2,}|[\/\\ ]\. {1,}[\/\\ ])"
199- if occursin (PATH_TRAVERSAL, url)
200- throw (ArgumentError (" URLs cannot contain path navigation" ))
201- end
202-
203- # do not allow new lines or carriage returns in URLs
204- if occursin (r" \s " , url)
205- throw (ArgumentError (" URLs cannot contain line breaks" ))
206- end
217+ assert_check_endpoint_url (url)
207218 return new (method, url, headers, query, allow_404)
208219 end
209220end
0 commit comments