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

Allow Exempting IP Ranges #429

Merged
merged 7 commits into from
Oct 8, 2022

Conversation

karmanyaahm
Copy link
Contributor

@karmanyaahm karmanyaahm commented Oct 5, 2022

  • Refactor visitor IPs and allow exempting IP Ranges

    Use netip.Addr instead of storing addresses as strings. This requires
    conversions at the database level and in tests, but is more memory
    efficient and efficient to compare; mainly it facilitates the following.

    Parse rate limit exemptions as netip.Prefix. This allows storing IP
    ranges in the exemption list. Regular IP addresses (entered explicitly
    or resolved from hostnames) are IPV4/32, denoting a range of just that one
    address.

draft still need to add tests and fix bugs.

However, is the change from string IPs to netip.Addr and netip.Prefix acceptable?

Closes #423

Use netip.Addr instead of storing addresses as strings. This requires
conversions at the database level and in tests, but is more memory
efficient otherwise, and facilitates the following.

Parse rate limit exemptions as netip.Prefix. This allows storing IP
ranges in the exemption list. Regular IP addresses (entered explicitly
or resolved from hostnames) are IPV4/32, denoting a range of one
address.
@binwiederhier
Copy link
Owner

This approach is perfectly fine. I'll leave some comments.

cmd/serve.go Outdated
if err == nil {
prefixes = append(prefixes, prefix.Masked()) // masked and canonical for easy of debugging, shouldn't matter
return prefixes, nil // success
}
Copy link
Owner

Choose a reason for hiding this comment

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

What's a masked/canonical address?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example, if someone enters 10.1.2.3/8, the canonical address is 10.0.0.0/8. It represents every address from 10.0.0.1 to 10.255.255.255. If the address is ever logged in the future, this would help the output be more consistent.

Should I explain this more in the comment?

cmd/serve.go Outdated Show resolved Hide resolved
cmd/serve.go Outdated Show resolved Hide resolved
server/message_cache.go Outdated Show resolved Hide resolved
server/server.go Outdated Show resolved Hide resolved
server/server.go Outdated Show resolved Hide resolved
server/server.go Outdated Show resolved Hide resolved
util/util.go Outdated Show resolved Hide resolved
@binwiederhier
Copy link
Owner

Looks great. What's missing (since it's still in draft)?

@karmanyaahm
Copy link
Contributor Author

karmanyaahm commented Oct 8, 2022

I'm working on fixing old tests and adding tests for new functionality. Should be done later today

@codecov-commenter
Copy link

codecov-commenter commented Oct 8, 2022

Codecov Report

Base: 65.97% // Head: 66.10% // Increases project coverage by +0.12% 🎉

Coverage data is based on head (1672322) compared to base (e0ad926).
Patch coverage: 77.08% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #429      +/-   ##
==========================================
+ Coverage   65.97%   66.10%   +0.12%     
==========================================
  Files          35       35              
  Lines        3689     3720      +31     
==========================================
+ Hits         2434     2459      +25     
- Misses        906      908       +2     
- Partials      349      353       +4     
Impacted Files Coverage Δ
server/smtp_sender.go 60.97% <0.00%> (ø)
server/types.go 90.27% <ø> (ø)
cmd/serve.go 59.09% <68.42%> (+1.71%) ⬆️
server/server.go 59.97% <73.33%> (+0.02%) ⬆️
server/config.go 100.00% <100.00%> (ø)
server/message_cache.go 67.80% <100.00%> (+0.30%) ⬆️
server/visitor.go 90.32% <100.00%> (ø)
util/util.go 71.81% <100.00%> (+0.97%) ⬆️

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

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +75 to +90
func TestIP_Host_Parsing(t *testing.T) {
cases := map[string]string{
"1.1.1.1": "1.1.1.1/32",
"fd00::1234": "fd00::1234/128",
"192.168.0.3/24": "192.168.0.0/24",
"10.1.2.3/8": "10.0.0.0/8",
"201:be93::4a6/21": "201:b800::/21",
}
for q, expectedAnswer := range cases {
ips, err := parseIPHostPrefix(q)
require.Nil(t, err)
assert.Equal(t, 1, len(ips))
assert.Equal(t, expectedAnswer, ips[0].String())
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I just omit testing hostnames (because I'd have to mock the DNS resolver and all that)? The code is not all that complicated.

@karmanyaahm
Copy link
Contributor Author

server/server.go is already very thoroughly tested. I don't think there's anything meaningful I can add for this specific change.

@karmanyaahm karmanyaahm marked this pull request as ready for review October 8, 2022 02:29
@binwiederhier binwiederhier merged commit 1672322 into binwiederhier:main Oct 8, 2022
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.

Allow IP CIDRs in visitor-request-limit-exempt-hosts
3 participants