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

Refactor login / ban management #1008

Merged
merged 1 commit into from
Feb 5, 2018

Conversation

virtualtam
Copy link
Member

Relates to #324

Added:

  • Add the LoginManager class to manage logins and bans

Changed:

  • Refactor IP ban management
  • Simplify logic
  • Avoid using globals, inject dependencies

Fixed:

  • Use ban_duration instead of ban_after when setting a new ban

@virtualtam virtualtam added this to the 0.10.0 milestone Oct 27, 2017
@virtualtam virtualtam self-assigned this Oct 27, 2017
@virtualtam virtualtam mentioned this pull request Oct 27, 2017
43 tasks
@virtualtam virtualtam force-pushed the refactor/authentication branch 2 times, most recently from ae90c6c to ff7fb9e Compare October 27, 2017 17:07
@ArthurHoaro
Copy link
Member

ArthurHoaro commented Oct 28, 2017

Nice one 👍

Note that you can get rid of that GLOBAL array, and just store a simple serialized array, now that everything is in the same scope.

nodiscc
nodiscc previously approved these changes Oct 30, 2017
Copy link
Member

@nodiscc nodiscc left a comment

Choose a reason for hiding this comment

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

Nice

@virtualtam virtualtam force-pushed the refactor/authentication branch 2 times, most recently from 9fc5e93 to 897f57d Compare October 31, 2017 18:25
@virtualtam
Copy link
Member Author

@ArthurHoaro getting rid of globals was planned for #587, I'm tempted to address it in this PR though as it makes more sense to refactor the whole feature at once

@nodiscc this is still a WIP, could you un-approve the PR? (not to say you should disapprove of it ^^)

@virtualtam virtualtam added the template HTML rendering label Oct 31, 2017
@nodiscc nodiscc dismissed their stale review October 31, 2017 21:26

work in progress

@ArthurHoaro
Copy link
Member

Oh right, I forgot about this one. I'll move its milestone to get it addressed sooner, if not in this PR.

@virtualtam virtualtam force-pushed the refactor/authentication branch 2 times, most recently from d509d54 to 48da9b5 Compare November 8, 2017 22:54
@virtualtam virtualtam force-pushed the refactor/authentication branch 4 times, most recently from f95593f to 041f8c4 Compare November 27, 2017 20:33
function handleSuccessfulLogin($server)
{
$ip = $server['REMOTE_ADDR'];
// FIXME unban when behind a trusted proxy?
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure what to do for this specific point; according to my understanding of the login / ban mechanism, I'd expect this method to be on par with handleFailedLogin() as far as proxy handling is concerned:

  • the IP is not behind a trusted proxy:
    • current, expected: reset the ban counter for this IP
  • the IP is behind a trusted proxy but is not forwarded:
    • expected: do nothing
    • current: reset the ban counter for the proxy's IP
  • the IP is behind a trusted proxy and forwarded:
    • expected: reset the ban counter for the forwarded IP
    • current: reset the ban counter for the proxy's IP

ping @ArthurHoaro @nodiscc

@virtualtam virtualtam changed the title [WIP] Refactor login / ban management Refactor login / ban management Nov 27, 2017
@virtualtam
Copy link
Member Author

This part of the refactoring is up for reviewing :)
I'll submit a separate PR to address #587

}
}

$ipBans = &$this->globals['IPBANS'];
Copy link
Member

Choose a reason for hiding this comment

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

Is this for readability only? I would tend to say that it is a bad practice as you then don't see at first glance that you're manipulating a class attribute.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is this for readability only?

Yup

you then don't see at first glance that you're manipulating a class attribute

Didn't think of it, that's a very valid point :)

Copy link
Member

@ArthurHoaro ArthurHoaro left a comment

Choose a reason for hiding this comment

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

Very nice, especially regarding test coverage 👍

public function handleSuccessfulLogin($server)
{
$ip = $server['REMOTE_ADDR'];
// FIXME unban when behind a trusted proxy?
Copy link
Member

Choose a reason for hiding this comment

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

It would make more sense in canLogin() as it's not possible to have a successful login while being banned.

Copy link
Member Author

Choose a reason for hiding this comment

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

Argh, my comment on this section was hidden after the last rebase... re-posting:


I'm not sure what to do for this specific point; according to my understanding of the login / ban mechanism, I'd expect this method to be on par with handleFailedLogin() as far as proxy handling is concerned:

  • the IP is not behind a trusted proxy:
    • current, expected: reset the ban counter for this IP
  • the IP is behind a trusted proxy but is not forwarded:
    • expected: do nothing
    • current: reset the ban counter for the proxy's IP
  • the IP is behind a trusted proxy and forwarded:
    • expected: reset the ban counter for the forwarded IP
    • current: reset the ban counter for the proxy's IP

Copy link
Member

Choose a reason for hiding this comment

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

Oh I didn't remember that. So, if I understand correctly, currently the ban mechanism is pretty useless if Shaarli is behind a reverse proxy, as the REMOTE_ADDR IP is always the proxy's one? It may be a good idea to expand a bit this comment and open a new issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

the ban mechanism is pretty useless if Shaarli is behind a reverse proxy

it seems so, unless the user has:

  • properly configured the proxy / proxy chain to forward client information
  • configured the trusted proxy list in Shaarli settings

I think this point is not clearly mentioned in the docs, and I completely missed it while working on Docker images too 😿

https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#security

expand a bit this comment and open a new issue

=> #1032

if (!ban_canLogin($conf)) die(t('I said: NO. You are banned for the moment. Go away.'));
if (! $loginManager->canLogin($_SERVER)) {
die(t('I said: NO. You are banned for the moment. Go away.'));
}
if (isset($_POST['password'])
&& $sessionManager->checkToken($_POST['token'])
&& (check_auth($_POST['login'], $_POST['password'], $conf))
Copy link
Member

@ArthurHoaro ArthurHoaro Dec 2, 2017

Choose a reason for hiding this comment

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

Shouldn't check_auth be a part of LoginManager?

Copy link
Member Author

Choose a reason for hiding this comment

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

... and so do isLoggedIn() and logout(), I'm doing this one step at a time :)

@virtualtam
Copy link
Member Author

Rebased, let me know if you see other points to be addressed in this PR (and are not covered by #587 nor #1032) :)

Relates to shaarli#324

Added:
- Add the `LoginManager` class to manage logins and bans

Changed:
- Refactor IP ban management
- Simplify logic
- Avoid using globals, inject dependencies

Fixed:
- Use `ban_duration` instead of `ban_after` when setting a new ban

Signed-off-by: VirtualTam <[email protected]>
@virtualtam virtualtam merged commit 91f17fc into shaarli:master Feb 5, 2018
@virtualtam virtualtam deleted the refactor/authentication branch February 5, 2018 17:16
@virtualtam
Copy link
Member Author

Rebased and merged, thanks for the reviews :)

virtualtam added a commit to virtualtam/Shaarli that referenced this pull request Mar 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup code cleanup and refactoring enhancement security template HTML rendering
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants