Skip to content

Commit

Permalink
Fix issue with limit duplication leading to errors
Browse files Browse the repository at this point in the history
[this
commit](dc75bc9)
introduced a default limit in the client. However, the limit is also
present in the URL. That leads to the 2nd and subsequent pages failing
due to passing `limit=X&limit=X`. That is interpreted as an array by the
server, and will fail with a client error.

The fix checks the URL for the limit, and avoids the duplication if
present.
  • Loading branch information
garethr committed Oct 11, 2023
1 parent dc75bc9 commit c415757
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions snyk/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import urllib.parse
from typing import Any, List, Optional
from urllib.parse import parse_qs, urlparse

import requests
from retry.api import retry_call
Expand Down Expand Up @@ -162,6 +163,11 @@ def get(
if isinstance(v, bool):
params[k] = str(v).lower()

# the limit is returned in the url, and if two limits are passed
# the API interprets as an array and throws an error
if "limit" in parse_qs(urlparse(path).query):
params.pop("limit", None)

debug_url = f"{url}&{urllib.parse.urlencode(params)}"
fkwargs = {"headers": self.api_headers, "params": params}
else:
Expand Down
4 changes: 1 addition & 3 deletions snyk/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ def _rest_to_v1_response_format(self, project):

def _query(self, tags: List[Dict[str, str]] = [], next_url: str = None):
projects = []
params: dict = {
"limit": 100,
}
params: dict = {"limit": 100}
if self.instance:
path = "/orgs/%s/projects" % self.instance.id if not next_url else next_url

Expand Down
7 changes: 7 additions & 0 deletions snyk/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,10 @@ def test_get_rest_pages(
data = rest_client.get_rest_pages(f"orgs/{V3_ORG}/targets", t_params)

assert len(data) == 30

def test_rest_limit_deduplication(self, requests_mock, rest_client):
requests_mock.get(
f"{REST_URL}/orgs/{REST_ORG}/projects?limit=100&version={REST_VERSION}"
)
params = {"limit": 10}
rest_client.get(f"orgs/{REST_ORG}/projects?limit=100", params)

0 comments on commit c415757

Please sign in to comment.