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

No error thrown when accessing private and protected members of Kirby\Cms\App in a plugin #2394

Closed
hdodov opened this issue Jan 14, 2020 · 8 comments · Fixed by #2563
Closed
Assignees
Labels
type: bug 🐛 Is a bug; fixes a bug
Milestone

Comments

@hdodov
Copy link
Contributor

hdodov commented Jan 14, 2020

Describe the bug
In a plugin, you can access kirby()->roots and no error will be thrown. We had a discussion about that with @distantnative here.

To Reproduce
Steps to reproduce the behavior:

  1. Clone the plainkit
  2. Create a plugin
  3. Put var_dump(kirby()->roots) in the plugin index.php file
  4. No error thrown

Expected behavior
An error should be thrown.

Kirby Version
3.3.2

Additional context
If you do var_dump(kirby()->roots) in a template, you correctly get the error:

Cannot access protected property Kirby\Cms\App::$roots

However, doing the same thing in a plugin works, and it shouldn't. @distantnative mentioned this might be due to the way hooks are triggered:

$function->call($this, ...$arguments);

@lukasbestle
Copy link
Member

It's because PHP's include() functions (including include_once(), require() and require_once()) import the included file into the current variable scope.

Plugins are loaded from the App class directly, which is why plugins have access to protected methods of the App class.

We could fix this by moving the pluginsLoader() method to a different class or by making it a PHP function, but I wonder if this is worth it. Please note that making the method static will not fix this as even static methods will have access to protected instance methods of the same class. So we really need to move it outside of the App class.

@lukasbestle lukasbestle added needs: discussion 🗣 Requires further discussion to proceed priority: low 🐌 type: bug 🐛 Is a bug; fixes a bug labels Jan 18, 2020
@hdodov
Copy link
Contributor Author

hdodov commented Jan 19, 2020

I think the question then is - should plugins be able to access private and protected members of App? If I build a plugin that relies on the extendTranslations() method, for example, will this plugin work as expected in future Kirby releases?

@lukasbestle
Copy link
Member

No, they should not rely on that access. It's a bug that you can access protected methods and properties, but the question is whether it matters: Plugin devs shouldn't use the protected methods for their plugins.

@hdodov
Copy link
Contributor Author

hdodov commented Jan 19, 2020

@lukasbestle devs shouldn't use protected members, that's right. But it should be enforced. If VSCode suggests the extendTranslations() method and I use it, there's no way to know I shouldn't do that - my plugin would work. So even though plugin devs should not use protected members, they can accidentally do it unless they open up the source code. Or even - look at the source code, but not pay attention to the visibility of the member, like I did. I used the method in my plugin, it worked perfectly, and I thought it was ready for release. Then, @distantnative pointed out (in the forum) that this shouldn't have worked.

So yes, I think it does matter. If you have access to something you shouldn't have access to, a fatal error should be thrown.

@lukasbestle
Copy link
Member

That's why this issue is currently "missing: discussion 🗣". Waiting for input from @bastianallgeier and @distantnative.

@lukasbestle
Copy link
Member

lukasbestle commented Jan 19, 2020

BTW: Same issue for:

Maybe we should make a new global helper to load PHP files (basically a helper that runs require_once or require depending on the use-case and returns the result) that can then safely be called from everywhere else.

Edit: We already have that: F::load(). As that method is in a class with no protected members, it should be safe to call that one from everywhere else.

@bastianallgeier
Copy link
Member

I agree that we should protect the App class from access via plugins. F::load does a few things that we don't really need here and I would probably prefer to have this as close to a native include as possible.

@lukasbestle lukasbestle removed the needs: discussion 🗣 Requires further discussion to proceed label Apr 5, 2020
@lukasbestle lukasbestle self-assigned this Apr 5, 2020
lukasbestle added a commit that referenced this issue Apr 5, 2020
This ensures that the included file can neither access any local variables from the calling methods nor protected methods from the calling classes.

See https://www.php.net/manual/en/function.include.php#example-124.
lukasbestle added a commit that referenced this issue May 6, 2020
lukasbestle added a commit that referenced this issue May 6, 2020
This ensures that the included file can neither access any local variables from the calling methods nor protected methods from the calling classes.

See https://www.php.net/manual/en/function.include.php#example-124.
bastianallgeier pushed a commit that referenced this issue May 8, 2020
bastianallgeier pushed a commit that referenced this issue May 8, 2020
This ensures that the included file can neither access any local variables from the calling methods nor protected methods from the calling classes.

See https://www.php.net/manual/en/function.include.php#example-124.
@bastianallgeier
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Is a bug; fixes a bug
Projects
None yet
3 participants