Skip to content

Commit 48fe092

Browse files
committed
Add guards against invalid url to initial state loading
1 parent d85204a commit 48fe092

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

mithril-explorer/components/AggregatorSetter/AddAggregatorModal.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useState} from 'react';
22
import {Button, Form, FormGroup, Modal} from "react-bootstrap";
33
import {useDispatch} from "react-redux";
44
import {selectAggregator} from "../../store/settingsSlice";
5+
import {checkUrl} from "../../utils";
56

67
export default function AddAggregatorModal(props) {
78
const [value, setValue] = useState("");
@@ -18,13 +19,10 @@ export default function AddAggregatorModal(props) {
1819
// Avoid form submit if the enter key is pressed
1920
event.preventDefault();
2021

21-
try {
22-
// Use the url constructor to check if the value is an url
23-
new URL(value);
22+
if (checkUrl(value)) {
2423
handleClose();
2524
dispatch(selectAggregator(value));
26-
} catch (ex) {
27-
console.warn("invalid url", ex);
25+
} else {
2826
setIsInvalid(true);
2927
}
3028
}

mithril-explorer/store/settingsSlice.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {createSlice} from "@reduxjs/toolkit";
22
import default_available_aggregators from "../aggregators-list";
3+
import {checkUrl} from "../utils";
34

45
export const initialState = {
56
autoUpdate: true,
@@ -20,6 +21,10 @@ export const settingsSlice = createSlice({
2021
state.autoUpdate = !state.autoUpdate;
2122
},
2223
selectAggregator: (state, action) => {
24+
if (!checkUrl(action.payload)) {
25+
return state;
26+
}
27+
2328
const availableAggregators =
2429
state.availableAggregators.includes(action.payload)
2530
? state.availableAggregators

mithril-explorer/store/store.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {configureStore} from "@reduxjs/toolkit";
22
import {createWrapper} from "next-redux-wrapper";
33
import {initialState as settingsInitialState, settingsSlice} from "./settingsSlice";
44
import default_available_aggregators from "../aggregators-list";
5+
import {checkUrl} from "../utils";
56

67
const SAVED_STATE_KEY = "Explorer_State";
78

@@ -24,10 +25,9 @@ function initStore() {
2425

2526
if (location?.search) {
2627
const params = new URLSearchParams(location.search);
28+
const aggregator = params.get('aggregator');
2729

28-
if (params.has('aggregator')) {
29-
const aggregator = params.get('aggregator');
30-
30+
if (aggregator && checkUrl(aggregator)) {
3131
const settings = {
3232
selectedAggregator: aggregator,
3333
availableAggregators:

mithril-explorer/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function checkUrl(url) {
2+
try {
3+
// Use the url constructor to check if the value is an url
4+
return Boolean(new URL(url));
5+
} catch (ex) {
6+
return false;
7+
}
8+
}
9+
10+
module.exports = {
11+
checkUrl
12+
}

0 commit comments

Comments
 (0)