putyourlightson / craft-campaign

Send and manage email campaigns, contacts and mailing lists in Craft CMS.
https://putyourlightson.com/plugins/campaign
Other
63 stars 25 forks source link

SQLSTATE[22001]: String data, right truncated #473

Closed samuelreichor closed 6 months ago

samuelreichor commented 6 months ago

Bug Report

We had a sql error when we wanted to add a person to a mailing list because of long UTM parameters in the url. The url from which the action was called looked like this: https://www.karriere.at/lp/gewinnspiel-profil-check?utm_source=newsletter&utm_medium=email&utm_campaign=activesourcing_gewinnspiel_llcheck&utm_content=cta&utm_source=emarsys&utm_medium=email&utm_campaign=B2C+Active+Sourcing+April+2024+-+Lottery+%26+Tips&&sc_src=email_13853398&sc_lid=858156625&sc_uid=wx849sJloR&sc_llid=1484&sc_eh=12e5956964efdaed1

When the form on this url was submitted, a 500 error occurred and we found this error in the logs:

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'source' at row 1

To test this, we truncated the get parameters as follows: https://www.karriere.at/lp/gewinnspiel-profil-check?utm_source=newsletter&utm_medium=email&utm_campaign=activesourcing_gewinnspiel_llcheck&utm_content=cta

This worked without any problems.

This is how adding the email to the mailing list is implemented:

  1. This hidden input field is in the form: <input type="hidden" name="action" value="campaign/forms/subscribe" />

  2. When the form gets submitted, it gets validated and this Twig file is called via ajax to determine if this email is already in the email list.

{% set mailingListSlug = craft.app.request.get('mailingList') %}
{% set email = craft.app.request.get('email') %}

{% set contact = craft.campaign.contacts.email(email).one() %}
{% set mailingLists = contact.mailingLists %}

{% set found = false %}
{% for mailingList in mailingLists %}
  {% if mailingList.slug == mailingListSlug %}
    {% set found = true %}
  {% endif %}
{% endfor %}

{{ {
  data: found ? contact : null
} | json_encode | raw }}
  1. After everything has been successfully completed, the contact gets added to the mailing list.

Our quick fix was a simple nginx redirect to strip down the get params, but hopefully there is a long term solution for that. If you need more information, feel free to ask. :)

Plugin Version

2.9.2

Craft CMS Version

4.5.11.1

PHP Version

8.2

bencroker commented 6 months ago

Thanks for reporting this. Made sure the URL is truncated to the allowed length in https://github.com/putyourlightson/craft-campaign/commit/972426aeec72aaeccb751b41bd56add5b4d02d3c and released in 2.15.3.