mirror of
https://github.com/uetchy/namae.git
synced 2025-03-17 12:30:32 +09:00
fix: sanitize input
This commit is contained in:
parent
01293b6628
commit
7752eb6f5a
@ -2,6 +2,7 @@ import React from 'react';
|
|||||||
import styled, {createGlobalStyle} from 'styled-components';
|
import styled, {createGlobalStyle} from 'styled-components';
|
||||||
import {Helmet} from 'react-helmet';
|
import {Helmet} from 'react-helmet';
|
||||||
import {useTranslation} from 'react-i18next';
|
import {useTranslation} from 'react-i18next';
|
||||||
|
import {Switch, Route, useParams, Redirect} from 'react-router-dom';
|
||||||
|
|
||||||
import Welcome from './components/Welcome';
|
import Welcome from './components/Welcome';
|
||||||
import Form from './components/Form';
|
import Form from './components/Form';
|
||||||
@ -10,18 +11,21 @@ import Footer from './components/Footer';
|
|||||||
|
|
||||||
import {mobile} from './util/css';
|
import {mobile} from './util/css';
|
||||||
import {isStandalone} from './util/pwa';
|
import {isStandalone} from './util/pwa';
|
||||||
import {Switch, Route, useParams} from 'react-router-dom';
|
import {sanitize} from './util/text';
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<GlobalStyle />
|
<GlobalStyle />
|
||||||
<Switch>
|
<Switch>
|
||||||
|
<Route exact path="/">
|
||||||
|
<Home />
|
||||||
|
</Route>
|
||||||
<Route path="/s/:query">
|
<Route path="/s/:query">
|
||||||
<Search />
|
<Search />
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="/">
|
<Route path="*">
|
||||||
<Home />
|
<Redirect to="/" />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
<Footer />
|
<Footer />
|
||||||
@ -30,7 +34,8 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Search() {
|
function Search() {
|
||||||
const {query: currentQuery} = useParams<{query: string}>();
|
const params = useParams<{query: string}>();
|
||||||
|
const currentQuery = sanitize(params.query);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -2,7 +2,7 @@ import React, {useState, useRef, useEffect} from 'react';
|
|||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import {useTranslation} from 'react-i18next';
|
import {useTranslation} from 'react-i18next';
|
||||||
import {Link, useHistory} from 'react-router-dom';
|
import {Link, useHistory} from 'react-router-dom';
|
||||||
|
import {sanitize} from '../util/text';
|
||||||
import {sendQueryStatistics} from '../util/analytics';
|
import {sendQueryStatistics} from '../util/analytics';
|
||||||
import {useDeferredState} from '../util/hooks';
|
import {useDeferredState} from '../util/hooks';
|
||||||
import {mobile} from '../util/css';
|
import {mobile} from '../util/css';
|
||||||
@ -56,7 +56,7 @@ const Form: React.FC<{
|
|||||||
}, [query, history]);
|
}, [query, history]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const modifiedValue = inputValue.replace(/[\s@+!#$%^&*()[\]]/g, '');
|
const modifiedValue = sanitize(inputValue);
|
||||||
setQuery(modifiedValue);
|
setQuery(modifiedValue);
|
||||||
}, [inputValue, setQuery]);
|
}, [inputValue, setQuery]);
|
||||||
|
|
||||||
|
@ -2,3 +2,7 @@ export function capitalize(text: string): string {
|
|||||||
if (text.length === 0) return '';
|
if (text.length === 0) return '';
|
||||||
return text[0].toUpperCase() + text.slice(1).toLowerCase();
|
return text[0].toUpperCase() + text.slice(1).toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sanitize(text: string): string {
|
||||||
|
return text.replace(/[\s@+!#$%^&*()[\]./<>{}]/g, '');
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user