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

issue/910: Fix CORS warning for localhost #939

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 14 additions & 10 deletions src/components/InputUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ function validate(url: string, t: TFunction): JSX.Element | undefined {
}

let error;
const getProtocol = (url: string) => {
const getUrlParams = (url: string) => {
try {
const urlObj = new URL(url);
return urlObj.protocol;
}
catch (err) {
return undefined;
const { protocol, hostname } = urlObj;
// Basic check against localhost; 127.0.0.1/8 and IPv6 localhost [::1]
const isLocal = /^(localhost|\[::1\]|127(.[0-9]{1,3}){3})/i.test(hostname);

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

if (!protocol) {
Expand All @@ -40,7 +43,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 +80,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 +104,7 @@ class FieldUrlInternal extends React.Component<FieldUrlInternalProps, FieldUrlSt
this.props.onChange(url);
}

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