Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check for tile coordinate validity #36

Merged
merged 3 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,23 @@ def retrieve_tile(meta, offset, cache_info):
)


def is_valid_tile_request(z, x, y):
return (0 <= z < 17) and (0 <= x < 2**z) and (0 <= y < 2**z)


@tile_bp.route('/tilezen/vector/v1/<int:tile_pixel_size>/all/<int:z>/<int:x>/<int:y>.<fmt>')
@tile_bp.route('/tilezen/vector/v1/all/<int:z>/<int:x>/<int:y>.<fmt>')
def handle_tile(z, x, y, fmt, tile_pixel_size=None):
requested_tile = TileRequest(z, x, y, fmt)
if not is_valid_tile_request(z, x, y):
return abort(400, "Requested tile out of range.")

tile_pixel_size = tile_pixel_size or 256
tile_size = tile_pixel_size / 256
if tile_size != int(tile_size):
return abort(400, "Invalid tile size. %s is not a multiple of 256." % tile_pixel_size)

requested_tile = TileRequest(z, x, y, fmt)

tile_size = int(tile_size)

meta, offset = meta_and_offset(
Expand Down
22 changes: 22 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ def test_zoom_zero(self):
self.assertTileEquals(TileRequest(1, 0, 1, 'zip'), meta)
self.assertTileEquals(TileRequest(2, 2, 3, 'json'), offset)

def test_valid_tile_request(self):
from server import is_valid_tile_request

# The world
self.assertTrue(is_valid_tile_request(0, 0, 0))
# Home sweet home
self.assertTrue(is_valid_tile_request(15, 15800, 23583))
# Negative!
self.assertFalse(is_valid_tile_request(-1, 15800, 23583))
self.assertFalse(is_valid_tile_request(15, -23, 23583))
self.assertFalse(is_valid_tile_request(15, 15800, -12))
# Too big!
self.assertFalse(is_valid_tile_request(15, 2401239, 23583))
self.assertFalse(is_valid_tile_request(15, 15800, 2341583))
self.assertFalse(is_valid_tile_request(12, 4096, 1844674407))
# In the corners
self.assertTrue(is_valid_tile_request(16, 0, 0))
self.assertTrue(is_valid_tile_request(16, 65535, 65535))
self.assertFalse(is_valid_tile_request(16, 65536, 65536))
# Max zoom is 16
self.assertFalse(is_valid_tile_request(17, 0, 0))

def test_compute_key(self):
from server import compute_key, KeyFormatType

Expand Down