Skip to content

Fix ModelInstance RegEx pattern for class names. #11650

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

Conversation

MartinPeverelli
Copy link
Contributor

The updated XSD files forbid the usage of digits in the class or namespaces for the modelInstance value.

Description

Changes '[A-Za-z_\\]+' for '[A-Za-z0-9_\\]+' to support namespaces with digits.

Fixed Issues (if relevant)

  1. import.xsd doesn't allow numbers in vendor name and class name #4470
  2. Exception error on Module name when it has number(s) on product_options.xml - Regex issue #9985

Manual testing scenarios

Create a module with a vendor name containing a digit. The following XML should be valid.

<!-- app/code/Interactiv4/TestProduct/etc/product_types.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
    <type name="test" label="Test Product" modelInstance="Interactiv4\TestProduct\Model\Product\Type\Course" />
</config>

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

Changes '[A-Za-z_\\]+' for '[A-Za-z0-9_\\]+' to support namespaces with numbers.
@orlangur orlangur self-assigned this Oct 23, 2017
@orlangur
Copy link
Contributor

To be processed together with #11062 as we agreed.

@orlangur orlangur assigned vrann and unassigned orlangur Oct 24, 2017
@adrian-martinez-interactiv4
Copy link
Contributor

adrian-martinez-interactiv4 commented Oct 30, 2017

@orlangur @MartinPeverelli This PR and #11062 propose using [a-zA-Z0-9_\\]+ expression, but it may not be valid, since it allows expression starting with a number like 4nteractiv4\CourseProduct\Model\Product\Type\Course, as can be seen in the following link, https://regex101.com/r/7WTFSy/1, and here :
captura de pantalla 2017-10-30 a las 23 25 23

Starting numbers weren't allowed before, and I think opening xsd that way would result in a regression. Also, current expression allows expressions starting with underscores like ___Interactiv4\CourseProduct\Model\Product\Type\Course, what I think shouldn't be allowed.

I'd propose one of the following expressions:

  • [A-Z][a-zA-Z0-9_\\]* if expected model name must start with a capital letter
  • [A-Z\\][a-zA-Z0-9_\\]* if expected model name must start with a capital letter or backslash
  • [a-zA-Z\\][a-zA-Z0-9_\\]* if expected model name must start with a letter (capital or not) or backslash

What do you think?

@magento-engcom-team magento-engcom-team added 2.2.x bugfix Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Component: Setup labels Nov 7, 2017
@vkublytskyi
Copy link

@adrian-martinez-interactiv4 I do agree with you that regexp from PR is too wide and allows invalid strings.

As Model name potentially could be any valid PHP class name (FQCN to be precise) and according to PHP documentation full regexp should looks like:

(?:\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+

@magento-engcom-team magento-engcom-team added Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line labels Nov 28, 2017
@orlangur
Copy link
Contributor

@vkublytskyi http://php.net/manual/en/language.variables.basics.php has nothing to do with class names, it's about variables, http://php.net/manual/en/language.oop5.basic.php fits better.

Looks like you added backslash by your own while referring to unrelated documentation page :D Leading digit is still allowed: https://regex101.com/r/U0VCMk/1 (don't forget about ^...$). Note that leading backslash MUST NOT be allowed. No reason to allow class names violating PSR-2 => probably leading lowercase letter is not needed to be allowed?

Another place with FQCN regexp: https://github.com/magento/magento2/pull/11765/files It is not so bad as leading backslash or digit is not allowed, characters allowed are not so wide as in mentioned regexp though.

(?:\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+

-> (just what PHP allows)

^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$

-> (just what seems reasonable for us)

^[A-Z][a-zA-Z0-9_]*(?:\\[A-Z][a-zA-Z0-9_]*)*$

Let's pick the best variant in Slack and fix all places.

@vkublytskyi
Copy link

vkublytskyi commented Nov 29, 2017

@orlangur RegExp from PHP site describes any names in PHP ("the same rules as other labels in PHP") which could be: variables, class names, function names, method names, property names, constants. I've added backslash to apply this pattern to FQCN as FQCN is a sequence of names joined by a backslash.

Also, not allowing leading backslash in Magento xml files (e.g. layouts, di, etc.) is ok. But blocks import process if someone specifies a valid model name with leading backslash is too hard and not necessary limitation.

Of course, any regexp that we choose should be wrapped with ^$

@orlangur orlangur assigned orlangur and unassigned vrann Nov 30, 2017
@okorshenko okorshenko removed the 2.2.x label Dec 14, 2017
@ihor-sviziev
Copy link
Contributor

@orlangur any updates?

@slavvka
Copy link
Member

slavvka commented Oct 5, 2018

Closed due to inactivity. Feel free to re-open it if you wish to continue working on it.

@slavvka slavvka closed this Oct 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Component: Setup Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Release Line: 2.2 Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants