Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: use SafeSet to filter known special protocols #24703

Closed
wants to merge 1 commit into from

Commits on Nov 29, 2018

  1. url: use SafeSet to filter known special protocols

    Avoids a maintenance hazard when reviewers assume that
    `hostlessProtocol` and `slashedProtocol` are disjoint.
    
    The following may be counter-intuitive:
    
    ```js
    // These objects seem to have no keys in common
    const hostlessProtocol = { 'javascript': true };
    const slashedProtocol = { 'http': true };
    // A reasonable reviewer may assumes bothTrue is never truthy
    function bothTrue(lowerProto) {
      return hostlessProtocol[lowerProto] && slashedProtocol[lowerProto];
    }
    // But
    console.log(Boolean(bothTrue('constructor')));  // true
    ```
    
    This change uses SafeSet instead of plain-old objects.
    
    ----
    
    Rejected alternative:
    
    We could have used object with a `null` prototype as lookup tables
    so that `lowerProto` is never treated as a key into `Object.prototype`.
    
    ```js
    const hostlessProtocol = { __proto__: null, 'javascript': true };
    const slashedProtocol = { __proto__: null, 'http': true };
    
    function bothTrue(lowerProto) {
      return hostlessProtocol[lowerProto] && slashedProtocol[lowerProto];
    }
    
    console.log(Boolean(bothTrue('constructor')));  // false
    ```
    mikesamuel committed Nov 29, 2018
    Configuration menu
    Copy the full SHA
    87677a9 View commit details
    Browse the repository at this point in the history