Skip to content
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

Merged

Conversation

franklinexpress
Copy link
Contributor

@franklinexpress franklinexpress commented Jun 25, 2022

This allows us to pass a custom AllowedHosts func that returns a list of such hosts and overrides the AllowedHosts list

Copy link
Owner

@unrolled unrolled left a 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?

@franklinexpress franklinexpress requested a review from unrolled June 29, 2022 20:34
Franklin De Los Santos added 2 commits June 29, 2022 14:05
Copy link
Owner

@unrolled unrolled left a 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
Comment on lines 298 to 301
if s.opt.AllowedHostsFunc != nil && !s.opt.IsDevelopment {
s.opt.AllowedHosts = append(s.opt.AllowedHosts, s.opt.AllowedHostsFunc()...)
}

Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Contributor Author

@franklinexpress franklinexpress Jul 1, 2022

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.

Copy link
Owner

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)

Copy link
Contributor Author

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

Copy link
Contributor Author

@franklinexpress franklinexpress Jul 5, 2022

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)

Copy link
Owner

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.

franklinexpress and others added 3 commits June 30, 2022 17:47
Co-authored-by: Cory Jacobsen <unrolled@users.noreply.github.com>
@franklinexpress franklinexpress requested a review from unrolled July 1, 2022 16:36
@unrolled unrolled merged commit 764d6a2 into unrolled:v1 Jul 2, 2022
@unrolled
Copy link
Owner

unrolled commented Jul 2, 2022

Thanks for all your work on this!

@franklinexpress
Copy link
Contributor Author

Thanks for all your work on this!

Thanks for the feedback

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants