Skip to content

Commit bc1c8a2

Browse files
fix(gateway): refuse a plug tool's negative or zero id, negative count and unparsed offset (#791)
Co-authored-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Co-authored-by: richard-epsilla <131846445+richard-epsilla@users.noreply.github.com>
1 parent 814632b commit bc1c8a2

2 files changed

Lines changed: 157 additions & 29 deletions

File tree

‎gateway/plugs_plane.py‎

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _need(args: dict, key: str):
180180
return v
181181

182182

183-
def _whole(args: dict, key: str, default=None) -> int:
183+
def _whole(args: dict, key: str, default=None, *, minimum: int | None = None) -> int:
184184
v = args.get(key)
185185
if v is None or (isinstance(v, str) and not v.strip()):
186186
if default is not None:
@@ -198,6 +198,8 @@ def _whole(args: dict, key: str, default=None) -> int:
198198
raise PlugToolError(f"{key} must be a whole number")
199199
if whole == 0 and default is not None:
200200
return default
201+
if minimum is not None and whole < minimum:
202+
raise PlugToolError(f"{key} must be at least {minimum}")
201203
return whole
202204

203205

@@ -402,7 +404,7 @@ async def _(c, f, cfg, a):
402404
_obj({"query": _s("A name, part of a name, or an address"), "top": _i("How many, at most 50")}, ("query",)))
403405
async def _(c, f, cfg, a):
404406
q = str(_need(a, "query")).replace('"', "")
405-
top = min(_whole(a, "top", 10), 50)
407+
top = min(_whole(a, "top", 10, minimum=1), 50)
406408
d = await _graph(c, f, cfg, "GET", "/users",
407409
params={"$search": f'"displayName:{q}" OR "mail:{q}" OR "userPrincipalName:{q}"',
408410
"$select": "id,displayName,userPrincipalName,mail,jobTitle,department", "$top": top},
@@ -417,7 +419,7 @@ async def _(c, f, cfg, a):
417419
async def _(c, f, cfg, a):
418420
q = str(a.get("query") or "*").replace("'", "")
419421
d = await _graph(c, f, cfg, "GET", "/sites", params={"search": q, "$select": "id,name,displayName,webUrl",
420-
"$top": min(_whole(a, "top", 25), 100)})
422+
"$top": min(_whole(a, "top", 25, minimum=1), 100)})
421423
return [{"site": _site_ref(s), **_pick(s, ("name", "displayName", "webUrl", "id"))} for s in d.get("value") or []]
422424

423425

@@ -431,7 +433,7 @@ async def _(c, f, cfg, a):
431433
path = _m365_path(a)
432434
url = f"{root}/root/children" if not path else f"{root}/root:/{path}:/children"
433435
d = await _graph(c, f, cfg, "GET", url, params={"$select": "name,id,size,lastModifiedDateTime,webUrl,folder,file,parentReference",
434-
"$top": min(_whole(a, "top", 50), 200)})
436+
"$top": min(_whole(a, "top", 50, minimum=1), 200)})
435437
return {"resource": where, "path": "/" + path, "items": [_item_out(x) for x in d.get("value") or []]}
436438

437439

@@ -443,7 +445,7 @@ async def _(c, f, cfg, a):
443445
root, where = _m365_drive(cfg, a)
444446
q = str(_need(a, "query")).replace("'", "''")
445447
d = await _graph(c, f, cfg, "GET", f"{root}/root/search(q='{q}')",
446-
params={"$select": "name,id,size,lastModifiedDateTime,webUrl,folder,file,parentReference", "$top": min(_whole(a, "top", 25), 100)})
448+
params={"$select": "name,id,size,lastModifiedDateTime,webUrl,folder,file,parentReference", "$top": min(_whole(a, "top", 25, minimum=1), 100)})
447449
return {"resource": where, "query": a.get("query"), "items": [_item_out(x) for x in d.get("value") or []]}
448450

449451

