Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:56:45+00:00 2026-06-14T10:56:45+00:00

I wrote a flawed implementation of detecting if two urls are on the same

  • 0

I wrote a flawed implementation of detecting if two urls are on the same domain. I realize this isn’t rocket science, but it seems like there would be a standard lib that has this built-in. My google-fu has failed me. Are there any libraries out there I can require or attribute the cargo-culted code to?

SOP says scheme, port are identical
AND
hosts match, and subdomain(s) is/are a subset of the origin

so

google.com matches google.com
a.google.com matches google.com

Turns out

a.b.google.com matches [b.google.com, google.com] but not [c.b.google.com, a.google.com]

Is only applicable when you can manipulate document.domain which is not the case for me. Direct host matches are required.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:56:46+00:00Added an answer on June 14, 2026 at 10:56 am

    From this Wikipedia article it looks like the scheme, host and port have to be the same to satisfy the same origin policy.

    http://en.wikipedia.org/wiki/Same_origin_policy

    require 'uri'
    
    class SameOrigin
      def self.test(str1, str2)
        uri1 = URI.parse(str1)
        uri2 = URI.parse(str2)
        uri1.scheme == uri2.scheme && uri1.host == uri2.host && uri1.port == uri2.port
      end
    end
    
    SameOrigin.test "http://google.com", "http://google.com"     # => true
    SameOrigin.test "http://google.com:80", "http://google.com"  # => true
    SameOrigin.test "http://google.com", "http://www.google.com" # => false
    SameOrigin.test "https://google.com", "http://google.com"    # => false
    

    If you use the Domainatrix library I found you could change the code to something like this for your test, but it runs a little slow for me. Another option would be to use this RegEx to find the domain of a url. The RegEx is faster but may not work in all cases. I found the RegEx here, btw.

    Remove subdomain from string in ruby

    require 'rubygems'
    require 'domainatrix'
    require 'uri'
    
    class SameOrigin
      def self.relaxed_test(str1, str2)
        d1 = Domainatrix.parse(str1)
        d1 = Domainatrix.parse(str2)
    
        uri1 = URI.parse(str1)
        uri2 = URI.parse(str2)
    
        uri1.scheme == uri2.scheme && 
        d1.domain == d1.domain && 
        d1.public_suffix == d1.public_suffix && 
        uri1.port == uri2.port
      end
    
      def self.relaxed_test2(str1, str2)
        uri1 = URI.parse(str1)
        uri2 = URI.parse(str2)
    
        re = /^(?:(?>[a-z0-9-]*\.)+?|)([a-z0-9-]+\.(?>[a-z]*(?>\.[a-z]{2})?))$/i
        domain1 = uri1.host.gsub(re, '\1').strip
        domain2 = uri2.host.gsub(re, '\1').strip
    
        uri1.scheme == uri2.scheme && domain1 == domain2 && uri1.port == uri2.port
      end
    end
    
    SameOrigin.relaxed_test "http://google.com", "http://google.com"     # => true
    SameOrigin.relaxed_test "http://google.com:80", "http://google.com"  # => true
    SameOrigin.relaxed_test "http://google.com", "http://www.google.com" # => false
    SameOrigin.relaxed_test "https://google.com", "http://google.com"    # => false
    
    SameOrigin.relaxed_test2 "http://google.com", "http://google.com"     # => true
    SameOrigin.relaxed_test2 "http://google.com:80", "http://google.com"  # => true
    SameOrigin.relaxed_test2 "http://google.com", "http://www.google.com" # => false
    SameOrigin.relaxed_test2 "https://google.com", "http://google.com"    # => false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple XML file and a DTD file including an entity, but
I wrote a tweak for cydia , but it does not seem to be
I wrote an application that use MKMapView. This application use a timer to update
I'm sorry in advance if this question is flawed. I'm pretty new to databases(I
I wrote this code to do the IEEE 754 floating point arithmetic on a
I wrote this script to looking for volume with enough free space: @echo on
I wrote a simple opengl application in C which plots sin(x). This is my
I wrote some flawed Huff compression code that I was trying to fix. The
Wrote the following two functions for storing and retrieving any Python (built-in or user-defined)
Are there any good (or at least interesting but flawed) analogs to regular expressions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.