SNS create_topic should only accept valid characters (#1329)

* SNS create_topic should only accept valid characters

Closes #1328

* Fix flake8 errors

* Updated regex to match full TopicName constraints

Fixed test_server as it contained invalid TopicNames’ per constraints

* fix error message for invalid topic name
This commit is contained in:
Adam Gilman 2017-11-13 18:27:11 +00:00 committed by Terry Cain
commit 2ad0f2fc1c
4 changed files with 38 additions and 7 deletions

View file

@ -32,3 +32,11 @@ class SNSInvalidParameter(RESTError):
def __init__(self, message):
super(SNSInvalidParameter, self).__init__(
"InvalidParameter", message)
class InvalidParameterValue(RESTError):
code = 400
def __init__(self, message):
super(InvalidParameterValue, self).__init__(
"InvalidParameterValue", message)

View file

@ -7,6 +7,7 @@ import json
import boto.sns
import requests
import six
import re
from moto.compat import OrderedDict
from moto.core import BaseBackend, BaseModel
@ -15,7 +16,8 @@ from moto.sqs import sqs_backends
from moto.awslambda import lambda_backends
from .exceptions import (
SNSNotFoundError, DuplicateSnsEndpointError, SnsEndpointDisabled, SNSInvalidParameter
SNSNotFoundError, DuplicateSnsEndpointError, SnsEndpointDisabled, SNSInvalidParameter,
InvalidParameterValue
)
from .utils import make_arn_for_topic, make_arn_for_subscription
@ -193,6 +195,9 @@ class SNSBackend(BaseBackend):
self.sms_attributes.update(attrs)
def create_topic(self, name):
fails_constraints = not re.match(r'^[a-zA-Z0-9](?:[A-Za-z0-9_-]{0,253}[a-zA-Z0-9])?$', name)
if fails_constraints:
raise InvalidParameterValue("Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long.")
candidate_topic = Topic(name, self)
if candidate_topic.arn in self.topics:
return self.topics[candidate_topic.arn]