I have been stuck on binascii.Error: Incorrect padding, not sure if it is an error

I am trying to publish to sns topic but have been hit by this error multiple times.
This happen when i have already subscribe and I use FilterPolicy.

There seems to be an error when create_sqs_message_attributes, if there is FilterPolicy

Subscription Attribute

{
    "Attributes": {
        "TopicArn": "arn:aws:sns:ap-southeast-1:000000000000:topups_local-dev-topic",
        "Endpoint": "arn:aws:sqs:ap-southeast-1:000000000000:topups_local-dev_create_transaction",
        "Protocol": "sqs",
        "SubscriptionArn": "arn:aws:sns:ap-southeast-1:000000000000:topups_local-dev-topic:fccf4f00-3d2a-4385-b6d4-4bc91e2dbcd1",
        "PendingConfirmation": "false",
        "Owner": "000000000000",
        "RawMessageDelivery": "True",
        "FilterPolicy": "{\"x-xsh-action\":[\"create_transaction\"]}",
        "ConfirmationWasAuthenticated": "true",
        "FilterPolicyScope": "MessageAttributes"
    }
}

What is being publish

  {
    TopicArn: 'arn:aws:sns:ap-southeast-1:000000000000:topups_local-dev-topic',
    Message: '{"id":"123"}',
    MessageAttributes: {
      'x-xsh-action': { DataType: 'String', StringValue: 'create_transaction' }
    }
  }

The Error

2023-04-14T06:15:55.136 ERROR --- [   sns_pub_0] l.services.sns.publisher   : An internal error occurred while trying to format the message for SQS

Traceback (most recent call last):

  File "/opt/code/localstack/localstack/services/sns/publisher.py", line 251, in _publish

    sqs_message_attrs = self.create_sqs_message_attributes(

  File "/opt/code/localstack/localstack/services/sns/publisher.py", line 311, in create_sqs_message_attributes

    attribute["BinaryValue"] = base64.b64decode(to_bytes(val))

  File "/usr/local/lib/python3.10/base64.py", line 87, in b64decode

    return binascii.a2b_base64(s)

binascii.Error: Incorrect padding

Hi @ivansim and thanks for your report!

I am not sure the FilterPolicy matters in this issue, did you try without it?

There might be an issue with BinaryAttributes, however, in your sample, you are publishing a DataType=String and StringValue=create_transation, which means LocalStack should not try to decode the StringValue as binary?
Would it be possible to start LocalStack with DEBUG=1 and LS_LOG=trace to have more verbose logs, and share the logs just before the exception happens? (Around 5 lines above, something that would start with:

2023-04-13T03:42:11.783  INFO --- [   asgi_gw_3] localstack.request.aws     : AWS sns.Publish => 200; PublishInput({'TopicArn': 'arn:aws:sns:us-east-1:000000000000:test-topic-2b287423', 'Message': 'test_external_http_endpoint'} [...]

Thank you!