Skip to content

Commit

Permalink
issue/910: Fix CORS warning for localhost
Browse files Browse the repository at this point in the history
See maplibre#910

Don't show CORS warning for localhost
  • Loading branch information
oobayly committed Sep 23, 2024
1 parent 0f1000c commit aa6e055
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/components/InputUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
}

let error;
const getProtocol = (url: string) => {
const getUrlParams = (url: string) => {
let protocol: string | undefined;
let isLocal = false;

try {
const urlObj = new URL(url);
return urlObj.protocol;
}
catch (err) {
return undefined;

protocol = urlObj.protocol;
// Basic check against localhost; 127.0.0.1/8 and IPv6 localhost [::1]
isLocal = /^(localhost|\[::1\]|127(.[0-9]{1,3}){3})/i.test(urlObj.hostname);
} catch (err) {
}

return { protocol, isLocal };
};
const protocol = getProtocol(url);
const {protocol, isLocal} = getUrlParams(url);
const isSsl = window.location.protocol === "https:";

if (!protocol) {
Expand All @@ -40,7 +46,8 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
else if (
protocol &&
protocol === "http:" &&
window.location.protocol === "https:"
window.location.protocol === "https:" &&
!isLocal
) {
error = (
<SmallError>
Expand Down Expand Up @@ -76,10 +83,10 @@ type FieldUrlState = {

class FieldUrlInternal extends React.Component<FieldUrlInternalProps, FieldUrlState> {
static defaultProps = {
onInput: () => {},
onInput: () => { },
}

constructor (props: FieldUrlInternalProps) {
constructor(props: FieldUrlInternalProps) {
super(props);
this.state = {
error: validate(props.value, props.t),
Expand All @@ -100,7 +107,7 @@ class FieldUrlInternal extends React.Component<FieldUrlInternalProps, FieldUrlSt
this.props.onChange(url);
}

render () {
render() {
return (
<div>
<InputString
Expand Down

0 comments on commit aa6e055

Please sign in to comment.