T09 · Insecure Skill Coding Practices
- Location
- scripts/spapi.py:58
- Finding
- Configurable SP-API Endpoint Can Receive Amazon Access Tokens and Sensitive Payloads<![CDATA[ ## Vulnerability Details **File Location**: `scripts/spapi.py:58-60, 90-109` **Vulnerability Type**: Unrestricted authenticated API destination **Risk Level**: High ### Vulnerable Code ```python ENDPOINT = os.environ.get("SPAPI_ENDPOINT") or _config().get( "endpoint", "https://sellingpartnerapi-na.amazon.com") MARKETPLACE = os.environ.get("SPAPI_MARKETPLACE_ID") or _config().get( "marketplace_id", "ATVPDKIKX0DER") ``` ```python def headers() -> dict: return {"x-amz-access-token": access_token(), "Content-Type": "application/json"} def request(method: str, url: str, body=None, params=None, retries: int = 6): if params: url += "?" + urllib.parse.urlencode(params) data = json.dumps(body).encode() if body is not None else None last: Exception | None = None for attempt in range(retries): req = urllib.request.Request(url, data=data, headers=headers(), method=method) try: with urllib.request.urlopen(req, timeout=40) as resp: raw = resp.read() return json.loads(raw) if raw else {} ``` ### Technical Analysis The base SP-API endpoint can be supplied through either the `SPAPI_ENDPOINT` environment variable or the `endpoint` property in `~/.config/sp-api/credentials.json`. The value is not validated to ensure that: - The scheme is HTTPS. - The destination belongs to an approved Amazon SP-API domain. - User information is not present in the URL. - Redirects do not move the request to an untrusted destination. Every request constructed from this endpoint includes the `x-amz-access-token` bearer credential. State-changing workflow requests can also include seller inventory, SKUs, source addresses, telephone numbers, email addresses, carrier selections, and shipment identifiers. Reading SP-API credentials and sending a derived access token to Amazon is necessary for the declared functionality. Allowing the authenticated destination to be an arbitrary host exceeds ...[truncated 1246 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Parse the endpoint with `urllib.parse.urlparse`. 2. Require `scheme == "https"`. 3. Allowlist the documented regional Amazon SP-API hosts, for example: - `sellingpartnerapi-na.amazon.com` - `sellingpartnerapi-eu.amazon.com` - `sellingpartnerapi-fe.amazon.com` 4. Reject URL credentials, unexpected ports, fragments, and paths in the configured base endpoint. 5. Prevent authenticated requests from following redirects to hosts outside the allowlist. 6. Separate unauthenticated downloads from the authenticated SP-API request function. 7. Fail closed with a clear error when endpoint validation fails. ]]>
