Skip to content

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

Allow to add url without TLD to install_app (#6956)

Damian Wysockifd91c9ca60bc
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index 941947e9f8..87ebd8c37e 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -1,8 +1,8 @@
import requests
from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError
-from django.core.validators import URLValidator
+from ..app.validators import AppURLValidator
from .models import App, AppInstallation
from .types import AppType
@@ -21,9 +21,8 @@ def send_app_token(target_url: str, token: str):
def validate_manifest_fields(manifest_data):
token_target_url = manifest_data.get("tokenTargetUrl")
-
try:
- url_validator = URLValidator()
+ url_validator = AppURLValidator()
url_validator(token_target_url)
except ValidationError:
raise ValidationError({"tokenTargetUrl": "Incorrect format."})
diff --git a/saleor/app/management/commands/install_app.py b/saleor/app/management/commands/install_app.py
index 050713ae02..0ddaf186f4 100644
--- a/saleor/app/management/commands/install_app.py
+++ b/saleor/app/management/commands/install_app.py
@@ -5,8 +5,8 @@ import requests
from django.core.exceptions import ValidationError
from django.core.management import BaseCommand, CommandError
from django.core.management.base import CommandParser
-from django.core.validators import URLValidator
+from ....app.validators import AppURLValidator
from ....core import JobStatus
from ...installation_utils import install_app
from ...models import AppInstallation
@@ -26,7 +26,7 @@ class Command(BaseCommand):
)
def validate_manifest_url(self, manifest_url: str):
- url_validator = URLValidator()
+ url_validator = AppURLValidator()
try:
url_validator(manifest_url)
except ValidationError:
diff --git a/saleor/app/tests/cassettes/test_creates_app_from_manifest.yaml b/saleor/app/tests/cassettes/test_creates_app_from_manifest.yaml
index d5af3d283c..fad50c61d0 100644
--- a/saleor/app/tests/cassettes/test_creates_app_from_manifest.yaml
+++ b/saleor/app/tests/cassettes/test_creates_app_from_manifest.yaml
@@ -11,7 +11,7 @@ interactions:
User-Agent:
- python-requests/2.23.0
method: GET
- uri: http://localhost:3000/manifest
+ uri: http://otherapp:3000/manifest
response:
body:
string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"aboutApp\"\
@@ -19,7 +19,7 @@ interactions:
: \"http://localhost:8888/app-data-privacy\",\n \"homepageUrl\": \"http://localhost:8888/homepage\"\
,\n \"supportUrl\": \"http://localhost:8888/support\",\n \"configurationUrl\"\
: \"htpp://localhost:8888/configuration\",\n \"appUrl\": \"http://localhost:8888/app\"\
- ,\n \"tokenTargetUrl\": \"http://localhost:3000/register\",\n \"id\": \"\
+ ,\n \"tokenTargetUrl\": \"http://otherapp:3000/register\",\n \"id\": \"\
app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
\n ]\n}"
headers:
@@ -62,7 +62,7 @@ interactions:
User-Agent:
- python-requests/2.23.0
method: GET
- uri: http://localhost:3000/manifest
+ uri: http://otherapp:3000/manifest
response:
body:
string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"aboutApp\"\
@@ -70,7 +70,7 @@ interactions:
: \"http://localhost:8888/app-data-privacy\",\n \"homepageUrl\": \"http://localhost:8888/homepage\"\
,\n \"supportUrl\": \"http://localhost:8888/support\",\n \"configurationUrl\"\
: \"htpp://localhost:8888/configuration\",\n \"appUrl\": \"http://localhost:8888/app\"\
- ,\n \"tokenTargetUrl\": \"http://localhost:3000/register\",\n \"id\": \"\
+ ,\n \"tokenTargetUrl\": \"http://otherapp:3000/register\",\n \"id\": \"\
app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
\n ]\n}"
headers:
@@ -119,7 +119,7 @@ interactions:
x-saleor-domain:
- mirumee.com
method: POST
- uri: http://localhost:3000/register
+ uri: http://otherapp:3000/register
response:
body:
string: "{\n \"auth_token\": \"mKWouu0pEUbGmQzohwWI78gRKAVRUP\",\n \"id\"\
@@ -144,7 +144,7 @@ interactions:
Expires:
- '-1'
Location:
- - http://localhost:3000/register/33
+ - http://otherapp:3000/register/33
Pragma:
- no-cache
Vary:
diff --git a/saleor/app/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml b/saleor/app/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml
index 0782bdc43d..de982cff58 100644
--- a/saleor/app/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml
+++ b/saleor/app/tests/cassettes/test_creates_app_from_manifest_activate_app.yaml
@@ -11,7 +11,7 @@ interactions:
User-Agent:
- python-requests/2.23.0
method: GET
- uri: http://localhost:3000/manifest
+ uri: http://otherapp:3000/manifest
response:
body:
string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"aboutApp\"\
@@ -19,7 +19,7 @@ interactions:
: \"http://localhost:8888/app-data-privacy\",\n \"homepageUrl\": \"http://localhost:8888/homepage\"\
,\n \"supportUrl\": \"http://localhost:8888/support\",\n \"configurationUrl\"\
: \"htpp://localhost:8888/configuration\",\n \"appUrl\": \"http://localhost:8888/app\"\
- ,\n \"tokenTargetUrl\": \"http://localhost:3000/register\",\n \"id\": \"\
+ ,\n \"tokenTargetUrl\": \"http://otherapp:3000/register\",\n \"id\": \"\
app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
\n ]\n}"
headers:
@@ -62,7 +62,7 @@ interactions:
User-Agent:
- python-requests/2.23.0
method: GET
- uri: http://localhost:3000/manifest
+ uri: http://otherapp:3000/manifest
response:
body:
string: "{\n \"name\": \"app\",\n \"version\": \"1.0.0\",\n \"aboutApp\"\
@@ -70,7 +70,7 @@ interactions:
: \"http://localhost:8888/app-data-privacy\",\n \"homepageUrl\": \"http://localhost:8888/homepage\"\
,\n \"supportUrl\": \"http://localhost:8888/support\",\n \"configurationUrl\"\
: \"htpp://localhost:8888/configuration\",\n \"appUrl\": \"http://localhost:8888/app\"\
- ,\n \"tokenTargetUrl\": \"http://localhost:3000/register\",\n \"id\": \"\
+ ,\n \"tokenTargetUrl\": \"http://otherapp:3000/register\",\n \"id\": \"\
app2\",\n \"permissions\": [\n \"MANAGE_USERS\",\n \"MANAGE_ORDERS\"\
\n ]\n}"
headers:
@@ -119,7 +119,7 @@ interactions:
x-saleor-domain:
- mirumee.com
method: POST
- uri: http://localhost:3000/register
+ uri: http://otherapp:3000/register
response:
body:
string: "{\n \"auth_token\": \"23tlfEQfMSrESRqUpbCTMy8ay0EwJg\",\n \"id\"\
@@ -144,7 +144,7 @@ interactions:
Expires:
- '-1'
Location:
- - http://localhost:3000/register/34
+ - http://otherapp:3000/register/34
Pragma:
- no-cache
Vary:
diff --git a/saleor/app/tests/test_app_commands.py b/saleor/app/tests/test_app_commands.py
index 1be3344595..28652ab18b 100644
--- a/saleor/app/tests/test_app_commands.py
+++ b/saleor/app/tests/test_app_commands.py
@@ -11,7 +11,7 @@ from ..models import App, AppInstallation
@pytest.mark.vcr
def test_creates_app_from_manifest():
- manifest_url = "http://localhost:3000/manifest"
+ manifest_url = "http://otherapp:3000/manifest"
call_command("install_app", manifest_url)
app = App.objects.get()
@@ -23,7 +23,7 @@ def test_creates_app_from_manifest():
@pytest.mark.vcr
def test_creates_app_from_manifest_activate_app():
- manifest_url = "http://localhost:3000/manifest"
+ manifest_url = "http://otherapp:3000/manifest"
call_command("install_app", manifest_url, activate=True)
app = App.objects.get()
diff --git a/saleor/app/tests/test_validators.py b/saleor/app/tests/test_validators.py
new file mode 100644
index 0000000000..b85614897f
--- /dev/null
+++ b/saleor/app/tests/test_validators.py
@@ -0,0 +1,17 @@
+import pytest
+from django.core.exceptions import ValidationError
+
+from ...app.validators import AppURLValidator
+
+
+def test_validate_url():
+ url_validator = AppURLValidator()
+ url = "http://otherapp:3000"
+ assert url_validator(url) is None
+
+
+def test_validate_invalid_url():
+ url_validator = AppURLValidator()
+ url = "otherapp:3000"
+ with pytest.raises(ValidationError):
+ url_validator(url)
diff --git a/saleor/app/validators.py b/saleor/app/validators.py
new file mode 100644
index 0000000000..7d5223e766
--- /dev/null
+++ b/saleor/app/validators.py
@@ -0,0 +1,18 @@
+import re
+
+from django.core.validators import URLValidator
+from django.utils.regex_helper import _lazy_re_compile # type: ignore
+
+
+class AppURLValidator(URLValidator):
+ validator = URLValidator
+ host_re = "(" + validator.hostname_re + validator.domain_re + "|localhost)"
+ regex = _lazy_re_compile(
+ r"^(?:[a-z0-9.+-]*)://" # scheme is validated separately
+ r"(?:[^\s:@/]+(?::[^\s:@/]*)?@)?" # user:pass authentication
+ r"(?:" + validator.ipv4_re + "|" + validator.ipv6_re + "|" + host_re + ")"
+ r"(?::\d{2,5})?" # port
+ r"(?:[/?#][^\s]*)?" # resource path
+ r"\Z",
+ re.IGNORECASE,
+ )
diff --git a/saleor/graphql/app/mutations.py b/saleor/graphql/app/mutations.py
index 7d002552ee..2778f379e9 100644
--- a/saleor/graphql/app/mutations.py
+++ b/saleor/graphql/app/mutations.py
@@ -3,11 +3,11 @@ from typing import List
import graphene
import requests
from django.core.exceptions import ValidationError
-from django.core.validators import URLValidator
from ...app import models
from ...app.error_codes import AppErrorCode
from ...app.tasks import install_app_task
+from ...app.validators import AppURLValidator
from ...core import JobStatus
from ...core.permissions import (
AppPermission,
@@ -376,7 +376,7 @@ class AppInstall(ModelMutation):
@classmethod
def clean_manifest_url(self, url):
- url_validator = URLValidator()
+ url_validator = AppURLValidator()
try:
url_validator(url)
except (ValidationError, AttributeError):
@@ -443,7 +443,7 @@ class AppFetchManifest(BaseMutation):
@classmethod
def clean_manifest_url(cls, manifest_url):
- url_validator = URLValidator()
+ url_validator = AppURLValidator()
try:
url_validator(manifest_url)
except (ValidationError, AttributeError):
diff --git a/saleor/webhook/models.py b/saleor/webhook/models.py
index 7d76dd6d1e..b328442cef 100644
--- a/saleor/webhook/models.py
+++ b/saleor/webhook/models.py
@@ -1,12 +1,12 @@
-from django.core import validators
from django.db import models
from ..app.models import App
+from ..app.validators import AppURLValidator
class WebhookURLField(models.URLField):
default_validators = [
- validators.URLValidator(schemes=["http", "https", "awssqs", "gcpubsub"])
+ AppURLValidator(schemes=["http", "https", "awssqs", "gcpubsub"])
]