Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loosen the validation on setting country in payment propertyBag to handle buggy code #24903

Closed
wants to merge 1 commit into from

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Nov 4, 2022

Overview

See https://lab.civicrm.org/dev/core/-/issues/3918

Before

Crash if bad country data passed in.

After

Logged in bad country data passed in.

Technical Details

Comments

Alternative to #24894

@civibot
Copy link

civibot bot commented Nov 4, 2022

(Standard links)

@eileenmcnaughton
Copy link
Contributor

@mattwire is this a good idea? It affects setBillingCountry which was OK - whereas we just want setCountry to be more lenient

@eileenmcnaughton
Copy link
Contributor

The good thing is the unit test fail flags where to add a new unit test :-)

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Nov 5, 2022

I guess the flip side to my thoughts about it making setBillingCountry more lenient is that in general when I tried to use PropertyBag I found it was probably a bit too strict to be easy to use (mostly cos of the handling of empty/null) - so maybe logging is enough

Copy link
Contributor

@artfulrobot artfulrobot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(SCRAP THIS)

I think logging this stuff is useful to search out places where the data is being incorrectly supplied, however this passes the problem on down the line - it means we accept whatever junk passed in as billingCountry, so we will output the same junk too, which likely leads to multiple implementations of fixes for this problem by the consumers of the propertybag.

Would it be too much to, as Eileen suggests do

if is_string and isISO3166ok: accept

elseif !empty && (is_int || preg_match('/^\d+$/')) && idMatchesCountryId: lookup and map to the ISO3166 code. Log warning

elseif !empty && is_string && matches country name: lookup and map to the ISO3166 code. Log warning.

else store NULL (as what we have cannot be valid) and log warning

EDIT: I've put my preferred aproach at #24927

@@ -647,10 +647,12 @@ public function getBillingCountry($label = 'default') {
*/
public function setBillingCountry($input, $label = 'default') {
if (!is_string($input) || strlen($input) !== 2) {
throw new \InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
\Civi::log()->error('setBillingCountry expects ISO 3166-1 alpha-2 country code. Attempted to use "' . $input . '"');

This comment was marked as outdated.

}
if (!CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'country_id', $input)) {
throw new \InvalidArgumentException("setBillingCountry expects ISO 3166-1 alpha-2 country code.");
\Civi::log()->error('setBillingCountry expects ISO 3166-1 alpha-2 country code. "' . $input . '" not found in database.');

This comment was marked as outdated.

@mattwire
Copy link
Contributor Author

mattwire commented Nov 8, 2022

Closing in favour of #24927

@mattwire mattwire closed this Nov 8, 2022
@mattwire mattwire deleted the issue3918 branch November 8, 2022 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants