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

Feature: Log failed login attempts #2359

Merged
merged 3 commits into from Feb 19, 2023
Merged

Feature: Log failed login attempts #2359

merged 3 commits into from Feb 19, 2023

Conversation

shamoon
Copy link
Member

@shamoon shamoon commented Jan 6, 2023

Proposed change

I think from a security standpoint this is a good idea to work with e.g. fail2ban or crowdsec even though reverse proxies might make this a little less critical. I tested this locally / remotely with a RP, e.g.

[2023-01-05 21:31:40,257] [INFO] [paperless.auth] Login failed for user `admin` from IP `x.x.x.x`

Fixes #407
Fixes #1456

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other (please explain)

Checklist:

  • I have read & agree with the contributing guidelines.
  • If applicable, I have tested my code for new features & regressions on both mobile & desktop devices, using the latest version of major browsers.
  • If applicable, I have checked that all tests pass, see documentation.
  • I have run all pre-commit hooks, see documentation.
  • I have made corresponding changes to the documentation as needed.
  • I have checked my modifications for any breaking changes.

@shamoon shamoon added this to the Next Release milestone Jan 6, 2023
@shamoon shamoon requested a review from a team as a code owner January 6, 2023 05:40
@paperless-ngx-secretary paperless-ngx-secretary bot added the non-trivial Requires approval by several team members label Jan 6, 2023
@coveralls
Copy link

coveralls commented Jan 6, 2023

Pull Request Test Coverage Report for Build 3894439824

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 60 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.09%) to 92.546%

Files with Coverage Reduction New Missed Lines %
documents/views.py 20 95.27%
paperless/settings.py 40 83.78%
Totals Coverage Status
Change from base Build 3884791465: -0.09%
Covered Lines: 5438
Relevant Lines: 5876

💛 - Coveralls

@tribut
Copy link
Member

tribut commented Jan 6, 2023

This seems insecure. When we trust x-forwarded-for, we would have to have some sort of list of trusted proxies. Otherwise an attacker could blacklist arbitrary IPs by including them in the x-forwarded-for header, leading to a DoS situation.

@shamoon shamoon marked this pull request as draft January 6, 2023 14:12
@shamoon
Copy link
Member Author

shamoon commented Jan 6, 2023

Thanks @tribut , wasn’t aware of that. Can you explain how one might do that (in a way that can’t also be spoofed)?

@tribut
Copy link
Member

tribut commented Jan 6, 2023

Normally, you require a list of trusted proxies as a setting and then the code looks something like this terrible pseudocode (sorry, I'm on mobile rn):

ip = remote_addr
if remote_addr in config['trusted_proxies']:
  if 'x-forwarded-for' in headers:
    ip = headers['x-forwarded-for']

This implies that when trusted proxies are not configured, the header is always ignored.

@vanto
Copy link

vanto commented Jan 6, 2023

Didn't think of this scenario either. You may consider to delegate the problem eg to https://github.com/un33k/django-ipware with trusted proxy IPs. Yet another dependency though.

@stumpylog
Copy link
Member

My own 2 cents on this is users should use a reverse proxy for integrating security related things like fail2ban or crowdsec. Tools like Traefik already handle access logs and integrate with other tools, while having handled the odd cases like trusted proxies, etc.

@shamoon
Copy link
Member Author

shamoon commented Jan 7, 2023

My own 2 cents on this is users should use a reverse proxy for integrating security related things like fail2ban or crowdsec. Tools like Traefik already handle access logs and integrate with other tools, while having handled the odd cases like trusted proxies, etc.

I agree except that at the moment I dont think that works unless you have auth integrated with your RP and are passing that same auth to ngx. Django gives status 200 on failed logins so Im not sure theres an obvious way that those tools would 'know' there was a failed login attempt. Please someone correct me if Im wrong.

django-ipware looks like a good solution to me but as evidenced by the above Im not an expert here. In general I'm happy to close this if others are not in favor.

@shamoon shamoon force-pushed the feature-log-failed-auth branch 3 times, most recently from a872374 to 12d9bda Compare January 10, 2023 21:26
@shamoon shamoon marked this pull request as ready for review January 10, 2023 21:29
@shamoon
Copy link
Member Author

shamoon commented Jan 10, 2023

Ok I've updated this to use django-ipware which has support for trusted proxies, and a new (optional) setting PAPERLESS_TRUSTED_PROXIES which I think achieves this. Again, if consensus is to not merge its NBD to me.

@shamoon
Copy link
Member Author

shamoon commented Jan 11, 2023

@tribut any other thoughts?

@shamoon shamoon requested a review from tribut January 20, 2023 04:20
@shamoon shamoon modified the milestones: v1.11.4, Next Major Release Jan 24, 2023
@shamoon shamoon requested review from tribut and removed request for tribut February 17, 2023 15:21
@codecov
Copy link

codecov bot commented Feb 17, 2023

Codecov Report

Merging #2359 (07ec6ff) into dev (9893ae9) will increase coverage by 0.26%.
The diff coverage is 96.00%.

@@            Coverage Diff             @@
##              dev    #2359      +/-   ##
==========================================
+ Coverage   92.53%   92.79%   +0.26%     
==========================================
  Files         145      147       +2     
  Lines        6306     6361      +55     
==========================================
+ Hits         5835     5903      +68     
+ Misses        471      458      -13     
Flag Coverage Δ
backend 92.79% <96.00%> (+0.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/paperless/settings.py 83.96% <75.00%> (-0.14%) ⬇️
src/paperless/apps.py 100.00% <100.00%> (ø)
src/paperless/signals.py 100.00% <100.00%> (ø)
src/documents/serialisers.py 91.99% <0.00%> (+1.01%) ⬆️
src/documents/permissions.py 71.69% <0.00%> (+22.64%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@shamoon shamoon merged commit 13ece25 into dev Feb 19, 2023
@shamoon shamoon deleted the feature-log-failed-auth branch February 19, 2023 03:40
@maxime1992

This comment was marked as off-topic.

@github-actions
Copy link
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new discussion or issue for related concerns.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backend enhancement New feature non-trivial Requires approval by several team members security
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

6 participants