-
Notifications
You must be signed in to change notification settings - Fork 587
feat: Add Sb-Forwarded-For header and IP-based rate limiting #2295
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e107e91
Add Sb-Forwarded-For support and middleware
jnschaeffer f9ced4c
Actually use middleware, rearrange recoverer to get used earlier
jnschaeffer 96350c2
Remove unused config value
jnschaeffer 5d29993
Rename functions to reduce stuttering, move middleware into sbff
jnschaeffer 4d89618
Add README documentation for SBFF, feature, fix config name JSON tag
jnschaeffer 30b0a1d
Add tests for sbff package
jnschaeffer 2d1005d
Add rate limiting tests, export SBFF header name
jnschaeffer 6672572
Add GetIPAddress tests
jnschaeffer cd7feb2
Make sbff.WithIPAddress unexported
jnschaeffer 5718dfb
Remove some extraneous whitespace
jnschaeffer c1495ca
Simplify GetIPAddress logic
jnschaeffer 87a91a5
Merge branch 'master' into feat/client-ip-forwarding
jnschaeffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package sbff | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/supabase/auth/internal/conf" | ||
| ) | ||
|
|
||
| // HeaderName is the Sb-Forwarded-For header name. It is all lowercase here as HTTP header names | ||
| // are not case-sensitive. | ||
| const HeaderName = "sb-forwarded-for" | ||
|
|
||
| var ( | ||
| ctxKeySBFF = &struct{}{} | ||
|
|
||
| ErrHeaderNotFound = errors.New("Sb-Forwarded-For header not found") | ||
| ErrHeaderInvalid = errors.New("invalid Sb-Forwarded-For header value") | ||
| ) | ||
|
|
||
| func parseSBFFHeader(headerVal string) (string, error) { | ||
jnschaeffer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| values := strings.SplitN(headerVal, ",", 2) | ||
| key := strings.TrimSpace(values[0]) | ||
| if ipAddr := net.ParseIP(key); ipAddr != nil { | ||
| return ipAddr.String(), nil | ||
| } | ||
|
|
||
| return "", ErrHeaderInvalid | ||
| } | ||
|
|
||
| // GetIPAddress returns the value of the IP address in Sb-Forwarded-For as defined by | ||
| // SBForwardedForMiddleware. If no value is present in the request context, this function will | ||
| // return ("", false). | ||
| func GetIPAddress(r *http.Request) (addr string, found bool) { | ||
| if ipAddr, ok := r.Context().Value(ctxKeySBFF).(string); ok && ipAddr != "" { | ||
| return ipAddr, true | ||
| } | ||
|
|
||
| return "", false | ||
| } | ||
cstockton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // withIPAddress parses the Sb-Forwarded-For header and adds the leftmost value to the | ||
| // request context if it is a valid IP address, then returns a new request with modified context. | ||
| // If the leftmost value is not a valid IP address or the header is not set, this function returns | ||
| // an error. | ||
| func withIPAddress(r *http.Request) (*http.Request, error) { | ||
| headerVal := r.Header.Get(HeaderName) | ||
| if headerVal == "" { | ||
| return nil, ErrHeaderNotFound | ||
| } | ||
|
|
||
| parsedIPAddr, err := parseSBFFHeader(headerVal) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| ctx := r.Context() | ||
| newCtx := context.WithValue(ctx, ctxKeySBFF, parsedIPAddr) | ||
| out := r.WithContext(newCtx) | ||
|
|
||
| return out, nil | ||
| } | ||
|
|
||
| // Middleware returns a middleware function that parses the Sb-Forwarded-For header | ||
| // and adds the leftmost header value to the request context if GOTRUE_SECURITY_SB_FORWARDED_FOR_ENABLED | ||
| // is true and the value is a valid IP address. | ||
| func Middleware(cfg *conf.SecurityConfiguration, errCallback func(*http.Request, error)) func(http.Handler) http.Handler { | ||
| out := func(next http.Handler) http.Handler { | ||
| handlerFunc := func(rw http.ResponseWriter, r *http.Request) { | ||
| if !cfg.SbForwardedForEnabled { | ||
| next.ServeHTTP(rw, r) | ||
| return | ||
| } | ||
|
|
||
| reqWithSBFF, err := withIPAddress(r) | ||
| switch { | ||
| case err == nil: | ||
| next.ServeHTTP(rw, reqWithSBFF) | ||
| case errors.Is(err, ErrHeaderNotFound): | ||
| next.ServeHTTP(rw, r) | ||
| default: | ||
| errCallback(r, err) | ||
| next.ServeHTTP(rw, r) | ||
| } | ||
| } | ||
|
|
||
| return http.HandlerFunc(handlerFunc) | ||
| } | ||
|
|
||
| return out | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.