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

caddyfile: Reject directives in the place of site addresses #6104

Conversation

armadi1809
Copy link
Contributor

Resolves #6095

@francislavoie from my understanding, that's the only change needed but I might be wrong. Also, let me know if you think we need more tests.

@francislavoie francislavoie changed the title Caddy file: Improve errors when directive is used outside of site block caddyfile: Reject directives in the place of site addresses Feb 14, 2024
@francislavoie
Copy link
Member

francislavoie commented Feb 14, 2024

Yeah I think we should have a test that ensures http://handle is still allowed. I think it will be because directives aren't registered with a scheme, but just a sanity check.

Also, I'm realizing that we're not marking where in the Caddyfile (filename/linenum) the problem happened. I don't think that information is known at this point in the parser unfortunately. Maybe we should move these checks to be somewhere else where we still have the Tokens?

@francislavoie francislavoie added the bug 🐞 Something isn't working label Feb 14, 2024
@francislavoie francislavoie added this to the v2.8.0 milestone Feb 14, 2024
@armadi1809 armadi1809 force-pushed the improve-top-level-directive-errors-caddy-file branch 2 times, most recently from 41dd354 to 5cde41d Compare February 17, 2024 21:15
@armadi1809 armadi1809 force-pushed the improve-top-level-directive-errors-caddy-file branch from 5cde41d to daa0406 Compare February 17, 2024 21:29
@armadi1809
Copy link
Contributor Author

@francislavoie I get your point with the filename and line number but I am not entirely sure about where to move this new check since we need to loop through the block keys to determine if we are using a directive as a site address but we only have access to the Tokens at the segment level. Maybe I am missing something here though?

@francislavoie
Copy link
Member

I think we could either rework ServerBlock to hold []Token instead of []string for the site addresses (to retain the file/line along with the text), or move the errors to func (p *parser) addresses() error { where it actually parses the addresses initially.

@armadi1809
Copy link
Contributor Author

armadi1809 commented Feb 17, 2024

I think I like the second option better as it feels more intuitive to perform the check there. One issue is that we don't have access to the registeredDirectives in the caddyfile package which makes me wonder if the only solution is reworking ServerBlocks. Unless there is another way of accessing all the caddy known directives.

@francislavoie
Copy link
Member

Ah right I forgot about that 😢 yeah it'll probably be better to pass down the tokens I guess. We can add some helper methods to ServerBlock to unwrap the tokens to a []string if necessary.

@francislavoie
Copy link
Member

Tests are failing - looks like we need to add the "," trim in another spot.

@armadi1809 armadi1809 force-pushed the improve-top-level-directive-errors-caddy-file branch from 7d1e18e to 50172d0 Compare February 18, 2024 21:22
@armadi1809
Copy link
Contributor Author

@francislavoie I think we should be good to go now. I changed the location of that Trim to be where we actually add the token rather than in the helper that maps the tokens to strings. I also addressed your comments from above. Thanks for your feedback and help.

caddyconfig/caddyfile/parse.go Outdated Show resolved Hide resolved
caddyconfig/caddyfile/parse.go Outdated Show resolved Hide resolved
caddyconfig/caddyfile/parse.go Outdated Show resolved Hide resolved
@armadi1809 armadi1809 force-pushed the improve-top-level-directive-errors-caddy-file branch from 50172d0 to 9d59719 Compare February 18, 2024 23:25
@armadi1809
Copy link
Contributor Author

All the feedback items from above should now be resolved.

Copy link
Member

@francislavoie francislavoie left a comment

Choose a reason for hiding this comment

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

Thanks, this is great!

I made a few minor test adjustments, i.e. the error now asserts that the file/line is present, and I changed a couple other tests to fix some port conflicts that happened on my machine due to the tests using common ports (admin 2019 but I already have Caddy running, and 8080 is commonly used for some HTTP apps).

@francislavoie francislavoie enabled auto-merge (squash) February 19, 2024 00:18
@francislavoie francislavoie merged commit b893c8c into caddyserver:master Feb 19, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better Caddyfile errors when a directive is used at top-level
2 participants