Sha256: b5060d6b9bcc8caa8f80aa246757876937b47c3244e1f821721f6643954467c5

Contents?: true

Size: 654 Bytes

Versions: 2

Compression:

Stored size: 654 Bytes

Contents

require 'uri'

module GoogleSignIn
  module RedirectProtector
    extend self

    class Violation < StandardError; end

    QUALIFIED_URL_PATTERN = /\A#{URI::DEFAULT_PARSER.make_regexp}\z/

    def ensure_same_origin(target, source)
      if target.blank? || (target =~ QUALIFIED_URL_PATTERN && origin_of(target) != origin_of(source))
        raise Violation, "Redirect target #{target.inspect} does not have same origin as request (expected #{origin_of(source)})"
      end
    end

    private
      def origin_of(url)
        uri = URI(url)
        "#{uri.scheme}://#{uri.host}:#{uri.port}"
      rescue ArgumentError
        nil
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
google_sign_in-1.2.1 lib/google_sign_in/redirect_protector.rb
google_sign_in-1.2.0 lib/google_sign_in/redirect_protector.rb