Skip to content

API rate limiting#2884

Merged
ryan-pratt merged 6 commits intomainfrom
maint/login
Mar 3, 2026
Merged

API rate limiting#2884
ryan-pratt merged 6 commits intomainfrom
maint/login

Conversation

@ryan-pratt
Copy link
Copy Markdown
Contributor

@ryan-pratt ryan-pratt commented Feb 26, 2026

Added rate limiting for failed auth attempts for the user and service passwords. The user and service passwords have their own counters so that a badly behaving service doesn't lock out the user. Counters are semaphored to be thread safe and effectively handle a brute force attack.

I made the threshold configurable in .env to allow people to set how aggressive they want it. I intentionally made it so you can't turn it off, but you can set arbitrarily high limits.

image

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 26, 2026

Codecov Report

❌ Patch coverage is 50.98039% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.25%. Comparing base (397abec) to head (6fb2a12).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
...mos-cmd-tlm-api/app/controllers/auth_controller.rb 50.00% 25 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2884      +/-   ##
==========================================
- Coverage   78.29%   78.25%   -0.04%     
==========================================
  Files         673      673              
  Lines       55130    55192      +62     
  Branches      728      728              
==========================================
+ Hits        43164    43192      +28     
- Misses      11888    11922      +34     
  Partials       78       78              
Flag Coverage Δ
python 79.33% <ø> (-0.01%) ⬇️
ruby-api 79.69% <50.00%> (-0.46%) ⬇️
ruby-backend 82.18% <100.00%> (+<0.01%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@jmthomas jmthomas left a comment

Choose a reason for hiding this comment

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

Makes sense to me! Let's make sure the CLI Tests pass ...

@jmthomas
Copy link
Copy Markdown
Member

@ryan-pratt seems like the CLI is failing now with an auth error. Can you investigate?

@jmthomas
Copy link
Copy Markdown
Member

What is this telling us about other people running COSMOS in a CI/CD pipeline? Will this be a breaking change for them as well?

@ryan-pratt
Copy link
Copy Markdown
Contributor Author

What is this telling us about other people running COSMOS in a CI/CD pipeline? Will this be a breaking change for them as well?

I won't know the details until I can get it to actually work, but we knew it was technically a breaking change from the outset (removing the ability to slam the auth endpoints, which someone could definitely be doing)

@ryan-pratt ryan-pratt force-pushed the maint/login branch 2 times, most recently from 9c54cb4 to 8b4b46b Compare March 2, 2026 23:10
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 2, 2026

@ryan-pratt
Copy link
Copy Markdown
Contributor Author

Ok, I gave up and just rolled my own rate limiting for bad pw attempts only, so this is not breaking for CI. Everything's passing now.

@ryan-pratt ryan-pratt requested a review from jmthomas March 2, 2026 23:54
Copy link
Copy Markdown
Member

@jmthomas jmthomas left a comment

Choose a reason for hiding this comment

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

So you were probably just rate limiting based on good logins. Yeah this is much better.

# Check stored password hash
pw_hash = Store.get(PRIMARY_KEY)
raise "invalid password hash" unless pw_hash.start_with?("$argon2") # Catch users who didn't run the migration utility when upgrading to COSMOS 7
raise "invalid password hash" if pw_hash.nil? || !pw_hash.start_with?("$argon2") # Catch users who didn't run the migration utility when upgrading to COSMOS 7
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How is it ever nil?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Only if it's a fresh install that never had a password set. I actually don't know if this is a real check that needs to be there, but I was getting "undefined method start_with? for nil" while messing with unit tests

@ryan-pratt
Copy link
Copy Markdown
Contributor Author

So you were probably just rate limiting based on good logins. Yeah this is much better.

I was rate limiting every request, good or bad. I did that intentionally, but it's become too much of a headache to do it that way.

@ryan-pratt ryan-pratt merged commit 02086f8 into main Mar 3, 2026
42 of 43 checks passed
@ryan-pratt ryan-pratt deleted the maint/login branch March 3, 2026 00:23
jmthomas pushed a commit that referenced this pull request Mar 21, 2026
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.

2 participants