Skip to content

ConfigurationBuilder.getDefaultXYZ returns internal instances #65

@per-erik

Description

@per-erik

Because ConfigurationBuilder.getDefaultXYZ returns the internal instances (and because of the way the documentation suggests to add stuff to the configuration builder), one can easily add for example matchers multiple times.

public int estimate(String password) {
    List<PasswordMatcher> matchers = ConfigurationBuilder.getDefaultPasswordMatchers();
            matchers.add(new PasswordMatcher() {
                @Override
                public List<Match> match(Configuration configuration, String password) {
                    System.out.println("Called with: " + password);
                    return new ArrayList<>();
                }
            });
    Configuration config = new ConfigurationBuilder()
        .setPasswordMatchers(matchers)
        .createConfiguration();
    Nbvcxz tester = new Nbvcxz(config)
    Result result = tester.estimate(password);
    return result.getBasicScore(); // Yes I've seen that you dislike this, but just to illustrate! :)
}

Running the above once will print "Called with ...". Running it a second time will print "Called with ..." twice, a third time trice, a fourth time will print it four times and so on.

One can get around this by either creating the configuration once (and not every time a test is made) or by wrapping the return value of ConfiguratioBuilder.getDefaultPasswordMatchers() in a new ArrayList.

This is not a big bug but given the documentation I spent a good half hour scratching my head as to why I was getting multiple calls to my "PasswordMatcher".

Best Regards,
Per-Erik

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions