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

Validate goal pattern and type in API #12788

Merged
merged 4 commits into from May 3, 2018
Merged

Validate goal pattern and type in API #12788

merged 4 commits into from May 3, 2018

Conversation

sgiehl
Copy link
Member

@sgiehl sgiehl commented Apr 29, 2018

Goal pattern type and pattern regexes will now be validated in API when creating or updating goals.

refs #12683

@sgiehl sgiehl added the Needs Review PRs that need a code review label Apr 29, 2018
@sgiehl sgiehl added this to the 3.5.0 milestone Apr 29, 2018
@@ -218,6 +220,10 @@ private function checkPatternIsValid($patternType, $pattern, $matchAttribute)
) {
throw new Exception(Piwik::translate('Goals_ExceptionInvalidMatchingString', array("http:// or https://", "http://www.yourwebsite.com/newsletter/subscribed.html")));
}

if ($patternType == 'regex' && @preg_match($pattern, '') === false) {
Copy link
Member

Choose a reason for hiding this comment

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

I tested it and it failed for me for rere(.*). I think this is because the / fail. Ideally we would re-use same logic as here: https://github.com/matomo-org/matomo/blob/3.4.0/core/Tracker/GoalManager.php#L826-L836
to ensure it will work or not work during tracking.

image

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I've changed it :)

@tsteur
Copy link
Member

tsteur commented Apr 30, 2018

Looks good and worked for me locally. Few tests are failing though such as https://travis-ci.org/matomo-org/matomo/jobs/372795044

@sgiehl
Copy link
Member Author

sgiehl commented Apr 30, 2018

Ah. Empty pattern types need to be allowed

@diosmosis
Copy link
Member

Would it be a good idea to make these validations https://github.com/matomo-org/matomo/blob/3.x-dev/core/Validators/BaseValidator.php instances?

@sgiehl
Copy link
Member Author

sgiehl commented Apr 30, 2018

Don't have a strong opinion on that, but not sure if it's something that should / needs to be reusible

@diosmosis
Copy link
Member

I'm thinking "is valid regex" could be reusable and "is one of values" could also be reusable (though this might already exist? not sure).

@tsteur
Copy link
Member

tsteur commented Apr 30, 2018

I think in this code might not be needed as it really depends on the way the preg_match is executed later see this PR. If a developer doesn't surround the regex in / but for example a tilde, then the validation might not 100% work or if the developer doesn't do the str_replace('/', '\/') etc

@sgiehl
Copy link
Member Author

sgiehl commented Apr 30, 2018

I've done some changes to use validator classes instead of directly checking it in API.
@tsteur @diosmosis if it was better the way before, let me know and I'll remove the commit again ;-)

@diosmosis
Copy link
Member

If a developer doesn't surround the regex in / but for example a tilde, then the validation might not 100% work or if the developer doesn't do the str_replace('/', '/') etc

This functionality could always be added when the need comes up (ie, a developer who uses the validation w/ a different delimiter will eventually notice the issue). But having the place to put this functionality (ie, a validator class) makes it possible. Otherwise these checks could get lost when someone eventually does add a regex validator and the same logic will be replicated in different places. That said, it's just validation, not a life or death issue. It's not like not using a validator will make matomo collapse with complexity :)

@sgiehl
Copy link
Member Author

sgiehl commented May 3, 2018

Anything left here, or is that ready to merge?

Copy link
Member

@diosmosis diosmosis left a comment

Choose a reason for hiding this comment

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

LGTM

@sgiehl sgiehl merged commit 28d5fc9 into 3.x-dev May 3, 2018
@sgiehl sgiehl deleted the validategoals branch May 3, 2018 20:17
InfinityVoid pushed a commit to InfinityVoid/matomo that referenced this pull request Oct 11, 2018
* Validate goal pattern and type

* allow empty pattern type

* Use validator classes to validate API params

* tweak error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants