Skip to content

Commit d56f211

Browse files
committed
limit input size
1 parent f450572 commit d56f211

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

‎src/tileserver.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class Log {
2121
this.loglevel = level;
2222
}
2323

24-
show(msg: any, level: number) {
25-
if (level <= this.loglevel) console.log(msg);
24+
show(msg: string, level: number) {
25+
if (level <= this.loglevel) console.log(msg.replace(/\n|\r/g, ""));
2626
}
2727
}
2828

@@ -103,6 +103,10 @@ export class Tileserver {
103103
* @return a tile for subsequent use or null if no valid Tile could be extracted.
104104
*/
105105
extractTile(path: string): Tile | null {
106+
if (path.length > 1000) {
107+
this.log.show(`extractTile(): input path length exceeds limit`, LogLevels.ERROR);
108+
return null;
109+
}
106110
const tile: Tile = { x: 0, y: 0, z: 0 };
107111
const tilepath: RegExpMatchArray | null = path.match(/\d+\/\d+\/\d+(?=\.mvt\b)/g);
108112
if (tilepath) {
@@ -121,6 +125,10 @@ export class Tileserver {
121125
* @return the name of the source if found
122126
*/
123127
extractSource(path: string): string | null {
128+
if (path.length > 1000) {
129+
this.log.show(`extractSource(): input path length exceeds limit`, LogLevels.ERROR);
130+
return null;
131+
}
124132
// match the last word between slashes before the actual tile (3-numbers + extension)
125133
const sourceCandidates: RegExpMatchArray | null = path.match(/(?!\/)\w+(?=\/\d+\/\d+\/\d+\.mvt\b)/g)
126134
if (sourceCandidates != null && sourceCandidates.length > 0) {
@@ -352,7 +360,7 @@ export class Tileserver {
352360
const error: Error = _e as Error;
353361
mvt.res = -4;
354362
mvt.status = `[ERROR] - Database error: ${error.message}`;
355-
this.log.show(error, LogLevels.ERROR);
363+
this.log.show(error.message, LogLevels.ERROR);
356364
return mvt;
357365
}
358366
}
@@ -365,7 +373,7 @@ export class Tileserver {
365373
data = Buffer.from("");
366374
}
367375

368-
this.log.show(data, LogLevels.TRACE);
376+
this.log.show(data.toString("base64"), LogLevels.TRACE);
369377

370378
const uncompressedBytes = data.byteLength;
371379
if (this.gzip) mvt.data = await asyncgzip(data) as Buffer;

‎test/parser.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ describe("Parsing functions", function () {
3535
let tile: Tile | null = tileserver.extractTile("foo");
3636
expect(tile).to.be.null;
3737
});
38-
it("extractTile negative #3 - invalid extension", function () {
38+
it("extractTile negative #4 - invalid extension", function () {
3939
let tile: Tile | null = tileserver.extractTile("/local/14/8691/5677.mvtinvalid");
4040
expect(tile).to.be.null;
4141
});
42-
42+
it("extractTile negative #5 - oversized input", function () {
43+
const longString = '9'.repeat(1024);
44+
let tile: Tile | null = tileserver.extractTile(longString);
45+
expect(tile).to.be.null;
46+
});
47+
4348

4449
it("extractSource regular #1 - simple path", function () {
4550
let source: string | null = tileserver.extractSource("/local/0/0/0.mvt");
@@ -58,6 +63,11 @@ describe("Parsing functions", function () {
5863
let source: string | null = tileserver.extractSource("foo");
5964
expect(source).to.be.null;
6065
});
66+
it("extractSource negative #3 - input length limit exceeded", function () {
67+
const longString = '9'.repeat(1024);
68+
let source: string | null = tileserver.extractSource(longString);
69+
expect(source).to.be.null;
70+
});
6171
it("extractSource SQL-Injection #1 - `select now()`", function () {
6272
let source: string | null = tileserver.extractSource("/select+now%28%29/0/0/0.mvt");
6373
expect(source).to.be.equal('29');

0 commit comments

Comments
 (0)