Concatenate two strings in apache config
Asked Answered
J

5

9

In an apache config file how can i concatenate two strings?

For example:

"hello" + "world"
// "helloworld"

Why?

One might want to do this to handle large Headers such as a Content-Security-Policy below is an example of my CSP. You can see it is nicely formatted for maintainability but when being sent in the Headers it has unwanted whitespace.

Header set Content-Security-Policy "\
;default-src\
    'self'\
;child-src\
    'self'\
;connect-src\
    'self'\
;font-src\
    'self'\
    https://*.gstatic.com\
;form-action\
    'self';\
;frame-ancestors\
    'self'\
;frame-src\
    'self'\
;img-src\
    'self'\
    https://www.google.com/s2/favicons\
    https://www.google-analytics.com\
    https://*.gstatic.com\
    https://*.googleapis.com\
;object-src\
    'none'\
;script-src\
    'self'\
    'unsafe-eval'\
    https://www.google-analytics.com\
    https://*.googleapis.com\
;style-src\
    'self'\
    'unsafe-inline'\
    https://*.googleapis.com\
;\
"
Juanitajuanne answered 27/7, 2018 at 10:58 Comment(3)
Do you mean for a specific directive?Ami
@Ami its more of a syntax question, but im my case im setting a very large header (CSP)Juanitajuanne
thanks for the syntax ;-) prefixing with ; and the extra line-breaks +1Pianette
E
3

I think you can declare variable like this : https://mcmap.net/q/269650/-how-to-define-a-variable-in-apache-39-s-httpd-conf-file
Then, you can easily concatenate your variables

Eurhythmic answered 31/7, 2018 at 10:31 Comment(1)
Thanks for your answer, however it doesn't really directly concatenate strings. I've updated my question to give some context.Juanitajuanne
P
0
OIDCScope "profile email openid offline_access"
OIDCRemoteUserClaim  sub

<Location "/app2">
    AuthType openid-connect
    Require valid-user
    ProxyPass   "http://192.168.10.237/myapp"
    ProxyPassReverse  "http://192.168.10.237/myapp"

    RewriteEngine On
    RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER}] 
    RequestHeader set REMOTE_USER  %{PROXY_USER}e
</Location>

</VirtualHost>
Parette answered 31/7, 2018 at 10:31 Comment(1)
Thanks and welcome to SO, could you care to elaborate on how this answers the question?Juanitajuanne
R
0

My best guess is to use either setenv or define directives to declare and define values and then simply refer it using ${VAR} syntax as noted here.

In principle, the below should work.

$httpd -DTestCaseOne

...

<IfDefine TestCaseOne>
  Define dir_name_one media
  Define dir_name_two images
</IfDefine>

...

Include "/etc/httpd/sites/${dir_name_one}-${dir_name_two}/env.conf"

The server version for above references is - Apache 2.4

Raoul answered 6/8, 2018 at 19:25 Comment(0)
G
0

You can try like this:

set $text = 'text';  
set $additional "${text}additional";
Gibbsite answered 7/8, 2018 at 9:54 Comment(1)
Thanks for your answer, however it doesn't really directly concatenate strings. I've updated my question to give some context.Juanitajuanne
P
0

So far the best I managed is to post-edit* the Header

Note the extra space before the \ new-line char

Header set Content-Security-Policy "\
;default-src \
    'self' \
;child-src \
    'self' \
;connect-src \
    'self' \
"

# Remove unwanted whitespaces
Header edit* Content-Security-Policy "\s\s\s\s" ""

not 100% great, a bit expensive and could back-fire a bit, but well this is a way to do it

Pianette answered 1/2, 2021 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.