-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add ability to pass a custom allowed hosts function #82
Add ability to pass a custom allowed hosts function #82
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, really like this idea! One comment on the code, and can you also update the README file with the new option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, just a couple suggestions!
secure.go
Outdated
if s.opt.AllowedHostsFunc != nil && !s.opt.IsDevelopment { | ||
s.opt.AllowedHosts = append(s.opt.AllowedHosts, s.opt.AllowedHostsFunc()...) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if s.opt.AllowedHostsFunc != nil && !s.opt.IsDevelopment { | |
s.opt.AllowedHosts = append(s.opt.AllowedHosts, s.opt.AllowedHostsFunc()...) | |
} | |
combinedAllowedHosts = s.opt.AllowedHosts | |
if s.opt.AllowedHostsFunc != nil { | |
combinedAllowedHosts = append(combinedAllowedHosts, s.opt.AllowedHostsFunc()...) | |
} |
This function is called on every request, so I think the current implementation will just append the function results every time. I'm thinking we can create a temporary variable and combine the static and dynamic lists instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I actually need it to be called on every request. In my case, the list changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggested change will still call the function on every request. The way the code is written now will append the results of the function call into s.opt.AllowedHosts
on every request... so that array will grow and grow and grow! Instead of appending to s.opt.AllowedHosts
, we can create a new variable that combines both the static list and the function call results. Then on lines 302 and 312 we can update the references from s.opt.AllowedHosts
to the new variable we created (I called it combinedAllowedHosts
in my suggestion above)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, of course, I see. Looked into the source more closely, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am actually wondering if we can modify this to pass in the request to the function, what do you think? Or have another functionisHostAllowed(host string)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of passing the request in! I'll try to prototype that soon and see how it fits in.
Co-authored-by: Cory Jacobsen <unrolled@users.noreply.github.com>
…' into Add_Custom_AllowedHosts_function
Thanks for all your work on this! |
Thanks for the feedback |
This allows us to pass a custom AllowedHosts func that returns a list of such hosts and overrides the AllowedHosts list