Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Improve query string arguments validation

Marcin Gębalacafb610b2b4a
diff --git a/saleor/static/js/components/categoryPage/CategoryPage.js b/saleor/static/js/components/categoryPage/CategoryPage.js
index 45bf35b8be..ef4cb01969 100644
--- a/saleor/static/js/components/categoryPage/CategoryPage.js
+++ b/saleor/static/js/components/categoryPage/CategoryPage.js
@@ -7,9 +7,10 @@ import PriceFilter from './PriceFilter';
import ProductFilters from './ProductFilters';
import ProductList from './ProductList';
import SortBy from './SortBy';
-import { floatOrNull, getFromQuery, getAttributesFromQuery } from './utils';
+import { ensureAllowedName, getAttributesFromQuery, getFromQuery } from './utils';
const PAGINATE_BY = 20;
+const SORT_BY_FIELDS = ['name', 'price'];
class CategoryPage extends Component {
@@ -48,8 +49,8 @@ class CategoryPage extends Component {
updatePriceFilter = (minPrice, maxPrice) => {
this.props.relay.setVariables({
- minPrice: floatOrNull(minPrice),
- maxPrice: floatOrNull(maxPrice)
+ minPrice: parseInt(minPrice) || null,
+ maxPrice: parseInt(maxPrice) || null
});
}
@@ -147,10 +148,10 @@ class CategoryPage extends Component {
export default Relay.createContainer(CategoryPage, {
initialVariables: {
attributesFilter: getAttributesFromQuery(['count', 'minPrice', 'maxPrice', 'sortBy']),
- count: floatOrNull(getFromQuery('count', PAGINATE_BY)),
- minPrice: floatOrNull(getFromQuery('minPrice')),
- maxPrice: floatOrNull(getFromQuery('maxPrice')),
- sortBy: getFromQuery('sortBy', 'name')
+ count: parseInt(getFromQuery('count', PAGINATE_BY)) || PAGINATE_BY,
+ minPrice: parseInt(getFromQuery('minPrice')) || null,
+ maxPrice: parseInt(getFromQuery('maxPrice')) || null,
+ sortBy: ensureAllowedName(getFromQuery('sortBy', 'name'), SORT_BY_FIELDS)
},
fragments: {
category: () => Relay.QL`
diff --git a/saleor/static/js/components/categoryPage/utils.js b/saleor/static/js/components/categoryPage/utils.js
index 3e9bde96f9..9055bc00c1 100644
--- a/saleor/static/js/components/categoryPage/utils.js
+++ b/saleor/static/js/components/categoryPage/utils.js
@@ -26,7 +26,10 @@ export const getAttributesFromQuery = (exclude) => {
return attributes;
};
-export const floatOrNull = (value) => {
- const parsed = parseFloat(value);
- return isNaN(parsed) ? null : parsed;
+export const ensureAllowedName = (name, allowed) => {
+ let origName = name;
+ if (name.startsWith('-')) {
+ name = name.substr(1, name.length);
+ }
+ return name in allowed ? origName : null;
};