Skip to content

[Form] UrlType should not add protocol to emails #43707

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

Closed
wants to merge 3 commits into from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Oct 25, 2021

Q A
Branch? 6.1
Bug fix? yes
New feature? no
Deprecations? yes
Tickets Fix #43506
License MIT
Doc PR -

UrlType fix URLs by adding the protocol (http://) to values that does not contains one. This is clever until users type their email in the field. Values like contact@example.com get converted to http://contact@example.com which is a valid URL.

Fixing this behavior being a breaking change, this PR adds the option default_protocol_skip_email that needs to be set to true to disable fix on email-like values.

In 6.0, the new option will be deprecated and email-like values will always be ignored.

URLs that are still fixed:

  • Contains user & passord: user:pass@example.com
  • Contains a path: user@example.com/path

@GromNaN GromNaN force-pushed the 5.4-form-url-prepend branch from 5e23cee to 320c757 Compare October 25, 2021 23:01
@carsonbot
Copy link

Hey!

I think @fre5h has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@ThomasLandauer
Copy link
Contributor

Trying to detect what probably is an email address and what not, is a good idea in principle. But it will never work 100%, since e.g. "user:pass"@example.com is a valid email address - see RFC 5322, and their weird examples in the Appendix

So I would say: UrlType is essentially just a nice UX feature for "lazy" end-users ;-) So I would only prepend the default_protocol if it's very certain that the input is an URL - i.e. no @. If somebody does need to enter some special URL, they need to type the protocol themselves ;-)
This is also easier to document (i.e. explain) than having to tell people about exotic email address syntaxes they've never seen before...

public function onSubmit(FormEvent $event)
{
$data = $event->getData();

if ($this->defaultProtocol && $data && \is_string($data) && !preg_match('~^[\w+.-]+://~', $data)) {
if (preg_match('~^[^:/]+@[A-Za-z0-9-.]+\.[A-Za-z0-9]+$~', $data)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we could be even more aggressive and simply skip adding the default protocol if there is an userinfo part in the URL. Since entering an URL with userinfo is quite rare, forcing to specify the protocol in this case would be a big decrease in user experience.

Copy link
Contributor

Choose a reason for hiding this comment

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

You mean "would not be a big decrease in user experience"

Copy link
Member

@nicolas-grekas nicolas-grekas Oct 28, 2021

Choose a reason for hiding this comment

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

~^[^:/?@]++@~ would fit

Copy link
Member Author

Choose a reason for hiding this comment

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

Could be ~^([^:/?@]++@|[^./]+$)~ to exclude values like no, that would be converted to a valid url.

/**
* @param bool $skipEmail the URL scheme is not added to values that match an email pattern
*/
public function skipEmail(): void
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to replace this setter by a constructor argument

@nicolas-grekas
Copy link
Member

I understand why this has been proposed, but I don't like adding a new required option to the constraint. It's going to make all this super verbose and noisy.

Instead, I suggest that ppl stop entering emails in a field that asks for a URL :trollface:

Alternatively, what about just throw the deprecation when this happens, aka without the option?

@nicolas-grekas
Copy link
Member

Oh, and ideally we'd switch to https by default in v6!

@GromNaN
Copy link
Member Author

GromNaN commented Oct 28, 2021

I agree that the new option is a very preventive way to change the behaviour, while providing an optin for this fix asap. Without this option, is it better to change the behaviour in 5.4 or 6.0 ?
Or should it be considered as a bugfix and released in 4.4 or 5.3 ?

@GromNaN GromNaN force-pushed the 5.4-form-url-prepend branch from b3c58ec to 0501d56 Compare October 28, 2021 21:01
@fabpot fabpot modified the milestones: 5.4, 6.1 Oct 29, 2021
@fabpot
Copy link
Member

fabpot commented Oct 29, 2021

That's for 6.1.

@stof
Copy link
Member

stof commented Oct 29, 2021

@nicolas-grekas if we want to change the UrlType, I'd rather change the default_protocol to null by default instead of changing it to https, which will then render a <input type=url> rather than <input type=text>.

In my own projects, I added a FormTypeExtension to change the default value of the option.

@GromNaN GromNaN closed this Jan 12, 2022
nicolas-grekas added a commit that referenced this pull request Jan 26, 2022
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Form] UrlType should not add protocol to emails

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #43506
| License       | MIT
| Doc PR        | n/a

Replace #43707, since there is a bug in the `UrlType` auto-prepend behavior.

The regex was suggested by `@nicolas`-grekas #43707 (comment)

Commits
-------

4f99449 [Form] UrlType should not add protocol to emails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Validator] Assert\Url Allow forbidding the @ character
7 participants