Skip to content

Commit

Permalink
fix(integrations): phabricator host validation (#64882)
Browse files Browse the repository at this point in the history
  • Loading branch information
oioki committed Feb 8, 2024
1 parent d7ef6ef commit 6013463
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/sentry_plugins/phabricator/plugin.py
Expand Up @@ -8,6 +8,7 @@

from sentry.exceptions import PluginError
from sentry.integrations import FeatureDescription, IntegrationFeatures
from sentry.net.socket import is_valid_url
from sentry.plugins.bases.issue2 import IssueGroupActionEndpoint, IssuePlugin2
from sentry.utils import json
from sentry.utils.http import absolute_uri
Expand All @@ -33,6 +34,12 @@ def query_to_result(field, result):
return result["fields"]["name"]


def validate_host(value: str, **kwargs: object) -> str:
if not value.startswith(("http://", "https://")) or not is_valid_url(value):
raise PluginError("Not a valid URL.")
return value


class PhabricatorPlugin(CorePluginMixin, IssuePlugin2):
description = DESCRIPTION

Expand Down Expand Up @@ -78,6 +85,7 @@ def get_configure_plugin_fields(self, request: Request, project, **kwargs):
"type": "text",
"placeholder": "e.g. http://secure.phabricator.org",
"required": True,
"validators": [validate_host],
},
secret_field,
{
Expand Down
14 changes: 14 additions & 0 deletions tests/sentry_plugins/phabricator/test_plugin.py
Expand Up @@ -2,8 +2,11 @@

import responses
from django.test import RequestFactory
from pytest import raises

from sentry.exceptions import PluginError
from sentry.testutils.cases import PluginTestCase
from sentry.testutils.helpers import override_blocklist
from sentry_plugins.phabricator.plugin import PhabricatorPlugin


Expand Down Expand Up @@ -43,3 +46,14 @@ def test_is_configured(self):
assert self.plugin.is_configured(None, self.project) is False
self.plugin.set_option("certificate", "a-certificate", self.project)
assert self.plugin.is_configured(None, self.project) is True

@override_blocklist("127.0.0.1")
def test_invalid_url(self):
with raises(PluginError):
self.plugin.validate_config_field(
project=self.project, name="host", value="ftp://example.com"
)
with raises(PluginError):
self.plugin.validate_config_field(
project=self.project, name="host", value="http://127.0.0.1"
)

0 comments on commit 6013463

Please sign in to comment.