Browse Source

fix(issues): Make system URL prefix handling more consistent (#40968)

fixing up PR [here](https://github.com/getsentry/sentry/pull/39597) so
that tests pass
this makes system url prefix handling more consistent by using the sentry.http.utils.absolute_uri function

Co-authored-by: Ville Laitila <ville.laitila@softagram.com>
Hubert Deng 2 years ago
parent
commit
4890581d67

+ 3 - 4
src/sentry/api/endpoints/chunk.py

@@ -2,7 +2,6 @@ import logging
 import re
 from gzip import GzipFile
 from io import BytesIO
-from urllib.parse import urljoin
 
 from django.conf import settings
 from django.urls import reverse
@@ -16,6 +15,7 @@ from sentry.api.bases.organization import OrganizationEndpoint, OrganizationRele
 from sentry.models import FileBlob
 from sentry.ratelimits.config import RateLimitConfig
 from sentry.utils.files import get_max_file_size
+from sentry.utils.http import absolute_uri
 
 MAX_CHUNKS_PER_REQUEST = 64
 MAX_REQUEST_SIZE = 32 * 1024 * 1024
@@ -74,11 +74,10 @@ class ChunkUploadEndpoint(OrganizationEndpoint):
                 url = relative_url.lstrip(API_PREFIX)
             # Otherwise, if we do not support them, return an absolute, versioned endpoint with a default, system-wide prefix
             else:
-                endpoint = options.get("system.url-prefix")
-                url = urljoin(endpoint.rstrip("/") + "/", relative_url.lstrip("/"))
+                url = absolute_uri(relative_url)
         else:
             # If user overridden upload url prefix, we want an absolute, versioned endpoint, with user-configured prefix
-            url = urljoin(endpoint.rstrip("/") + "/", relative_url.lstrip("/"))
+            url = absolute_uri(relative_url, endpoint)
 
         return Response(
             {

+ 2 - 4
src/sentry/middleware/customer_domain.py

@@ -9,11 +9,11 @@ from django.urls import resolve, reverse
 from rest_framework.request import Request
 from rest_framework.response import Response
 
-from sentry import options
 from sentry.api.base import resolve_region
 from sentry.api.utils import generate_organization_url
 from sentry.models import Organization
 from sentry.utils import auth
+from sentry.utils.http import absolute_uri
 
 
 def _org_exists(slug):
@@ -91,9 +91,7 @@ class CustomerDomainMiddleware:
             # DISALLOWED_CUSTOMER_DOMAINS is a list of org slugs that are explicitly not allowed to use customer domains.
             # We kick any request to the logout view.
             logout(request)
-            url_prefix = options.get("system.url-prefix")
-            logout_view = reverse("sentry-logout")
-            redirect_url = f"{url_prefix}{logout_view}"
+            redirect_url = absolute_uri(reverse("sentry-logout"))
             return HttpResponseRedirect(redirect_url)
 
         activeorg = _resolve_activeorg(request)

+ 2 - 3
src/sentry/models/authprovider.py

@@ -4,7 +4,6 @@ from django.db import models
 from django.utils import timezone
 
 from bitfield import BitField
-from sentry import options
 from sentry.db.models import (
     BoundedPositiveIntegerField,
     FlexibleForeignKey,
@@ -13,6 +12,7 @@ from sentry.db.models import (
     sane_repr,
 )
 from sentry.db.models.fields.jsonfield import JSONField
+from sentry.utils.http import absolute_uri
 
 logger = logging.getLogger("sentry.authprovider")
 
@@ -99,9 +99,8 @@ class AuthProvider(Model):
 
     def get_scim_url(self):
         if self.flags.scim_enabled:
-            url_prefix = options.get("system.url-prefix")
             # the SCIM protocol doesn't use trailing slashes in URLs
-            return f"{url_prefix}/api/0/organizations/{self.organization.slug}/scim/v2"
+            return absolute_uri(f"api/0/organizations/{self.organization.slug}/scim/v2")
 
         else:
             return None

+ 2 - 2
src/sentry/templatetags/sentry_avatars.py

@@ -4,9 +4,9 @@ from django import template
 from django.urls import reverse
 from django.utils.safestring import mark_safe
 
-from sentry import options
 from sentry.models import User, UserAvatar
 from sentry.utils.avatar import get_email_avatar, get_gravatar_url, get_letter_avatar
+from sentry.utils.http import absolute_uri
 
 register = template.Library()
 
@@ -33,7 +33,7 @@ def profile_photo_url(context, user_id, size=None):
     url = reverse("sentry-user-avatar-url", args=[avatar.ident])
     if size:
         url += "?" + urlencode({"s": size})
-    return options.get("system.url-prefix") + url
+    return absolute_uri(url)
 
 
 # Don't use this in any situations where you're rendering more

+ 2 - 3
src/sentry/web/frontend/base.py

@@ -15,7 +15,6 @@ from django.views.generic import View
 from rest_framework.request import Request
 from rest_framework.response import Response
 
-from sentry import options
 from sentry.api.serializers import serialize
 from sentry.api.utils import is_member_disabled_from_limit
 from sentry.auth import access
@@ -26,6 +25,7 @@ from sentry.silo import SiloMode
 from sentry.utils import auth
 from sentry.utils.audit import create_audit_entry
 from sentry.utils.auth import is_valid_redirect, make_login_link_with_redirect
+from sentry.utils.http import absolute_uri
 from sentry.web.frontend.generic import FOREVER_CACHE
 from sentry.web.helpers import render_to_response
 from sudo.views import redirect_to_sudo
@@ -178,8 +178,7 @@ class OrganizationMixin:
         else:
             url = "/organizations/new/"
             if request.subdomain:
-                base = options.get("system.url-prefix")
-                url = f"{base}{url}"
+                url = absolute_uri(url)
         return HttpResponseRedirect(url)