Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Disallow redirects for external requests

Michal Widera3e6915b142b5
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index 89ecdbe17b..3dc70d4b78 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -42,13 +42,19 @@ def send_app_token(target_url: str, token: str):
}
json_data = {"auth_token": token}
response = requests.post(
- target_url, json=json_data, headers=headers, timeout=REQUEST_TIMEOUT
+ target_url,
+ json=json_data,
+ headers=headers,
+ timeout=REQUEST_TIMEOUT,
+ allow_redirects=False,
)
validate_app_install_response(response)
def install_app(app_installation: AppInstallation, activate: bool = False):
- response = requests.get(app_installation.manifest_url, timeout=REQUEST_TIMEOUT)
+ response = requests.get(
+ app_installation.manifest_url, timeout=REQUEST_TIMEOUT, allow_redirects=False
+ )
response.raise_for_status()
assigned_permissions = app_installation.permissions.all()
manifest_data = response.json()
diff --git a/saleor/app/management/commands/create_app.py b/saleor/app/management/commands/create_app.py
index 9b8a8f92e2..3bfe4eaf4d 100644
--- a/saleor/app/management/commands/create_app.py
+++ b/saleor/app/management/commands/create_app.py
@@ -50,7 +50,13 @@ class Command(BaseCommand):
AppHeaders.API_URL: build_absolute_uri(reverse("api"), domain),
}
try:
- response = requests.post(target_url, json=data, headers=headers, timeout=15)
+ response = requests.post(
+ target_url,
+ json=data,
+ headers=headers,
+ timeout=15,
+ allow_redirects=False,
+ )
except RequestException as e:
raise CommandError(f"Request failed. Exception: {e}")
response.raise_for_status()
diff --git a/saleor/app/management/commands/install_app.py b/saleor/app/management/commands/install_app.py
index d03567f10d..caea182593 100644
--- a/saleor/app/management/commands/install_app.py
+++ b/saleor/app/management/commands/install_app.py
@@ -33,7 +33,7 @@ class Command(BaseCommand):
raise CommandError(f"Incorrect format of manifest-url: {manifest_url}")
def fetch_manifest_data(self, manifest_url: str) -> dict:
- response = requests.get(manifest_url)
+ response = requests.get(manifest_url, timeout=30, allow_redirects=False)
response.raise_for_status()
return response.json()
diff --git a/saleor/app/tests/test_app_commands.py b/saleor/app/tests/test_app_commands.py
index dddfdf88a8..306d4c5d20 100644
--- a/saleor/app/tests/test_app_commands.py
+++ b/saleor/app/tests/test_app_commands.py
@@ -67,6 +67,7 @@ def test_creates_app_from_manifest_sends_token(monkeypatch):
},
json={"auth_token": ANY},
timeout=ANY,
+ allow_redirects=False,
)
@@ -132,4 +133,5 @@ def test_sends_data_to_target_url(monkeypatch):
},
json={"auth_token": ANY},
timeout=ANY,
+ allow_redirects=False,
)
diff --git a/saleor/app/tests/test_installation_utils.py b/saleor/app/tests/test_installation_utils.py
index 7d0841817a..8aaee982e1 100644
--- a/saleor/app/tests/test_installation_utils.py
+++ b/saleor/app/tests/test_installation_utils.py
@@ -71,6 +71,7 @@ def test_install_app_created_app(
},
json={"auth_token": ANY},
timeout=ANY,
+ allow_redirects=False,
)
assert App.objects.get().id == app.id
assert list(app.permissions.all()) == [permission_manage_products]
diff --git a/saleor/graphql/app/mutations/app_fetch_manifest.py b/saleor/graphql/app/mutations/app_fetch_manifest.py
index e028375029..4f44c3c326 100644
--- a/saleor/graphql/app/mutations/app_fetch_manifest.py
+++ b/saleor/graphql/app/mutations/app_fetch_manifest.py
@@ -35,7 +35,9 @@ class AppFetchManifest(BaseMutation):
@classmethod
def fetch_manifest(cls, manifest_url):
try:
- response = requests.get(manifest_url, timeout=REQUEST_TIMEOUT)
+ response = requests.get(
+ manifest_url, timeout=REQUEST_TIMEOUT, allow_redirects=False
+ )
response.raise_for_status()
return response.json()
except requests.Timeout:
diff --git a/saleor/graphql/core/validators/file.py b/saleor/graphql/core/validators/file.py
index 6e3acff972..ac24cb1c8d 100644
--- a/saleor/graphql/core/validators/file.py
+++ b/saleor/graphql/core/validators/file.py
@@ -41,7 +41,7 @@ def validate_image_url(url: str, field_name: str, error_code: str) -> None:
Instead of the whole file, only the headers are fetched.
"""
- head = requests.head(url)
+ head = requests.head(url, timeout=30, allow_redirects=False)
header = head.headers
content_type = header.get("content-type")
if content_type is None or not is_supported_image_mimetype(content_type):
diff --git a/saleor/graphql/product/bulk_mutations/product_bulk_create.py b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
index 73ec0f1495..691696aaf4 100644
--- a/saleor/graphql/product/bulk_mutations/product_bulk_create.py
+++ b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
@@ -796,7 +796,9 @@ class ProductBulkCreate(BaseMutation):
media_url, "media_url", ProductBulkCreateErrorCode.INVALID.value
)
filename = get_filename_from_url(media_url)
- image_data = requests.get(media_url, stream=True)
+ image_data = requests.get(
+ media_url, stream=True, timeout=30, allow_redirects=False
+ )
image_data = File(image_data.raw, filename)
media_to_create.append(
models.ProductMedia(
diff --git a/saleor/graphql/product/mutations/product/product_media_create.py b/saleor/graphql/product/mutations/product/product_media_create.py
index 56f6c3bd80..fa63223c89 100644
--- a/saleor/graphql/product/mutations/product/product_media_create.py
+++ b/saleor/graphql/product/mutations/product/product_media_create.py
@@ -114,7 +114,9 @@ class ProductMediaCreate(BaseMutation):
media_url, "media_url", ProductErrorCode.INVALID.value
)
filename = get_filename_from_url(media_url)
- image_data = requests.get(media_url, stream=True)
+ image_data = requests.get(
+ media_url, stream=True, timeout=30, allow_redirects=False
+ )
image_file = File(image_data.raw, filename)
media = product.media.create(
image=image_file,
diff --git a/saleor/payment/gateways/adyen/tests/utils/test_apple_pay.py b/saleor/payment/gateways/adyen/tests/utils/test_apple_pay.py
index 513788b5e9..6b8e6f9b76 100644
--- a/saleor/payment/gateways/adyen/tests/utils/test_apple_pay.py
+++ b/saleor/payment/gateways/adyen/tests/utils/test_apple_pay.py
@@ -111,7 +111,11 @@ def test_initialize_payment_for_apple_pay(mocked_request, mocked_tmp_file):
}
mocked_request.assert_called_with(
- validation_url, json=expected_data, cert=mocked_cert_file_name
+ validation_url,
+ json=expected_data,
+ cert=mocked_cert_file_name,
+ timeout=30,
+ allow_redirects=False,
)
diff --git a/saleor/payment/gateways/adyen/utils/apple_pay.py b/saleor/payment/gateways/adyen/utils/apple_pay.py
index ee0a49c618..fb4223dc68 100644
--- a/saleor/payment/gateways/adyen/utils/apple_pay.py
+++ b/saleor/payment/gateways/adyen/utils/apple_pay.py
@@ -101,7 +101,13 @@ def make_request_to_initialize_apple_pay(
with NamedTemporaryFile() as f:
f.write(certificate.encode())
f.flush() # ensure all data written
- return requests.post(validation_url, json=request_data, cert=f.name)
+ return requests.post(
+ validation_url,
+ json=request_data,
+ cert=f.name,
+ timeout=30,
+ allow_redirects=False,
+ )
def initialize_apple_pay(payment_data: dict, certificate: str) -> dict:
diff --git a/saleor/payment/gateways/authorize_net/__init__.py b/saleor/payment/gateways/authorize_net/__init__.py
index d236ac8b02..027c49f948 100644
--- a/saleor/payment/gateways/authorize_net/__init__.py
+++ b/saleor/payment/gateways/authorize_net/__init__.py
@@ -39,7 +39,11 @@ def authenticate_test(
}
}
response = requests.post(
- url, json=data, headers={"content-type": "application/json"}
+ url,
+ json=data,
+ headers={"content-type": "application/json"},
+ timeout=30,
+ allow_redirects=False,
)
# Response content is utf-8-sig, which requires usage of json.loads
result = json.loads(response.content)
diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.py
index 3bcdf0d447..aea54366f2 100644
--- a/saleor/payment/gateways/np_atobarai/api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py
@@ -57,6 +57,7 @@ def _request(
json=json or {},
auth=HTTPBasicAuth(config.merchant_code, config.sp_code),
headers={"X-NP-Terminal-Id": config.terminal_id},
+ allow_redirects=False,
)
# NP Atobarai returns error codes with http status code 400
# Because we want to pass those errors to the end user,
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index a04ebd7063..a193c32638 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -97,7 +97,13 @@ def api_post_request(
response = None
try:
auth = HTTPBasicAuth(config.username_or_account, config.password_or_license)
- response = requests.post(url, auth=auth, data=json.dumps(data), timeout=TIMEOUT)
+ response = requests.post(
+ url,
+ auth=auth,
+ data=json.dumps(data),
+ timeout=TIMEOUT,
+ allow_redirects=False,
+ )
logger.debug("Hit to Avatax to calculate taxes %s", url)
json_response = response.json()
if "error" in response: # type: ignore
@@ -123,7 +129,7 @@ def api_get_request(
response = None
try:
auth = HTTPBasicAuth(username_or_account, password_or_license)
- response = requests.get(url, auth=auth, timeout=TIMEOUT)
+ response = requests.get(url, auth=auth, timeout=TIMEOUT, allow_redirects=False)
json_response = response.json()
logger.debug("[GET] Hit to %s", url)
if "error" in json_response:
diff --git a/saleor/plugins/openid_connect/utils.py b/saleor/plugins/openid_connect/utils.py
index eeb75c35dd..ce97cdeead 100644
--- a/saleor/plugins/openid_connect/utils.py
+++ b/saleor/plugins/openid_connect/utils.py
@@ -64,7 +64,9 @@ def fetch_jwks(jwks_url) -> Optional[dict]:
"""
response = None
try:
- response = requests.get(jwks_url, timeout=REQUEST_TIMEOUT)
+ response = requests.get(
+ jwks_url, timeout=REQUEST_TIMEOUT, allow_redirects=False
+ )
response.raise_for_status()
jwks = response.json()
except requests.exceptions.RequestException:
@@ -120,6 +122,7 @@ def get_user_info(user_info_url, access_token) -> Optional[dict]:
user_info_url,
headers={"Authorization": f"Bearer {access_token}"},
timeout=REQUEST_TIMEOUT,
+ allow_redirects=False,
)
response.raise_for_status()
return response.json()
diff --git a/saleor/plugins/webhook/tasks.py b/saleor/plugins/webhook/tasks.py
index 1eef953674..07678b3494 100644
--- a/saleor/plugins/webhook/tasks.py
+++ b/saleor/plugins/webhook/tasks.py
@@ -356,7 +356,11 @@ def send_webhook_using_http(
try:
response = requests.post(
- target_url, data=message, headers=headers, timeout=timeout
+ target_url,
+ data=message,
+ headers=headers,
+ timeout=timeout,
+ allow_redirects=False,
)
except RequestException as e:
if e.response:
diff --git a/saleor/plugins/webhook/tests/test_tasks.py b/saleor/plugins/webhook/tests/test_tasks.py
index 348cbfcbe2..f9b96a41f9 100644
--- a/saleor/plugins/webhook/tests/test_tasks.py
+++ b/saleor/plugins/webhook/tests/test_tasks.py
@@ -217,7 +217,11 @@ def test_handle_transaction_request_task_with_only_psp_reference(
event.refresh_from_db()
assert event.psp_reference == expected_psp_reference
mocked_post_request.assert_called_once_with(
- target_url, data=payload.encode("utf-8"), headers=mock.ANY, timeout=mock.ANY
+ target_url,
+ data=payload.encode("utf-8"),
+ headers=mock.ANY,
+ timeout=mock.ANY,
+ allow_redirects=False,
)
@@ -349,7 +353,11 @@ def test_handle_transaction_request_task_with_missing_psp_reference(
assert failure_event.amount_value == event.amount_value
assert failure_event.transaction_id == event.transaction_id
mocked_post_request.assert_called_once_with(
- target_url, data=payload.encode("utf-8"), headers=mock.ANY, timeout=mock.ANY
+ target_url,
+ data=payload.encode("utf-8"),
+ headers=mock.ANY,
+ timeout=mock.ANY,
+ allow_redirects=False,
)
@@ -428,7 +436,11 @@ def test_handle_transaction_request_task_with_missing_required_event_field(
assert failure_event.amount_value == event.amount_value
assert failure_event.transaction_id == event.transaction_id
mocked_post_request.assert_called_once_with(
- target_url, data=payload.encode("utf-8"), headers=mock.ANY, timeout=mock.ANY
+ target_url,
+ data=payload.encode("utf-8"),
+ headers=mock.ANY,
+ timeout=mock.ANY,
+ allow_redirects=False,
)
@@ -522,7 +534,11 @@ def test_handle_transaction_request_task_with_result_event(
assert success_event.message == event_cause
mocked_post_request.assert_called_once_with(
- target_url, data=payload.encode("utf-8"), headers=mock.ANY, timeout=mock.ANY
+ target_url,
+ data=payload.encode("utf-8"),
+ headers=mock.ANY,
+ timeout=mock.ANY,
+ allow_redirects=False,
)
@@ -610,7 +626,11 @@ def test_handle_transaction_request_task_with_only_required_fields_for_result_ev
assert success_event.message == ""
mocked_post_request.assert_called_once_with(
- target_url, data=payload.encode("utf-8"), headers=mock.ANY, timeout=mock.ANY
+ target_url,
+ data=payload.encode("utf-8"),
+ headers=mock.ANY,
+ timeout=mock.ANY,
+ allow_redirects=False,
)
diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/plugins/webhook/tests/test_webhook_protocols.py
index 1ce1505ea8..6f8f8b4563 100644
--- a/saleor/plugins/webhook/tests/test_webhook_protocols.py
+++ b/saleor/plugins/webhook/tests/test_webhook_protocols.py
@@ -259,6 +259,7 @@ def test_trigger_webhooks_with_http(
data=bytes(expected_data, "utf-8"),
headers=expected_headers,
timeout=10,
+ allow_redirects=False,
)
@@ -303,6 +304,7 @@ def test_trigger_webhooks_with_http_and_secret_key(
data=bytes(expected_data, "utf-8"),
headers=expected_headers,
timeout=10,
+ allow_redirects=False,
)
@@ -351,6 +353,7 @@ def test_trigger_webhooks_with_http_and_secret_key_as_empty_string(
data=bytes(expected_data, "utf-8"),
headers=expected_headers,
timeout=10,
+ allow_redirects=False,
)