Parsing error if file_server is used twice even in different handle blocks?

My aim is to have some reusable snippets for server locations pardon site blocks. In my case I want to always serve the same two files, security.txt and robots.txt at the exact same solution for each root.

However, note that the server itself may have another file location/directory at a totally different place to also serve files or it may otherwise be a reverse proxy or do something completely different or so…

1. Caddy version (caddy version):

v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=

2. How I run Caddy:

a. System environment:

Fedora CoreOS and Fedora Workstation 34
podman

b. Command:

For a test I am able to reproduce it like that:

podman run --rm --name caddytest -v $PWD:/etc/caddy:z caddy

The examples I show here are made a little more minimalistic, but I could reproduce the error with these examples, so they are minimum reproducible examples (MWEs). For production use, you of course need to mount the actual files to serve etc.

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

{
	admin off
	# debug
}

(header_csp_disallowall) {
	header Content-Security-Policy "default-src 'none'; referrer no-referrer"
}

(header_security) {
	header {
		X-Frame-Options "SAMEORIGIN"
		X-Xss-Protection "1; mode=block"
		# MIME Sniffing protection (Chrome & IE)
		X-Content-Type-Options "nosniff"
		Referrer-Policy "no-referrer"
	}
}

(header_https) {
	# HSTS (63072000 seconds)
	# https://ssl-config.mozilla.org/#server=caddy&version=2.1.1&config=modern&guideline=5.6
	header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
}
(basic_security) {
	import header_security
	import header_https
	import securitytxt
	import robotstxt # <-- THIS HERE IS LINE 29 (you see below why I'm highlighting this) 
}

(securitytxt) {
	handle /.well-known/security.txt {
		file_server /srv/www
	}
}
(robotstxt) {
	handle /robots.txt {
		file_server /srv/www
	}
}

(webroot) {
	redir /security.txt /.well-known/security.txt

	root * /srv/www
	file_server
}

# manually expose metrics as we disabled the admin API
:2019 {
	metrics /metrics
}

:81 {
	import basic_security
	import header_csp_disallowall

	redir https://is.gd/5H4nJ1
}

:80 {
	import basic_security
	import header_csp_disallowall

	import webroot
}

3. The problem I’m having:

$ podman run --rm --name caddytest -v $PWD:/etc/caddy:z caddy                                           
{"level":"info","ts":1625874928.3258336,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
run: adapting config using caddyfile: parsing caddyfile tokens for 'handle': /etc/caddy/Caddyfile:29 - Error during parsing: Wrong argument count or unexpected line ending after 'robotstxt'

4. Error messages and/or full log output:

N/A

5. What I already tried:

I originally thought the issue is that file_server is used twice (but with a different matcher, so I thought this should work, should not it?)…

So this was my original version, which I changed to use handle as shown above, which did not help, as you can see: (the error is the same)

(securitytxt) {
	file_server /.well-known/security.txt {
		root /srv/www
	}
}
(robotstxt) {
	file_server /robots.txt {
		root /srv/www
	}
}

BTW, this works successfully:

$ podman run --rm --name caddytest -v $PWD:/etc/caddy:z caddy caddy fmt /etc/caddy/Caddyfile

Also, if you remove line 29, i.e. remove import robotstxt, then it works, but obviously this is not what I want…

6. Links to relevant resources:

N/A


The error is just really confusing as I don’t know at all, why it points me to this line which is just an import

I agree that error message is confusing.

The problem though is that you import securitytxt and import robotstxt before those snippets are defined; they need to be defined in order, because the Caddyfile is parsed top-down.

I’ll look into what we can do to make that error message clearer.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.