Stop treating "std::map" or "Error: foo" as URIs.

Forbid unusual URIs in :open, etc. Fix #352.

```
If the uri looks like it might contain a schema and a domain, try url()
test for a non-whitespace, non-colon character after the colon to avoid
false positives like "error: can't reticulate spline" and "std::map".

These heuristics mean that very unusual URIs will be coerced to
something else by this function.
if (/^[a-zA-Z0-9+.-]+:[^\s:]/.test(maybeURI)) {
```
This commit is contained in:
Colin Caine 2018-04-19 17:35:05 +01:00
parent 07b0a20923
commit 4c7efaaef5

View file

@ -137,15 +137,24 @@ function searchURL(provider: string, query: string) {
return UrlUtil.interpolateSearchItem(new URL(searchurlprovider), query) return UrlUtil.interpolateSearchItem(new URL(searchurlprovider), query)
} }
/** If maybeURI doesn't have a schema, affix http:// */ /** Take a string and find a way to interpret it as a URI or search query. */
/** @hidden */ /** @hidden */
export function forceURI(maybeURI: string): string { export function forceURI(maybeURI: string): string {
// Need undefined to be able to open about:newtab // Need undefined to be able to open about:newtab
if (maybeURI == "") return undefined if (maybeURI == "") return undefined
try {
return new URL(maybeURI).href // If the uri looks like it might contain a schema and a domain, try url()
} catch (e) { // test for a non-whitespace, non-colon character after the colon to avoid
if (e.name !== "TypeError") throw e // false positives like "error: can't reticulate spline" and "std::map".
//
// These heuristics mean that very unusual URIs will be coerced to
// something else by this function.
if (/^[a-zA-Z0-9+.-]+:[^\s:]/.test(maybeURI)) {
try {
return new URL(maybeURI).href
} catch (e) {
if (e.name !== "TypeError") throw e
}
} }
// Else if search keyword: // Else if search keyword: