Skip to content

Conversation

dvdnwoke
Copy link
Contributor

@dvdnwoke dvdnwoke commented Jun 3, 2018

Implementation of bot Honeypot as discussed on this issue

@dvdnwoke dvdnwoke changed the title Honeypot Honeypot Filter Jun 3, 2018
Copy link
Member

@lonnieezell lonnieezell left a 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!

use CodeIgniter\HTTP\ResponseInterface;
use Config\Honeypot;

class Honeypoter
Copy link
Member

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.

new self(): self::$selfObject;

// TODO Will there be need to protect against bad data?
if($request->getVar(self::$selfObject->name)){
Copy link
Member

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.

{

// Checks honeypot field if value was entered then show blank if so.
if(Honeypoter::honeypotHasContent($request))
Copy link
Member

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) { ... }

self::$selfObject = (self::$selfObject === null) ?
new self(): self::$selfObject;

// TODO Will there be need to protect against bad data?
Copy link
Member

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.

return true;
}

if($request->getGet(self::$selfObject->name)){
Copy link
Member

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.

*/
protected function getStyle(): string
{
return '<script type="text/css" media="all">
Copy link
Member

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.

*/
protected function getDefaultTemplate(): string
{
return '<div class="hidden" style="display:none">
Copy link
Member

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.

*/
protected function getDefaultLabel(): string
{
return 'Fill This Field';
Copy link
Member

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.

*/
protected function getDefaultName(): string
{
return 'honeypot';
Copy link
Member

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(),
Copy link
Member

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();
``

@jim-parry
Copy link
Contributor

For future reference, we expect commits to be GPG-signed :-/

@jim-parry
Copy link
Contributor

You are not synchronizing properly. You would synch your develop branch locally, then merge your develop into your honeypot branch.
As it sits, you are trying to re-merge the most recent changes that were merged into the main repo, leaving all sorts of inaoorioriate commits in your PR :-/

@dvdnwoke
Copy link
Contributor Author

dvdnwoke commented Jun 5, 2018

@jim-parry the issue with the merge has been solve sorry for that

Copy link
Member

@lonnieezell lonnieezell left a 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.

*
* @var boolean
*/
public $hidden = '';
Copy link
Member

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.

// return new \CodeIgniter\Example();
// }

public static function honeypot($getShared = true)
Copy link
Member

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);
}


//--------------------------------------------------------------------

function __construct () {
Copy link
Member

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.

$honeypot = Services::honeypot();
if($honeypot->hasContent($request))
{
die();
Copy link
Member

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.

Copy link
Contributor Author

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);
Copy link
Member

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....

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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. :)

env
# HONEYPOT
#--------------------------------------------------------------------

# honeypot.hidden = 'true'
Copy link
Member

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.

* Self Instance of Class
* @var Honeypot
*/
protected $honeypotConfig;
Copy link
Member

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.

@dvdnwoke
Copy link
Contributor Author

dvdnwoke commented Jun 8, 2018

@lonnieezell

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.

Do you mean documentation on the class or what? Please explain further.

@lonnieezell
Copy link
Member

@dvdnwoke

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!

{
if ($getShared)
{
return self::getSharedInstance('honeypot');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line was removed, Because after the changes it keeps throwing error.
screenshot_20180608_184010

And i don't know the actual function of that line. If it's needed just suggest to me how to add it.

Copy link
Member

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);

@dvdnwoke
Copy link
Contributor Author

dvdnwoke commented Jun 8, 2018

@lonnieezell @jim-parry

For future reference, we expect commits to be GPG-signed :-/

Can you advice me on how to add this?

@jim-parry
Copy link
Contributor

@dvdnwoke https://bcit-ci.github.io/CodeIgniter4/contributing/index.html
Specifically, https://bcit-ci.github.io/CodeIgniter4/contributing/signing.html

dvdnwoke added 2 commits June 9, 2018 12:48
Signed-off-by: Nwoke David Udoka <[email protected]>
Signed-off-by: Nwoke David Udoka <[email protected]>
@dvdnwoke
Copy link
Contributor Author

dvdnwoke commented Jun 9, 2018

@lonnieezell I think everything is ok here. If there is any more changes just let me know am available.

@dvdnwoke dvdnwoke closed this Jun 9, 2018
@dvdnwoke dvdnwoke reopened this Jun 9, 2018
@lonnieezell
Copy link
Member

Looks pretty good, @dvdnwoke, thanks! There are a couple of small things, but I can tweak those later.

@lonnieezell lonnieezell merged commit 2a89af6 into codeigniter4:develop Jun 12, 2018
@neznaika0 neznaika0 mentioned this pull request Sep 13, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants