Skip to content

Consider some more narrow _origin_ matching mechanism? #275

@mikewest

Description

@mikewest

What problem are you trying to solve?

Hey folks! I wonder whether you've considered a narrower use case of the URLPattern object which would make it more easily possible for folks to match against a pattern representing an origin as opposed to a URL?

I'm thinking about this in the context of handling MessageEvent objects, where we'd like developers to take a close look at the origin attribute before handling the message. URLPattern can certainly support this use case, as in:

function isReasonableOrigin(origin) {
  const validOrigin = new URLPattern("https://*.trusted.site");
  try {
    const url = new URL(origin);
    return validOrigin.test(url);
  } catch(e) {
    return false; // Or `true`, I suppose, if your application needs to
                  // work with opaque origins.
  }
}

window.addEventListener('message', e => {
  if (isReasonableOrigin(e.origin)) {
    // Exciting activities go here...
  }
});

This works, as long as developers remember to only populate protocol, hostname, and port in a URLPatternInit dictionary, or to ensure that they don't include a trailing slash in the URL pattern string (new URLPattern("https://*.trusted.site/") is fairly restrictive, for instance).

What solutions exist today?

Developers can hold the URLPattern object correctly.

How would you solve it?

Two possibilities come to mind:

  1. Create an origin-matching variant of test() (and exec(), I suppose) which would only take protocol, hostname, and port into account, returning a match iff those three aspects of the pattern matched while ignoring extraneous attributes.

  2. Create an OriginPattern object (and OriginPatternInit) which only accepts the relevant attributes.

The latter seems clearer to me, though I'm not sure how it would be related to the URLPattern object (subclass? parent class? completely separate thing? It would be nice if it could be used wherever URLPattern was used, so subclassing makes the most sense, but that means it would have a bunch of properties that are somewhat meaningless. Meh.)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalNew features or enhancementsneeds implementer interestMoving the issue forward requires implementers to express interest

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions