Giter Site home page Giter Site logo

eohoneypotbundle's Introduction

EoHoneypotBundle

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads

Honeypot for Symfony2 forms.

What is Honey pot?

A honey pot trap involves creating a form with an extra field that is hidden to human visitors but readable by robots. The robot fills out the invisible field and submits the form, leaving you to simply ignore their spammy submission or blacklist their IP. It’s a very simple concept that can be implemented in a few minutes and it just works – add them to your contact and submission forms to help reduce spam.

Prerequisites

This version of the bundle requires Symfony 2.1+

Installation

Step 1: Download EoHoneypotBundle using Composer

Add EoHoneypotBundle to your project by running the command:

$ composer require eo/honeypot-bundle

Composer will install the bundle to your project's vendor/eo directory.

Step 2: Enable the bundle

If you use Symfony Flex - skip this step. Otherwise, enable the bundle in bundles.php:

<?php
// config/bundles.php

<?php
return [
    // ...
    Eo\HoneypotBundle\EoHoneypotBundle::class => ['all' => true],
];

Step 3 (optional): Configure bundle to use database

To save honeypot catched requests into database you have to enable it in your configuration file: All parameters are optional

# config/packages/eo_honeypot.yaml
eo_honeypot:
    storage:
        database:
            enabled: false
            driver: mongodb # orm and mongodb are supported
            class: ApplicationEoHoneypotBundle:HoneypotPrey
        # You can also use file format to store honeypot preys.
        # This may come handy if you need to parse logs with fail2ban
        # file:
            # enabled: false
            # output: /var/log/honeypot.log
    redirect:
        enabled: true
        url: "/"
        # route: homepage
        # route_parameters: ~

If you enable the database storage, you must create a class which extends the Eo\HoneypotBundle\<Entity|Document>\HoneypotPrey base class :

<?php
namespace Application\Eo\HoneypotBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eo\HoneypotBundle\Entity\HoneypotPrey as BaseHoneypotPrey;

/**
 * @ORM\Entity
 */
class HoneypotPrey extends BaseHoneypotPrey
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

or

<?php
namespace Application\Eo\HoneypotBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Eo\HoneypotBundle\Document\HoneypotPrey as BaseHoneypotPrey;

/**
 * @MongoDB\Document
 */
class HoneypotPrey extends BaseHoneypotPrey
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

Usage

Once installed and configured you can start using Eo\HoneypotBundle\Form\Type\HoneypotType form type in your forms.

Basic usage example:

<?php

namespace Acme\DemoBundle\Form\Type;

use Eo\HoneypotBundle\Form\Type\HoneypotType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class FooType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType);
        $builder->add('email', EmailType);

        // Honeypot field
        $builder->add('SOME-FAKE-NAME', HoneypotType::class);
    }
}

Events

If the hidden honeypot field has some data bundle will dispatch a bird.in.cage event. You can create an event listener to execute custom actions. See Eo\HoneypotBundle\Event\BirdInCage and How to Register Event Listeners and Subscribers for more information.

License

This bundle is under the MIT license. See the complete license in the bundle:

Resources/meta/LICENSE

Reporting an issue or a feature request

Issues and feature requests related to this bundle are tracked in the Github issue tracker https://github.com/eymengunay/EoHoneypotBundle/issues.

eohoneypotbundle's People

Contributors

akenroberts avatar bocharsky-bw avatar ceesvanegmond avatar ehibes avatar emmanuelvella avatar esserj avatar eymengunay avatar janopae avatar jorenmartens avatar polarbirke avatar relthyg avatar stefantalen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

eohoneypotbundle's Issues

Is MongoDB mandatory?

Hi there, I configured config.yml for disabling db and configuring at least orm:

eo_honeypot:
use_db: false
db_driver: orm # orm and mongodb are supported

Unfortunately calling a form that include an honeypot field it returns:

ServiceNotFoundException: The service "eo_honeypot.form.type.honeypot" has a dependency on a non-existent service "doctrine.odm.mongodb.document_manager".

So, do I really need to set up a mongoDB server to use this extension?

Thx in advance

Errors in FormType

Hi there, as of commit bbaf1ae a few errors:

1. Variable $seance not set in %mydir%/vendor/eo/honeypot-bundle/Eo/HoneypotBundle/Form/Type/HoneypotType.php line 45

2. Using $this when not in object context in %mydir%/vendor/eo/honeypot-bundle/Eo/HoneypotBundle/Form/Type/HoneypotType.php line 48

I reached the point 2 after commenting the previous two rows.

Thx in advance!

add ability to not raise FormError when honeypot is filled

My use case is as follows:
I have kind of a registration form.
If a bot or a bot writer fills out all fields then:

  • the registration mail should not be send
  • there should be no error information in frontend so that the bot can't validate it's form submit

Alternate ORM configuration

Using the class HoneypotPrey extends BaseHoneypotPrey configuration as shown in the example, I was getting this error when running app/console doctrine:schema:update --dump-sql:

[Doctrine\ORM\Mapping\MappingException]
Duplicate definition of column 'id' on entity 'ExampleBundle\Entity\HoneypotPrey' in a field or discriminator column mapping.

I then tried extending Eo\HoneypotBundle\Model\HoneypotPrey instead of Eo\HoneypotBundle\Entity\HoneypotPrey, and that worked as far as the schema update was concerned. I had two tables - HoneypotPrey with just the id column, and honeypot_prey with the columns id, ip, and createdAt. However, after submitting some test spam registrations with the honeypot field populated with data, I was only getting new rows in the HoneypotPrey table (and lines appended to honeypot.log), but nothing in the honeypot_prey table.