@@ -492,7 +494,7 @@ def _addr(x) -> str:
492494
async def _(c, f, cfg, a):
493495
prefix, who = _m365_person(cfg, a)
494496
folder = str(a.get("folder") or "inbox").strip()
495-
params = {"$select": "id,subject,from,receivedDateTime,isRead,hasAttachments,bodyPreview,webLink", "$top": min(_whole(a, "top", 20), 50)}
497+
params = {"$select": "id,subject,from,receivedDateTime,isRead,hasAttachments,bodyPreview,webLink", "$top": min(_whole(a, "top", 20, minimum=1), 50)}
496498
if a.get("search"):
497499
params["$search"] = '"' + str(a["search"]).replace('"', "") + '"'
498500
else:
@@ -526,7 +528,7 @@ async def _(c, f, cfg, a):
526528
d = await _graph(c, f, cfg, "GET", f"{prefix}/calendarView",
527529
params={"startDateTime": str(_need(a, "start")), "endDateTime": str(_need(a, "end")),
528530
"$select": "id,subject,start,end,location,organizer,attendees,isAllDay,webLink",
529-
"$orderby": "start/dateTime", "$top": min(_whole(a, "top", 50), 100)})
531+
"$orderby": "start/dateTime", "$top": min(_whole(a, "top", 50, minimum=1), 100)})
530532
out = []
531533
for e in d.get("value") or []:
532534
out.append({**_pick(e, ("id", "subject", "isAllDay", "webLink")),
@@ -640,7 +642,7 @@ async def _(c, f, cfg, a):
640642
"per_page": _i("How many, at most 100")}))
641643
async def _(c, f, cfg, a):
642644
params = {k: a[k] for k in ("sha", "path") if a.get(k)}
643-
params["per_page"] = min(_whole(a, "per_page", 30), 100)
645+
params["per_page"] = min(_whole(a, "per_page", 30, minimum=1), 100)
644646
return [_commit_out(x) for x in await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/commits", params=params)]
645647

646648

@@ -663,20 +665,20 @@ async def _(c, f, cfg, a):
663665
"per_page": _i("How many, at most 100")}))
664666
async def _(c, f, cfg, a):
665667
params = {k: a[k] for k in ("state", "labels") if a.get(k)}
666-
params["per_page"] = min(_whole(a, "per_page", 30), 100)
668+
params["per_page"] = min(_whole(a, "per_page", 30, minimum=1), 100)
667669
d = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/issues", params=params)
668670
return [_issue_out(x) for x in d if "pull_request" not in x]
669671

670672

