Stricter int parsing

This commit is contained in:
Tom Russell 2018-09-13 20:36:14 +01:00
parent 4939c1dfb0
commit 6c0c1b51c0
4 changed files with 33 additions and 15 deletions

View File

@ -19,7 +19,6 @@
"express-session": "^1.15.6",
"leaflet": "^1.3.4",
"mapnik": "^4.0.1",
"path-to-regexp": "^2.4.0",
"razzle": "2.4.0",
"react": "16.4.2",
"react-dom": "16.4.2",

26
app/src/parse.js Normal file
View File

@ -0,0 +1,26 @@
/**
* Utility functions for parsing
*/
/**
* Parse a string as positive integer or NaN
*
* @param {string} value
*/
function strictParseInt(value) {
if (/^([1-9][0-9]+)$/.test(value))
return Number(value);
return NaN;
}
function parseBuildingURL(url){
const re = /^\/building\/([1-9][0-9]+).html$/;
const matches = re.exec(url);
if (matches && matches.length === 2) {
return parseInt(matches[1])
}
return undefined;
}
export { strictParseInt, parseBuildingURL };

View File

@ -3,7 +3,6 @@ import { StaticRouter } from 'react-router-dom';
import express from 'express';
import { renderToString } from 'react-dom/server';
import serialize from 'serialize-javascript';
import pathToRegexp from 'path-to-regexp';
import bodyParser from 'body-parser';
import session from 'express-session';
@ -14,6 +13,7 @@ import { pool } from './db';
import { authUser, createUser, getUserById } from './user';
import { queryBuildingAtPoint, getBuildingById, saveBuilding } from './building';
import tileserver from './tileserver';
import { parseBuildingURL } from './parse';
// create server
const server = express();
@ -72,15 +72,6 @@ function frontendRoute(req, res) {
})
}
function parseBuildingURL(url){
const re = pathToRegexp('/building/:building.html')
const matches = re.exec(url)
if (matches && matches.length === 2) {
return matches[1]
}
return undefined;
}
function renderHTML(data, req, res){
const context = {};
const markup = renderToString(

View File

@ -2,6 +2,8 @@ import path from 'path';
import mapnik from 'mapnik';
import SphericalMercator from '@mapbox/sphericalmercator';
import { strictParseInt } from './parse';
// config file with connection details
const config = require('../../config.json')
const DATASOURCE_CONFIG = {
@ -29,11 +31,11 @@ const mercator = new SphericalMercator({
function get_bbox(params){
const { z, x, y } = params
const int_z = parseInt(z);
const int_x = parseInt(x);
const int_y = parseInt(y);
const int_z = strictParseInt(z);
const int_x = strictParseInt(x);
const int_y = strictParseInt(y);
if (!int_x || !int_y || !int_z){
if (isNaN(int_x) || isNaN(int_y) || isNaN(int_z)){
console.error("Missing x or y or z")
return {error:'Bad parameter'}
}