Skip to content

Commit

Permalink
Coverages: error on zero-size / too large requests
Browse files Browse the repository at this point in the history
The OGC API Coverages implementation now produces a "Bad Request"
response if the client requests a coverage with zero size, or one
larger than a predefined limit.
  • Loading branch information
pont-us committed Nov 13, 2023
1 parent e994afe commit 3d5cef7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions xcube/webapi/ows/coverages/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def get_coverage_data(
bbox_crs = pyproj.CRS(query.get('bbox-crs', ['OGC:CRS84'])[0])
subset_crs = query.get('subset-crs', ['OGC:CRS84'])[0]

# TODO: apply a size limit to avoid OOMs from attempting to
# produce arbitrarily large coverages

if 'properties' in query:
requested_vars = set(query['properties'][0].split(','))
data_vars = set(map(str, ds.data_vars))
Expand Down Expand Up @@ -142,8 +139,7 @@ def get_coverage_data(

# ds.rio.write_crs(get_crs_from_dataset(ds), inplace=True)

# TODO: check for empty dataset here, otherwise it will produce
# a confusing "cannot find any grid mapping in dataset" error.
_assert_coverage_size_ok(ds)

source_gm = GridMapping.from_dataset(ds, crs=get_crs_from_dataset(ds))
target_gm = None
Expand Down Expand Up @@ -204,6 +200,23 @@ def get_coverage_data(
)


def _assert_coverage_size_ok(ds):
size_limit = 4000 * 4000
h_dim = _get_h_dim(ds)
v_dim = _get_v_dim(ds)
for d in h_dim, v_dim:
size = ds.dims[d]
if size == 0:
raise ApiError.BadRequest(
f'Requested coverage contains no data: {d} has zero size.'
)
if (h_size := ds.dims[h_dim]) * (y_size := ds.dims[v_dim]) > size_limit:
raise ApiError.BadRequest(
f'Requested coverage is too large:'
f'{h_size} × {y_size} > {size_limit}.'
)


_IndexerTuple = NamedTuple(
'Indexers', [('indices', dict[str, Any]), ('slices', dict[str, slice])]
)
Expand Down

0 comments on commit 3d5cef7

Please sign in to comment.