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

Allow wildcard url in domain list #38

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ typings/
# next.js build output
.next
dist

# jetbrain IDE files
/.idea
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,10 @@ To configure domains, add a `<meta name="import-map-overrides-domains">` element

<!-- Disable the entirety of import-map-overrides when you're on the production environment -->
<meta name="import-map-overrides-domains" content="denylist:prod.example.com" />

<!-- You can also use wildcards url -->
<meta
name="import-map-overrides-domains"
content="allowlist:*.example.com,example-*.com"
/>
```
10 changes: 10 additions & 0 deletions src/api/js-api-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// from https://github.com/sindresorhus/escape-string-regexp
export const escapeStringRegexp = (string) => {
if (typeof string !== "string") {
throw new TypeError("Expected a string");
}

// Escape characters with special meaning either inside or outside character sets.
// Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
};
14 changes: 8 additions & 6 deletions src/api/js-api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { escapeStringRegexp } from "./js-api-util";

const localStoragePrefix = "import-map-override:";
const disabledOverridesLocalStorageKey = "import-map-overrides-disabled";
const externalOverridesLocalStorageKey = "import-map-overrides-external-maps";
Expand Down Expand Up @@ -27,16 +29,16 @@ if (domainsElement) {
if (!content) {
console.warn(`Invalid ${domainsMeta} meta element - content required.`);
}

const matchHostname = (domain) =>
new RegExp(escapeStringRegexp(domain).replace("*", ".+")).test(domain);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you confirmed that this works for a variety of allowlists and domains?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit complicated locally but at least this is the result of manual tests on localhost:5000:

Active When:

  • content="allowlist:localhost"
  • content="denylist:localhost-*,localhost.*,*.localhost,*-localhost,*localhost,local*host,localhost*"

Inactive When:

  • content="denylist:localhost"
  • content="allowlist:localhost-*,localhost.*,*.localhost,*-localhost,*localhost,local*host,localhost*"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 seems reasonable. Manipulation of /etc/hosts file could allow for further testing, but it seems like we've covered most of the cases with those tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idd I tried playing with hosts but I don't know why, I couldn't access to the test server with it. I thought about a host parameter to provide to the "serve" ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code broke the allowlist and denylist, as reported in https://single-spa.slack.com/archives/C8R6U7MT7/p1605204526245000.

The reason is that window.location.hostname is no longer the domain being tested against.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, wildcard domains aren't working because the * character is escaped before being replaced with .+


if (content.indexOf(allowListPrefix) === 0) {
const allowedDomains = content.slice(allowListPrefix.length).split(",");
isDisabled = !allowedDomains.some(
(allowedDomain) => window.location.hostname === allowedDomain
);
isDisabled = !allowedDomains.some(matchHostname);
} else if (content.indexOf(denyListPrefix) === 0) {
const deniedDomains = content.slice(denyListPrefix.length).split(",");
isDisabled = deniedDomains.some(
(deniedDomain) => deniedDomain === window.location.hostname
);
isDisabled = deniedDomains.some(matchHostname);
} else {
console.log(
`Invalid ${domainsMeta} meta content attribute - must start with ${allowListPrefix} or ${denyListPrefix}`
Expand Down
22 changes: 22 additions & 0 deletions test/deny-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Import Map Overrides test</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="importmap-type" content="systemjs-importmap" />
<meta name="import-map-overrides-domains" content="denylist:localhost" />
<script src="/import-map-overrides.js"></script>
<script src="https://cdn.jsdelivr.net/npm/systemjs/dist/system.js"></script>
</head>
<body>
<import-map-overrides-full dev-libs></import-map-overrides-full>
<ul>
<li><a href="index.html">With embedded map</a></li>
<li><a href="no-map.html">Without embedded map</a></li>
<li><a href="server-map.html">Server map</a></li>
<li>With deny list (current)</li>
</ul>
</body>
</html>
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<li>With embedded map (current)</li>
<li><a href="no-map.html">Without embedded map</a></li>
<li><a href="server-map.html">Server map</a></li>
<li><a href="deny-list.html">With deny list</a></li>
</ul>
</body>
</html>
2 changes: 2 additions & 0 deletions test/no-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<ul>
<li><a href="index.html">With embedded map</a></li>
<li>Without embedded map (current)</li>
<li><a href="server-map.html">Server map</a></li>
<li><a href="deny-list.html">With deny list</a></li>
</ul>
</body>
</html>
1 change: 1 addition & 0 deletions test/server-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<li><a href="index.html">With embedded map</a></li>
<li><a href="no-map.html">Without embedded map</a></li>
<li>Server map (current)</li>
<li><a href="deny-list.html">With deny list</a></li>
</ul>
</body>
</html>