So, instead, I mapped the schema like so (sorry, not a fan of annotations)...

ExampleBundle\Entity\HoneypotPrey:
    type: entity
    table: HoneypotPrey
    id:
        id:
            type:   integer
            generator:
                strategy: AUTO
    fields:
        createdAt:
            type:     datetime
        ip:
            type:     string

... generated the entities, copied the __construct() method from Eo\HoneypotBundle\Model\HoneypotPrey into my Entity class, modified my Entity class like so...

namespace ExampleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eo\HoneypotBundle\Model\HoneypotPreyInterface;

/**
 * HoneypotPrey
 */
class HoneypotPrey implements HoneypotPreyInterface
{
    ...

... dropped the honeypot_prey table, and reran the schema update command. I'm now getting records in my HoneypotPrey table...

mysql> select * from HoneypotPrey;
+----+---------------------+-------------+
| id | createdAt           | ip          |
+----+---------------------+-------------+
|  4 | 2015-01-14 00:05:56 | 172.16.61.1 |
+----+---------------------+-------------+

Configs are set like so:

# Honeypot
eo_honeypot:
    storage:
        # Record for reporting
        database:
            enabled: true
            driver:  orm
            class:   ExampleBundle:HoneypotPrey
        # Log for IP banning using fail2ban
        file:
            enabled: true
            output:  %kernel.root_dir%/logs/honeypot.log

Template reference not found

Using Symfony 4.2.4 and HoneypotBundle 1.2.1 I get a Twig_Error_Loader Exception because your template can't be found/resolved.
Template reference "EoHoneypotBundle:Form:div_layout.html.twig" not found, did you mean "@EoHoneypot/Form/div_layout.html.twig"?

I suggest using the @EoHoneypot/Form/div_layout.html.twig resource instead of EoHoneypotBundle:Form:div_layout.html.twig in your FormCompilerPass class.

Implement time-based protection

In order to complicate automated mass-submissions of forms, one possible solution would be to include a hidden field in the form that includes a timestamp, plus a second field that contains an HMAC for this timestamp value + a secret value.

Upon submission, we could check if the timestamp is legit (the HMAC signature is correct) and falls into a configurable range – so for example, only accept form submissions for forms rendered at least 30s ago and not older than 4 hours.

This does of course not prevent automated form submissions, but would at least require that forms be fetched periodically and kept on hold for some time before they can be abused.

The timestamp + HMAC cannot prevent the same form from being submitted multiple times, but it has the advantage that we do not need to keep state in the backend.

Another approach would be to issue unique form-IDs, but that would require some tracking mechanism (a database, key-value-store, ...) to keep the issued IDs, remove submitted and expire old ones. That would be more involved to set up.

For sure, it's not a perfect solution – but anyway, is that something you would support in this bundle and that you would accept a PR for? Or is this outside of what this bundle tries to provide?

Add new version tag

Since the documentation says we should add the bundle with dev-master I'm currently getting failed requirements when updating composer since the dev-master requires symfony 3.0 and my project runs on ~2.3

Reject form if honeypot field is omitted?

I am under the impression that if the submitted form does not contain the honeypot field at all, it is accepted as well.

Would it make sense to make this check more strict and also reject the form if someone tries to submit an older (cached) version of the form without the field?

Would you accept a PR for that?

Using $this when not in object context

An error was not solved with commit 95d2976

  1. Using $this when not in object context in %mydir%/vendor/eo/honeypot-bundle/Eo/HoneypotBundle/Form/Type/HoneypotType.php line 44

To solve this problem please replace row 42 with the followings two lines:

$container = $this->container; 
$builder->addEventListener(FormEvents::PRE_BIND, function(FormEvent $event) use ($container) {

then replace every occurence of $this->container with $container (in rows 45, 46 and 52).

This works with

database:
     enabled: false

and

file:
     enabled: true 

in config.yml (not tested with different config)

Exception while trying to display form

Hi when I try to display a form I get this exception:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class QuienLoRepara\UsuariosBundle\Entity\Cliente could not be converted to string in /data2/virtual/quienlorepara.com/app/cache/dev/twig/98/90/f8aef2eebbb3f0282b24bad434c9.php line 323") in QLRUsuariosBundle:User:signupClient.html.twig at line 7

This is part of my code:

$cliente = new Cliente();
$form = $this->createFormBuilder($cliente)
->add('nombre', 'text')
->add('email', 'text')
->add('pais', 'country')
->add('password', 'password')
->add('pass_conf', 'password')
->add('prueba', 'honeypot')
->add('Registrarme', 'submit')
->getform();

$form->handleRequest($request);

Any ideas?

Ready for Symfony 3.0

In Symfony 2.8 your Bundle throws a deprecated notice.

You need to add the configureOptions() Function to replace the setDefaultOptions() Function in HoneypotType.

[Request] Adding develop branch

Right now PR's can only be send to the master branch.

Maybe it would be a good idea to have PR's send to the develop branch and be merged from there to the master branch

Tag release

Can we get a new tagged release? I just ran into this error

  [Doctrine\Common\Annotations\AnnotationException]                                                                           
  [Semantical Error] The annotation "@Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Eo\HoneypotBundle\Document  
  \HoneypotPrey does not exist, or could not be auto-loaded.                                                                  

Locking version against dev-master fixes.

How to use this field type in the FOSUserBundle login form?

The login form in FOSUserBundle does not have a LoginFormType or equivalent, is just a form HTML markup added in a twig template which I can override: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Security/login_content.html.twig

I'm still not sure how to add the Eo\HoneypotBundle\Form\Type\HoneypotType field provided by this bundle under this scenario.

Can someone help to point me in the right direction?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.