Caddy2-filter: set regex in contenty_type

Hi! I would like to check if the {http.request.orig_uri.query} contains the expected regex.

If my URL is: https://testcaddy.com/file.xml?test=12341234123412341234123412341234, then the pattern “test” should be replaced by test=12341234123412341234123412341234 (it is a GUID).

filter {
        path file\.xml
        search_pattern "test"
        replacement {http.request.orig_uri.query}
        content_type /^test=\b[a-zA-Z0-9]{32}\b$/
    }

I tried it but it not works…could you help me? Thanks!

I see you’ve already opened an issue on GitHub:

You’ll probably get better help from there. I’ve not used the plugin, the author will probably be the best person to help.

It would probably be helpful if you can show example requests with curl -v and an example of what the response body looks like that you’re trying to replace. You might want to enable the debug global option in case the plugin has additional logging that would help show what’s going on.

1 Like

Thank you for the answer. I don’t receive any reply from GitHub…
I tried the debug mode but there are no error logs that could help me to solve the issue.

I have an XML file and with Caddy, I would like to replace some strings. So, the filter code should search the word “test”, and replace it with {http.request.orig_uri.query}, but I would like to insert the condition that the value of {http.request.orig_uri.query} must be a GUID (e.g. to avoid XML injection).

{http.request.orig_uri.query} is the URL value after the ? character.

Might be worth trying to checkout the plugin and add debug statements to the code yourself to find the issue. You can build the plugin from your local checkout using xcaddy, see the README on the xcaddy project’s GitHub page which explains how to replace the module with a local copy. You can add fmt.Printf("\n%+v\n", someVariable) throughout to get it to spit out some data to stdout at runtime.

Is it possible to use path_regexp to check if the path contains the regex /^test=\b[a-zA-Z0-9]{32}\b$/ and, if true, use the following filter?

filter {
path file.xml
search_pattern “test”
replacement {http.request.orig_uri.query}
content_type .*
}

path_regexp only looks at the path, not the query. But you can use the expression matcher to do a regexp match on the query.

@query-match `{query}.matches('^test=\b[a-zA-Z0-9]{32}\b$')`
filter @query-match {
	...
}

It not works, let me know if there are any errors please, thanks…
image

I tried also {query} instead of {http.request.orig_uri.query}

Like I said earlier, I don’t use that plugin, so I can’t help you with that specifically.

But you can try replacing filter with another directive like respond to see if the matcher is working properly.

@query-match `{query}.matches('^test=\b[a-zA-Z0-9]{32}\b$')`
respond @query-match "yep, the query matched"

If you don’t see that when you make the request with curl -v, then the matcher is incorrect.

The issue was the regex, solution: ^test=\\b[a-zA-Z0-9]{32}\\b

1 Like
@query-match  `{query}.matches('^test=\b[a-zA-Z0-9]{32}\b$')`

not @query-match {
       respond 404
}

Error: adapting config using caddyfile: Caddyfile:33: unrecognized directive: not

I would like to get 404 error if there is no match, but I get the error during the caddy validation. How can I fix it?

Just put a ! in front of the condition in the expression.

@query-match `!{query}.matches('^test=\b[a-zA-Z0-9]{32}\b$')`

Or the long way:

@query-match not expression `{query}.matches('^test=\b[a-zA-Z0-9]{32}\b$')`
2 Likes

Hi! I’m trying the expressions but can’t get them to work…I need to check 3 different queries, but they could be present or not in the URL…could you check this code, please?

@query `!{http.request.orig_uri.query}.matches("^\\s*$")` //check if there is the query in url

@string1-not-match `!{http.request.uri.query.string1}.matches("^(.*\\.google\\.com$")`

@string2-not-match `!{http.request.uri.query.string2}.matches("^.*\\b[a-zA-Z0-9]{32}\\b$")`

@string3-not-match `!{http.request.uri.query.string3}.matches("^.*\\b[a-zA-Z0-9]{32}\\b$")`

@match expression `{string1-not-match} || {string2-not-match} || {string3-not-match}`

handle @query {
	respond @match 404
}

I have this error in logs:
logger":“http.matchers.expression”,“msg”:“evaluating expression”,“error”:“no such overload”

You can’t use a prior expression in another. That’s not a thing. Matchers don’t create placeholders.

You should just make one big expression for the thing you want to match, with || in between for OR etc.

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