Skip to content

Commit

Permalink
YDA-5725: validate API names
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Jun 5, 2024
1 parent fad9cc3 commit 13b319c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python3

__copyright__ = 'Copyright (c) 2021-2023, Utrecht University'
__copyright__ = 'Copyright (c) 2021-2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'

import base64
import json
import re
import sys
import zlib
from timeit import default_timer as timer
Expand All @@ -13,7 +14,7 @@
from flask import Blueprint, current_app as app, g, jsonify, request, Response
from irods import message, rule

from errors import UnauthorizedAPIAccessError
from errors import InvalidAPIError, UnauthorizedAPIAccessError
from util import log_error

api_bp = Blueprint('api_bp', __name__)
Expand All @@ -24,6 +25,9 @@ def _call(fn: str) -> Response:
if not authenticated():
raise UnauthorizedAPIAccessError

if not re.match("^([a-z_]+)$", fn):
raise InvalidAPIError

data: Dict[str, Any] = {}
if 'data' in request.form:
data = json.loads(request.form['data'])
Expand Down Expand Up @@ -110,6 +114,10 @@ def api_error_handler(error: Exception) -> Response:
data: Dict[str, Any] = {}
code = 500

if type(error) == InvalidAPIError:
code = 400
status_info = "Bad API request"

if type(error) == UnauthorizedAPIAccessError:
code = 401
status_info = "Not authorized to use the API"
Expand Down
6 changes: 5 additions & 1 deletion errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

__copyright__ = 'Copyright (c) 2021, Utrecht University'
__copyright__ = 'Copyright (c) 2021-2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'


Expand All @@ -12,5 +12,9 @@ class UnauthorizedAPIAccessError(YodaError):
pass


class InvalidAPIError(YodaError):
pass


class MissingDataError(YodaError):
pass

0 comments on commit 13b319c

Please sign in to comment.