Module: SafeUrl

Extended by:
ActiveSupport::Concern
Included in:
RemoteMirror
Defined in:
app/models/concerns/safe_url.rb

Instance Method Summary collapse

Instance Method Details

#safe_url(allowed_usernames: []) ⇒ Object

Return the URL with obfuscated userinfo and keeping it intact



8
9
10
11
12
13
14
15
16
17
# File 'app/models/concerns/safe_url.rb', line 8

def safe_url(allowed_usernames: [])
  return if url.nil?

  escaped = Addressable::URI.escape(url)
  uri = URI.parse(escaped)
  uri.password = '*****' if uri.password
  uri.user = '*****' if uri.user && allowed_usernames.exclude?(uri.user)
  Addressable::URI.unescape(uri.to_s)
rescue URI::Error, TypeError
end