671673
@_tool("github", "get_issue", "read", "One issue.", _obj({"number": _i("Issue number")}, ("number",)))
672674
async def _(c, f, cfg, a):
673-
return _issue_out(await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number')}"))
675+
return _issue_out(await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number', minimum=1)}"))
674676

675677

676678
@_tool("github", "get_issue_comments", "read", "Comments on an issue or pull request.",
677679
_obj({"number": _i("Issue or pull request number")}, ("number",)))
678680
async def _(c, f, cfg, a):
679-
d = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number')}/comments", params={"per_page": 100})
681+
d = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number', minimum=1)}/comments", params={"per_page": 100})
680682
return [{**_pick(x, ("id", "body", "created_at", "html_url")), "user": _user(x)} for x in d]
681683

682684

@@ -690,26 +692,26 @@ async def _(c, f, cfg, a):
690692
@_tool("github", "list_pull_requests", "read", "Pull requests of the repository.",
691693
_obj({"state": _s("open, closed or all", enum=["open", "closed", "all"]), "per_page": _i("How many, at most 100")}))
692694
async def _(c, f, cfg, a):
693-
params = {"state": a.get("state") or "open", "per_page": min(_whole(a, "per_page", 30), 100)}
695+
params = {"state": a.get("state") or "open", "per_page": min(_whole(a, "per_page", 30, minimum=1), 100)}
694696
return [_pull_out(x) for x in await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls", params=params)]
695697

696698

697699
@_tool("github", "get_pull_request", "read", "One pull request.", _obj({"number": _i("Pull request number")}, ("number",)))
698700
async def _(c, f, cfg, a):
699-
return _pull_out(await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number')}"))
701+
return _pull_out(await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number', minimum=1)}"))
700702

701703

702704
@_tool("github", "get_pull_request_files", "read", "Files a pull request changes.",
703705
_obj({"number": _i("Pull request number")}, ("number",)))
704706
async def _(c, f, cfg, a):
705-
d = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number')}/files", params={"per_page": 100})
707+
d = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number', minimum=1)}/files", params={"per_page": 100})
706708
return [_pick(x, ("filename", "status", "additions", "deletions", "patch")) for x in d]
707709

708710

709711
@_tool("github", "get_pull_request_diff", "read", "The unified diff of a pull request.",
710712
_obj({"number": _i("Pull request number")}, ("number",)))
711713
async def _(c, f, cfg, a):
712-
return await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number')}",
714+
return await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number', minimum=1)}",
713715
accept="application/vnd.github.diff", text=True)
714716

715717

@@ -723,7 +725,7 @@ async def _(c, f, cfg, a):
723725
_obj({"workflow_id": _s("Workflow id or file name (ci.yml); all workflows when omitted"),
724726
"branch": _s("Only runs on this branch"), "per_page": _i("How many, at most 100")}))
725727
async def _(c, f, cfg, a):
726-
params = {"per_page": min(_whole(a, "per_page", 20), 100)}
728+
params = {"per_page": min(_whole(a, "per_page", 20, minimum=1), 100)}
727729
if a.get("branch"):
728730
params["branch"] = a["branch"]
729731
path = (f"/repos/{_repo(cfg, a)}/actions/workflows/{a['workflow_id']}/runs" if a.get("workflow_id")
@@ -735,7 +737,7 @@ async def _(c, f, cfg, a):
735737

736738
@_tool("github", "get_workflow_run", "read", "One workflow run with its jobs.", _obj({"run_id": _i("Run id")}, ("run_id",)))
737739
async def _(c, f, cfg, a):
738-
rid = _whole(a, "run_id")
740+
rid = _whole(a, "run_id", minimum=1)
739741
d = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/actions/runs/{rid}")
740742
jobs = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/actions/runs/{rid}/jobs")
741743
return {**_pick(d, ("id", "name", "head_branch", "head_sha", "status", "conclusion", "html_url", "created_at", "updated_at")),
@@ -744,7 +746,7 @@ async def _(c, f, cfg, a):
744746

745747
@_tool("github", "get_job_logs", "read", "The log of one workflow job (the tail, when long).", _obj({"job_id": _i("Job id")}, ("job_id",)))
746748
async def _(c, f, cfg, a):
747-
text = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/actions/jobs/{_whole(a, 'job_id')}/logs", accept="*/*", text=True)
749+
text = await _gh(c, f, "GET", f"/repos/{_repo(cfg, a)}/actions/jobs/{_whole(a, 'job_id', minimum=1)}/logs", accept="*/*", text=True)
748750
return text if len(text) <= 60_000 else "…" + text[-60_000:]
749751

750752

@@ -834,13 +836,13 @@ async def _(c, f, cfg, a):
834836
"labels": {"type": "array", "items": {"type": "string"}, "description": "Label names (replaces the set)"}}, ("number",)))
835837
async def _(c, f, cfg, a):
836838
body = {k: a[k] for k in ("title", "body", "state", "labels") if a.get(k) is not None}
837-
return _issue_out(await _gh(c, f, "PATCH", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number')}", body=body))
839+
return _issue_out(await _gh(c, f, "PATCH", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number', minimum=1)}", body=body))
838840

839841

840842
@_tool("github", "add_issue_comment", "write", "Comment on an issue or pull request.",
841843
_obj({"number": _i("Issue or pull request number"), "body": _s("Comment (markdown)")}, ("number", "body")))
842844
async def _(c, f, cfg, a):
843-
d = await _gh(c, f, "POST", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number')}/comments", body={"body": _need(a, "body")})
845+
d = await _gh(c, f, "POST", f"/repos/{_repo(cfg, a)}/issues/{_whole(a, 'number', minimum=1)}/comments", body={"body": _need(a, "body")})
844846
return {**_pick(d, ("id", "html_url", "created_at")), "user": _user(d)}
845847

846848

@@ -858,15 +860,15 @@ async def _(c, f, cfg, a):
858860
"state": _s("open or closed", enum=["open", "closed"]), "base": _s("New base branch")}, ("number",)))
859861
async def _(c, f, cfg, a):
860862
body = {k: a[k] for k in ("title", "body", "state", "base") if a.get(k) is not None}
861-
return _pull_out(await _gh(c, f, "PATCH", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number')}", body=body))
863+
return _pull_out(await _gh(c, f, "PATCH", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number', minimum=1)}", body=body))
862864

863865

864866
@_tool("github", "create_pull_request_review", "write", "Review a pull request: approve, request changes or comment.",
865867
_obj({"number": _i("Pull request number"), "event": _s("APPROVE, REQUEST_CHANGES or COMMENT", enum=["APPROVE", "REQUEST_CHANGES", "COMMENT"]),
866868
"body": _s("Review text")}, ("number", "event")))
867869
async def _(c, f, cfg, a):
868870
body = {"event": _need(a, "event"), **({"body": a["body"]} if a.get("body") else {})}
869-
d = await _gh(c, f, "POST", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number')}/reviews", body=body)
871+
d = await _gh(c, f, "POST", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number', minimum=1)}/reviews", body=body)
870872
return {**_pick(d, ("id", "state", "html_url", "submitted_at")), "user": _user(d)}
871873

872874

@@ -875,7 +877,7 @@ async def _(c, f, cfg, a):
875877
"commit_title": _s("Title of the merge commit")}, ("number",)))
876878
async def _(c, f, cfg, a):
877879
body = {k: a[k] for k in ("merge_method", "commit_title") if a.get(k)}
878-
d = await _gh(c, f, "PUT", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number')}/merge", body=body)
880+
d = await _gh(c, f, "PUT", f"/repos/{_repo(cfg, a)}/pulls/{_whole(a, 'number', minimum=1)}/merge", body=body)
879881
return _pick(d, ("sha", "merged", "message"))
880882

881883

@@ -890,14 +892,14 @@ async def _(c, f, cfg, a):
890892

891893
@_tool("github", "rerun_workflow_run", "write", "Re-run a workflow run.", _obj({"run_id": _i("Run id")}, ("run_id",)))
892894
async def _(c, f, cfg, a):
893-
rid = _whole(a, "run_id")
895+
rid = _whole(a, "run_id", minimum=1)
894896
await _gh(c, f, "POST", f"/repos/{_repo(cfg, a)}/actions/runs/{rid}/rerun")
895897
return {"rerun": True, "run_id": rid}
896898

897899

898900
@_tool("github", "cancel_workflow_run", "write", "Cancel a workflow run.", _obj({"run_id": _i("Run id")}, ("run_id",)))
899901
async def _(c, f, cfg, a):
900-
rid = _whole(a, "run_id")
902+
rid = _whole(a, "run_id", minimum=1)
901903
await _gh(c, f, "POST", f"/repos/{_repo(cfg, a)}/actions/runs/{rid}/cancel")
902904
return {"cancelled": True, "run_id": rid}
903905

@@ -959,7 +961,7 @@ async def _(c, f, cfg, a):
959961
_obj({"limit": _i("How many, at most 100"), "state": _s("Only this state (READY, ERROR, BUILDING, QUEUED, CANCELED)"),
960962
"target": _s("production or preview", enum=["production", "preview"])}))
961963
async def _(c, f, cfg, a):
962-
params = {"projectId": _project(cfg), "limit": min(_whole(a, "limit", 20), 100)}
964+
params = {"projectId": _project(cfg), "limit": min(_whole(a, "limit", 20, minimum=1), 100)}
963965
for k in ("state", "target"):
964966
if a.get(k):
965967
params[k] = a[k]
@@ -976,7 +978,7 @@ async def _(c, f, cfg, a):
976978
_obj({"id": _s("Deployment id"), "limit": _i("How many lines, at most 500")}, ("id",)))
977979
async def _(c, f, cfg, a):
978980
d = await _vc(c, f, cfg, "GET", f"/v3/deployments/{_need(a, 'id')}/events",
979-
params={"limit": min(_whole(a, "limit", 200), 500), "builds": 1, "direction": "backward"})
981+
params={"limit": min(_whole(a, "limit", 200, minimum=1), 500), "builds": 1, "direction": "backward"})
980982
rows = d if isinstance(d, list) else d.get("events") or []
981983
lines = []
982984
for e in rows:
@@ -1156,10 +1158,13 @@ async def _(c, f, cfg, a):
11561158
"order": _s("Column with direction, e.g. created_at.desc"), "limit": _i("At most this many rows (default 100)"),
11571159
"offset": _i("Skip this many rows")}, ("table",)))
11581160
async def _(c, f, cfg, a):
1159-
params = {**_filters(a), "limit": min(_whole(a, "limit", 100), 1000)}
1160-
for k in ("select", "order", "offset"):
1161+
params = {**_filters(a), "limit": min(_whole(a, "limit", 100, minimum=1), 1000)}
1162+
for k in ("select", "order"):
11611163
if a.get(k) not in (None, ""):
11621164
params[k] = a[k]
1165+
offset = _whole(a, "offset", 0, minimum=0)
1166+
if offset > 0:
1167+
params["offset"] = offset
11631168
return await _inf(c, f, cfg, "GET", f"/api/database/records/{_need(a, 'table')}", params=params)
11641169

11651170

0 commit comments

Comments
 (0)