Skip to content

Commit

Permalink
all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
acalcutt committed Jan 3, 2025
1 parent 8bdd14a commit 3260611
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
76 changes: 46 additions & 30 deletions src/serve_rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ const respondImage = async (
overlay = null,
mode = 'tile',
) => {
console.log(lat);
console.log(lon);
if (
Math.abs(lon) > 180 ||
Math.abs(lat) > 85.06 ||
Expand Down Expand Up @@ -724,6 +726,7 @@ async function handleTileRequest(
* @param {string} req.params.format - The format of the image.
* @param {Function} next - Express next middleware function.
* @param {number} maxScaleFactor - The maximum scale factor allowed.
* @param verbose
* @returns {Promise<void>}
*/
async function handleStaticRequest(
Expand All @@ -733,6 +736,7 @@ async function handleStaticRequest(
res,
next,
maxScaleFactor,
verbose,
) {
const {
id,
Expand All @@ -742,6 +746,13 @@ async function handleStaticRequest(
scale: scaleParam,
format,
} = req.params;
if (verbose) {
console.log(
`Handling static request for: /styles/${id}/static/${raw ? raw + '/' : ''}${staticType}${widthAndHeight ? '/' + widthAndHeight : ''}${
scaleParam ? '@' + scaleParam : ''
}.${format}`,

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
Log entry depends on a
user-provided value
.
Log entry depends on a
user-provided value
.
Log entry depends on a
user-provided value
.
Log entry depends on a
user-provided value
.
Log entry depends on a
user-provided value
.
);
}
console.log(req.params);
const item = repo[id];

Expand All @@ -752,7 +763,6 @@ async function handleStaticRequest(
if (sizeMatch) {
const width = parseInt(sizeMatch[1], 10);
const height = parseInt(sizeMatch[2], 10);

if (
isNaN(width) ||
isNaN(height) ||
Expand All @@ -775,22 +785,22 @@ async function handleStaticRequest(
.status(400)
.send('Invalid width or height provided in size parameter');
}

const scale = parseScale(scaleParam, maxScaleFactor);
let isRaw = raw === 'raw';

if (!item || !staticType || !format || !scale) {
const staticTypeMatch = staticType.match(staticTypeRegex);
if (!item || !format || !scale || !staticTypeMatch?.groups) {
return res.sendStatus(404);
}

const staticTypeMatch = staticType.match(staticTypeRegex);
if (staticTypeMatch.groups.lon) {
// Center Based Static Image
const z = staticTypeMatch.groups.zoom;
let x = staticTypeMatch.groups.lon;
let y = staticTypeMatch.groups.lat;
const bearing = staticTypeMatch.groups.bearing;
const pitch = staticTypeMatch.groups.pitch;

const z = parseFloat(staticTypeMatch.groups.zoom) || 0;
let x = parseFloat(staticTypeMatch.groups.lon) || 0;
let y = parseFloat(staticTypeMatch.groups.lat) || 0;
const bearing = parseFloat(staticTypeMatch.groups.bearing) || 0;
const pitch = parseInt(staticTypeMatch.groups.pitch) || 0;
if (z < 0) {
return res.status(404).send('Invalid zoom');
}
Expand All @@ -807,24 +817,27 @@ async function handleStaticRequest(

const paths = extractPathsFromQuery(req.query, transformer);
const markers = extractMarkersFromQuery(req.query, options, transformer);

// prettier-ignore
const overlay = await renderOverlay(
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
);
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
);

// prettier-ignore
return await respondImage(
options, item, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, format, res, overlay, 'static',
);
options, item, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, format, res, overlay, 'static',
);
} else if (staticTypeMatch.groups.minx) {
// Area Based Static Image
const bbox = [
+staticTypeMatch.groups.minx,
+staticTypeMatch.groups.miny,
+staticTypeMatch.groups.maxx,
+staticTypeMatch.groups.maxx,
];
const minx = parseFloat(staticTypeMatch.groups.minx) || 0;
const miny = parseFloat(staticTypeMatch.groups.miny) || 0;
const maxx = parseFloat(staticTypeMatch.groups.maxx) || 0;
const maxy = parseFloat(staticTypeMatch.groups.maxy) || 0;
if (isNaN(minx) || isNaN(miny) || isNaN(maxx) || isNaN(maxy)) {
return res
.status(400)
.send('Invalid bounding box provided in staticType parameter');
}
const bbox = [minx, miny, maxx, maxy];
let center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];

const transformer = isRaw
Expand All @@ -841,23 +854,27 @@ async function handleStaticRequest(
center = transformer(center);
}

if (Math.abs(center[0]) > 180 || Math.abs(center[1]) > 85.06) {
return res.status(400).send('Invalid center');
}

const z = calcZForBBox(bbox, parsedWidth, parsedHeight, req.query);
const x = center[0];
const y = center[1];
const bearing = 0;
const pitch = 0;

const paths = extractPathsFromQuery(req.query, transformer);
const markers = extractMarkersFromQuery(req.query, options, transformer);

// prettier-ignore
const overlay = await renderOverlay(
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
);

// prettier-ignore
return await respondImage(
options, item, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, format, res, overlay, 'static',
);
options, item, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, format, res, overlay, 'static',
);
} else if (staticTypeMatch.groups.auto) {
// Area Static Image
const bearing = 0;
Expand Down Expand Up @@ -910,18 +927,17 @@ async function handleStaticRequest(

// prettier-ignore
const overlay = await renderOverlay(
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
);
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
);

// prettier-ignore
return await respondImage(
options, item, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, format, res, overlay, 'static',
);
options, item, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, format, res, overlay, 'static',
);
} else {
return res.sendStatus(404);
}
}

const existingFonts = {};
let maxScaleFactor = 2;

Expand Down Expand Up @@ -980,7 +996,7 @@ export const serve_rendered = {
res,
next,
maxScaleFactor,
defailtTileSize,
verbose,
);
}
return res.sendStatus(404);
Expand Down
2 changes: 1 addition & 1 deletion test/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Static endpoints', function () {
testStatic(prefix, '0,0,0/256x256', 'png', 404, 1);

testStatic(prefix, '0,0,-1/256x256', 'png', 404);
testStatic(prefix, '0,0,0/256.5x256.5', 'png', 404);
testStatic(prefix, '0,0,0/256.5x256.5', 'png', 400);

testStatic(prefix, '0,0,0,/256x256', 'png', 404);
testStatic(prefix, '0,0,0,0,/256x256', 'png', 404);
Expand Down

0 comments on commit 3260611

Please sign in to comment.