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

feat: Add rate limiter #1976

Merged
merged 13 commits into from
Feb 23, 2023
Merged

feat: Add rate limiter #1976

merged 13 commits into from
Feb 23, 2023

Conversation

bolkedebruin
Copy link
Contributor

@bolkedebruin bolkedebruin commented Jan 9, 2023

Description

This allows regulation of the number of times a user can request a particular API or view given a time-frame. It protects against brute-forcing entry and denial of service.

Both Airflow and Superset have a long standing issue of not being able to rate limit access to APIs and views allowing for brute forcing access.

apache/superset#10560
https://lists.apache.org/thread/hdpyr3y5cq0djcogn1ql7gj7vq09xo0j
apache/superset#10620

This change allows users to decorate their APIs and Views with @limit.

Note I have turned to default to be ON for authentication for AuthDB and AuthLDAP (Oauth/oidc have their own protection mechanisms) as FAB should be secure out of the box I assume.

cc @potiuk

ADDITIONAL INFORMATION

  • Has associated issue:
  • Is CRUD MVC related.
  • Is Auth, RBAC security related.
  • Changes the security db schema.
  • Introduces new feature
  • Removes existing feature

@codecov
Copy link

codecov bot commented Jan 9, 2023

Codecov Report

Merging #1976 (ea19c87) into master (57e9d10) will increase coverage by 0.16%.
The diff coverage is 98.57%.

❗ Current head ea19c87 differs from pull request most recent head 7723ff1. Consider uploading reports for the commit 7723ff1 to get more accurate results

@@            Coverage Diff             @@
##           master    #1976      +/-   ##
==========================================
+ Coverage   78.10%   78.27%   +0.16%     
==========================================
  Files          71       72       +1     
  Lines        8611     8667      +56     
==========================================
+ Hits         6726     6784      +58     
+ Misses       1885     1883       -2     
Flag Coverage Δ
python 78.27% <98.57%> (+0.16%) ⬆️

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

Impacted Files Coverage Δ
flask_appbuilder/security/views.py 62.63% <50.00%> (ø)
flask_appbuilder/api/__init__.py 96.34% <100.00%> (+0.03%) ⬆️
flask_appbuilder/base.py 84.85% <100.00%> (+0.27%) ⬆️
flask_appbuilder/baseviews.py 89.90% <100.00%> (+0.25%) ⬆️
flask_appbuilder/security/decorators.py 94.68% <100.00%> (+0.77%) ⬆️
flask_appbuilder/security/manager.py 75.64% <100.00%> (+0.56%) ⬆️
flask_appbuilder/utils/limit.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Owner

@dpgaspar dpgaspar left a comment

Choose a reason for hiding this comment

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

This is great! left some non blocking comments. Can you please revisit?

flask_appbuilder/security/manager.py Outdated Show resolved Hide resolved
flask_appbuilder/security/manager.py Outdated Show resolved Hide resolved
flask_appbuilder/security/manager.py Outdated Show resolved Hide resolved
flask_appbuilder/security/decorators.py Outdated Show resolved Hide resolved
flask_appbuilder/security/manager.py Outdated Show resolved Hide resolved
This allows regulation of the number of times a user can
request a particular API or view given a time-frame. It
protects against brute-forcing entry and denial of service.
@bolkedebruin
Copy link
Contributor Author

@dpgaspar ptl, i have addressed your comments and fixed the linting (can I suggest using a newer flake8, the one used here is really really old).

@bolkedebruin
Copy link
Contributor Author

Hi @dpgaspar PR is finished. Would be great if it was included in 4.2.1 :-)

@dpgaspar
Copy link
Owner

sorry, it won't, but I'll make a 4.3.0 next week just for this, no worries

@bolkedebruin
Copy link
Contributor Author

awesome, that'll work :-)

@@ -255,6 +257,10 @@ def __init__(self, appbuilder):
app.config.setdefault("AUTH_LDAP_LASTNAME_FIELD", "sn")
app.config.setdefault("AUTH_LDAP_EMAIL_FIELD", "mail")

# Rate limiting
app.config.setdefault("AUTH_RATE_LIMITED", True)
app.config.setdefault("AUTH_RATE_LIMIT", "2 per 5 second")
Copy link
Owner

Choose a reason for hiding this comment

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

This seems a bit agressive, what do you think about keeping the same rate but increasing the number of attempts needed. Example: "10 per 25 second"?

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 don't think it's aggressive. A human being will have trouble meeting this if they have to retype the password and otherwise a short pause won't hurt. An automated system configured wrongly will remain wrong.

So if you realize "omg ive typed in the password/username twice" you probably start thinking a bit longer and cross the 5s barrier (try it!) and if you do make it a slight pause won't hurt IMHO.

An alternative is to redirect to a captcha when reaching the limit but I'll leave that to someone else.

Anyways I'm not married to this limit but I do not think it's too aggressive.

Copy link
Owner

Choose a reason for hiding this comment

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

I was able to reach it pretty easily and manually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Easily as in normal human behavior or as in trying to trigger it? Triple entry isn't common.

But if you think it's better we can switch.

Copy link
Owner

Choose a reason for hiding this comment

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

Trying to trigger it, but If you agree, I think it would be more conservative and still safe against brute force and DoS

@dpgaspar dpgaspar merged commit bef2421 into dpgaspar:master Feb 23, 2023
potiuk added a commit to potiuk/airflow that referenced this pull request Mar 4, 2023
FAB 4.3.0 added rate limiting and we would like to upgrade to this
version.

This requires to bring some of the changes from the PRs merged in
Flask App Builder:

* dpgaspar/Flask-AppBuilder#1976
* dpgaspar/Flask-AppBuilder#1997
* dpgaspar/Flask-AppBuilder#1999

While Flask App Builder disabled rate limitig by default, Airlfow
is "end product" using it and we decided to enable it.
potiuk added a commit to apache/airflow that referenced this pull request Mar 4, 2023
FAB 4.3.0 added rate limiting and we would like to upgrade to this
version.

This requires to bring some of the changes from the PRs merged in
Flask App Builder:

* dpgaspar/Flask-AppBuilder#1976
* dpgaspar/Flask-AppBuilder#1997
* dpgaspar/Flask-AppBuilder#1999

While Flask App Builder disabled rate limitig by default, Airlfow
is "end product" using it and we decided to enable it.
ahidalgob pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Jun 12, 2023
FAB 4.3.0 added rate limiting and we would like to upgrade to this
version.

This requires to bring some of the changes from the PRs merged in
Flask App Builder:

* dpgaspar/Flask-AppBuilder#1976
* dpgaspar/Flask-AppBuilder#1997
* dpgaspar/Flask-AppBuilder#1999

While Flask App Builder disabled rate limitig by default, Airlfow
is "end product" using it and we decided to enable it.

GitOrigin-RevId: b7f05008ae45fc208456f7f64c19d08ad1cf7313
ahidalgob pushed a commit to GoogleCloudPlatform/composer-airflow that referenced this pull request Nov 7, 2023
FAB 4.3.0 added rate limiting and we would like to upgrade to this
version.

This requires to bring some of the changes from the PRs merged in
Flask App Builder:

* dpgaspar/Flask-AppBuilder#1976
* dpgaspar/Flask-AppBuilder#1997
* dpgaspar/Flask-AppBuilder#1999

While Flask App Builder disabled rate limitig by default, Airlfow
is "end product" using it and we decided to enable it.

GitOrigin-RevId: b7f05008ae45fc208456f7f64c19d08ad1cf7313
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.

None yet

2 participants