-
Notifications
You must be signed in to change notification settings - Fork 2k
Honeypot Filter #1063
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
Honeypot Filter #1063
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start, but there's a few things that need to be changed up to fit with the rest of the framework. Thanks!
system/Honeypot/Honeypoter.php
Outdated
use CodeIgniter\HTTP\ResponseInterface; | ||
use Config\Honeypot; | ||
|
||
class Honeypoter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's rename this class/file to simply Honeypot
.
system/Honeypot/Honeypoter.php
Outdated
new self(): self::$selfObject; | ||
|
||
// TODO Will there be need to protect against bad data? | ||
if($request->getVar(self::$selfObject->name)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to our style guide the opening brackets should be on a line of their own.
application/Filters/Honeypot.php
Outdated
{ | ||
|
||
// Checks honeypot field if value was entered then show blank if so. | ||
if(Honeypoter::honeypotHasContent($request)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To maintain consistency with the rest of the framework, this should not be a class with static methods. Instead, if should be instantiated with methods called non-statically. Since you need to inject a config class as a dependency, you should add a honeypot
method to CodeIgniter\Services and use the service to get the instance. Like:
$honeypot = Services::honeypot();
if ($honeypot->hasContent($request) { ... }
system/Honeypot/Honeypoter.php
Outdated
self::$selfObject = (self::$selfObject === null) ? | ||
new self(): self::$selfObject; | ||
|
||
// TODO Will there be need to protect against bad data? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to protect the data since it's not be displayed or saved anywhere. Please remove the comment.
system/Honeypot/Honeypoter.php
Outdated
return true; | ||
} | ||
|
||
if($request->getGet(self::$selfObject->name)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check and the one from getPost are redundant. getVar
already checks both of those arrays.
system/Honeypot/Honeypoter.php
Outdated
*/ | ||
protected function getStyle(): string | ||
{ | ||
return '<script type="text/css" media="all"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a script tag - it should be a style tag. And I think this might be overkill, actually. While it's good, and thorough, the developer should I think keeping a default of hidden
in the config file is good enough. The developer can change as needed at that point. They'll notice the honeypot fields showing up during testing.
system/Honeypot/Honeypoter.php
Outdated
*/ | ||
protected function getDefaultTemplate(): string | ||
{ | ||
return '<div class="hidden" style="display:none"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the default values to the config file. Easier for the developer spot/change that way.
system/Honeypot/Honeypoter.php
Outdated
*/ | ||
protected function getDefaultLabel(): string | ||
{ | ||
return 'Fill This Field'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move to config file.
system/Honeypot/Honeypoter.php
Outdated
*/ | ||
protected function getDefaultName(): string | ||
{ | ||
return 'honeypot'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move to config file.
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->request = new IncomingRequest(new App(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better done by pulling it from the Services class:
$this->request = Services::request();
``
For future reference, we expect commits to be GPG-signed :-/ |
You are not synchronizing properly. You would synch your develop branch locally, then merge your develop into your honeypot branch. |
@jim-parry the issue with the merge has been solve sorry for that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking better - a few more small items noted. Also - will need some form of basic docs in place, please. We can always expand them later if needed, but a start would be appreciated.
application/Config/Honeypot.php
Outdated
* | ||
* @var boolean | ||
*/ | ||
public $hidden = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the default values in this file for all values.
application/Config/Services.php
Outdated
// return new \CodeIgniter\Example(); | ||
// } | ||
|
||
public static function honeypot($getShared = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should take an instance of Config\Honeypot
as an optional parameter. Something like:
public static function honeypot(BaseConfig $config = null, $getShared = true)
{
if ($getShared) { ... }
if (is_null($config))
{
$config = new Config\Honeypot();
}
return new CodeIgniter\Honeypot\Honeypot($config);
}
system/Honeypot/Honeypot.php
Outdated
|
||
//-------------------------------------------------------------------- | ||
|
||
function __construct () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should accept an instance of the BaseConfig in the parameter instead of creating an instance in side the header. Makes it more robust and easier to test.
application/Filters/Honeypot.php
Outdated
$honeypot = Services::honeypot(); | ||
if($honeypot->hasContent($request)) | ||
{ | ||
die(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should throw an exception with an explanatory method. Then devs have something they can catch and provide a better experience for users. In the near future, the exception handler in the framework will be upgraded to allow custom handlers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know die() is not a good option but never knew what to replace it with.
Thanks for that suggestion.
public function after (RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$honeypot = Services::honeypot(); | ||
$honeypot->attachHoneypot($response); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure uou need to return the modified $response
object here in order for it to work....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lonnieezell You are saying
$response = $honeypot->attachHoneypot($response);
should return the modified response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Unless it's working for you as it is. But I'm pretty sure you need to return the modified response for the change to stick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's working as it is. But do you feel returning it would be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah - if it's working, that's cool. I might have been remembering it wrong. I built it then moved on and haven't had a chance to use it much just yet. :)
# HONEYPOT | ||
#-------------------------------------------------------------------- | ||
|
||
# honeypot.hidden = 'true' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have an issue with them being here - but need to make sure to include default values in the config file. Any values in a .env file will overwrite those, so it all works as it should.
system/Honeypot/Honeypot.php
Outdated
* Self Instance of Class | ||
* @var Honeypot | ||
*/ | ||
protected $honeypotConfig; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm nit-picking here, but why use honeypot
in the name? It's in a class named Honeypot already, so I'd go with the simpler $config
personally.
Do you mean documentation on the class or what? Please explain further. |
Yes, some documentation for the class. It would go in this folder. You should be able to look at any of the files in that area for an example. You'll also need to add it to the index.rst file that's in that directory. If you have any questions feel free to ask! |
application/Config/Services.php
Outdated
{ | ||
if ($getShared) | ||
{ | ||
return self::getSharedInstance('honeypot'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - it is needed. That allows the instance to be cached and shared among all classes. The problem is that you need to pass the $config param into it. Basically, any params that get passed in the main function need to be passed into the getSharedInstance
method:
return self::getSharedInstance('honeypot', $config);
Can you advice me on how to add this? |
Signed-off-by: Nwoke David Udoka <[email protected]>
Signed-off-by: Nwoke David Udoka <[email protected]>
@lonnieezell I think everything is ok here. If there is any more changes just let me know am available. |
Looks pretty good, @dvdnwoke, thanks! There are a couple of small things, but I can tweak those later. |
Implementation of bot Honeypot as discussed on this issue