1
0

j2cfg: provide almost sane escape filter

This commit is contained in:
Konstantin Demin 2024-11-08 12:10:06 +03:00
parent 99696ed5d9
commit 441ea2e64b
Signed by: krd
GPG Key ID: 4D56F87A8BA65FD0
19 changed files with 50 additions and 45 deletions

View File

@ -26,6 +26,6 @@ env {{ k }};
## ##
{%- for k in c_vars_override %} {%- for k in c_vars_override %}
{#- {%- set v = c_env[k] %} #} {#- {%- set v = c_env[k] %} #}
## env {{ k }}={{ c_env[k].__repr__() }}; ## env {{ k }}={{ c_env[k] | ngx_esc }};
{%- endfor %} {%- endfor %}
{%- endif %} {%- endif %}

View File

@ -7,6 +7,5 @@
{%- set proto = proto | re_sub('^v3$', 'h3=":443"; ma=3600') -%} {%- set proto = proto | re_sub('^v3$', 'h3=":443"; ma=3600') -%}
{#- main part -#} {#- main part -#}
{%- if proto %} {%- if proto %}
{#- TODO: precise quotation #} add_header Alt-Svc {{ (proto | join(', ')) | ngx_esc }};
add_header Alt-Svc {{ (proto | join(', ')).__repr__() }};
{%- endif %} {%- endif %}

View File

@ -11,7 +11,7 @@ map $http_user_agent
default $http_user_agent; default $http_user_agent;
{%- if env.NGX_HTTP_FAKE_UA %} {%- if env.NGX_HTTP_FAKE_UA %}
## merely fake ## merely fake
"" {{ env.NGX_HTTP_FAKE_UA.__repr__() }}; "" {{ env.NGX_HTTP_FAKE_UA | ngx_esc }};
{%- else %} {%- else %}
"" "Angie/$angie_version"; "" "Angie/$angie_version";
{%- endif %} {%- endif %}

View File

@ -1,15 +1,14 @@
{#- TODO: precise quotation -#}
{%- set cache_bypass = j2cfg.cache_bypass or [] -%} {%- set cache_bypass = j2cfg.cache_bypass or [] -%}
{%- if cache_bypass -%} {%- if cache_bypass -%}
## disable (response) cache under following conditions ## disable (response) cache under following conditions
fastcgi_cache_bypass fastcgi_cache_bypass
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
fastcgi_no_cache fastcgi_no_cache
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
{%- endif -%} {%- endif -%}

View File

@ -1,6 +1,5 @@
{%- for k, v in j2cfg.tls.conf_cmd.items() %} {%- for k, v in j2cfg.tls.conf_cmd.items() %}
{#- TODO: precise quotation #} grpc_ssl_conf_command {{ k }} {{ v | ngx_esc }};
grpc_ssl_conf_command {{ k }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}
grpc_ssl_trusted_certificate {{ env.NGX_SSL_CERT_FILE }}; grpc_ssl_trusted_certificate {{ env.NGX_SSL_CERT_FILE }};

View File

@ -15,12 +15,10 @@ ssl_stapling_verify on;
ssl_stapling_verify off; ssl_stapling_verify off;
{%- endif %} {%- endif %}
{%- if j2cfg.tls.stapling.file %} {%- if j2cfg.tls.stapling.file %}
{#- TODO: precise quotation #} ssl_stapling_file {{ j2cfg.tls.stapling.file | ngx_esc }};
ssl_stapling_file {{ j2cfg.tls.stapling.file.__repr__() }};
{%- endif %} {%- endif %}
{%- if j2cfg.tls.stapling.responder %} {%- if j2cfg.tls.stapling.responder %}
{#- TODO: precise quotation #} ssl_stapling_responder {{ j2cfg.tls.stapling.responder | ngx_esc }};
ssl_stapling_responder {{ j2cfg.tls.stapling.responder.__repr__() }};
{%- endif %} {%- endif %}
{%- else %} {%- else %}
ssl_stapling off; ssl_stapling off;

View File

@ -1,15 +1,14 @@
{#- TODO: precise quotation -#}
{%- set cache_bypass = j2cfg.cache_bypass or [] -%} {%- set cache_bypass = j2cfg.cache_bypass or [] -%}
{%- if cache_bypass -%} {%- if cache_bypass -%}
## disable (response) cache under following conditions ## disable (response) cache under following conditions
proxy_cache_bypass proxy_cache_bypass
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
proxy_no_cache proxy_no_cache
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
{%- endif -%} {%- endif -%}

View File

@ -1,6 +1,5 @@
{%- for k, v in j2cfg.tls.conf_cmd.items() %} {%- for k, v in j2cfg.tls.conf_cmd.items() %}
{#- TODO: precise quotation #} proxy_ssl_conf_command {{ k }} {{ v | ngx_esc }};
proxy_ssl_conf_command {{ k }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}
proxy_ssl_trusted_certificate {{ env.NGX_SSL_CERT_FILE }}; proxy_ssl_trusted_certificate {{ env.NGX_SSL_CERT_FILE }};

View File

@ -1,15 +1,14 @@
{#- TODO: precise quotation -#}
{%- set cache_bypass = j2cfg.cache_bypass or [] -%} {%- set cache_bypass = j2cfg.cache_bypass or [] -%}
{%- if cache_bypass -%} {%- if cache_bypass -%}
## disable (response) cache under following conditions ## disable (response) cache under following conditions
scgi_cache_bypass scgi_cache_bypass
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
scgi_no_cache scgi_no_cache
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
{%- endif -%} {%- endif -%}

View File

@ -1,4 +1,3 @@
{%- for k, v in j2cfg.tls.conf_cmd.items() %} {%- for k, v in j2cfg.tls.conf_cmd.items() %}
{#- TODO: precise quotation #} ssl_conf_command {{ k }} {{ v | ngx_esc }};
ssl_conf_command {{ k }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -1,15 +1,14 @@
{#- TODO: precise quotation -#}
{%- set cache_bypass = j2cfg.cache_bypass or [] -%} {%- set cache_bypass = j2cfg.cache_bypass or [] -%}
{%- if cache_bypass -%} {%- if cache_bypass -%}
## disable (response) cache under following conditions ## disable (response) cache under following conditions
uwsgi_cache_bypass uwsgi_cache_bypass
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
uwsgi_no_cache uwsgi_no_cache
{%- for v in cache_bypass %} {%- for v in cache_bypass %}
{{ v.__repr__() }} {{ v | ngx_esc }}
{%- endfor %} {%- endfor %}
; ;
{%- endif -%} {%- endif -%}

View File

@ -1,6 +1,5 @@
{%- for k, v in j2cfg.tls.conf_cmd.items() %} {%- for k, v in j2cfg.tls.conf_cmd.items() %}
{#- TODO: precise quotation #} uwsgi_ssl_conf_command {{ k }} {{ v | ngx_esc }};
uwsgi_ssl_conf_command {{ k }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}
uwsgi_ssl_trusted_certificate {{ env.NGX_SSL_CERT_FILE }}; uwsgi_ssl_trusted_certificate {{ env.NGX_SSL_CERT_FILE }};

View File

@ -1,7 +1,6 @@
## sourced by conf.d/fastcgi/headers.conf ## sourced by conf.d/fastcgi/headers.conf
## hide/remove request headers ## set/remove request headers
{%- set req_hdr_dict = j2cfg.request_headers or {} -%} {%- set req_hdr_dict = j2cfg.request_headers or {} -%}
{%- for h, v in req_hdr_dict.items() %} {%- for h, v in req_hdr_dict.items() %}
{#- TODO: precise quotation #} fastcgi_param {{ h | as_cgi_header }} {{ v | ngx_esc }};
fastcgi_param {{ h | as_cgi_header }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -1,7 +1,6 @@
## sourced by conf.d/grpc/headers.conf ## sourced by conf.d/grpc/headers.conf
## hide/remove request headers ## set/remove request headers
{%- set req_hdr_dict = j2cfg.request_headers or {} -%} {%- set req_hdr_dict = j2cfg.request_headers or {} -%}
{%- for h, v in req_hdr_dict.items() %} {%- for h, v in req_hdr_dict.items() %}
{#- TODO: precise quotation #} grpc_set_header {{ h }} {{ v | ngx_esc }};
grpc_set_header {{ h }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -2,6 +2,5 @@
## add response headers ## add response headers
{%- set resp_hdr_dict = j2cfg.response_headers or {} -%} {%- set resp_hdr_dict = j2cfg.response_headers or {} -%}
{%- for h, v in resp_hdr_dict.items() %} {%- for h, v in resp_hdr_dict.items() %}
{#- TODO: precise quotation #} add_header {{ h }} {{ v | ngx_esc }};
add_header {{ h }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -1,7 +1,6 @@
## sourced by conf.d/proxy-http/headers.conf ## sourced by conf.d/proxy-http/headers.conf
## hide/remove request headers ## set/remove request headers
{%- set req_hdr_dict = j2cfg.request_headers or {} -%} {%- set req_hdr_dict = j2cfg.request_headers or {} -%}
{%- for h, v in req_hdr_dict.items() %} {%- for h, v in req_hdr_dict.items() %}
{#- TODO: precise quotation #} proxy_set_header {{ h }} {{ v | ngx_esc }};
proxy_set_header {{ h }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -1,7 +1,6 @@
## sourced by conf.d/scgi/headers.conf ## sourced by conf.d/scgi/headers.conf
## hide/remove request headers ## set/remove request headers
{%- set req_hdr_dict = j2cfg.request_headers or {} -%} {%- set req_hdr_dict = j2cfg.request_headers or {} -%}
{%- for h, v in req_hdr_dict.items() %} {%- for h, v in req_hdr_dict.items() %}
{#- TODO: precise quotation #} scgi_param {{ h | as_cgi_header }} {{ v | ngx_esc }};
scgi_param {{ h | as_cgi_header }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -1,7 +1,6 @@
## sourced by conf.d/uwsgi/headers.conf ## sourced by conf.d/uwsgi/headers.conf
## hide/remove request headers ## set/remove request headers
{%- set req_hdr_dict = j2cfg.request_headers or {} -%} {%- set req_hdr_dict = j2cfg.request_headers or {} -%}
{%- for h, v in req_hdr_dict.items() %} {%- for h, v in req_hdr_dict.items() %}
{#- TODO: precise quotation #} uwsgi_param {{ h | as_cgi_header }} {{ v | ngx_esc }};
uwsgi_param {{ h | as_cgi_header }} {{ v.__repr__() }};
{%- endfor %} {%- endfor %}

View File

@ -240,6 +240,27 @@ def sh_like_file_to_list(j2env, file_in: str) -> list:
)) ))
def ngx_esc(x):
if isinstance(x, str):
if x == "":
return "''"
if re.search(r'(?:\s|[;{}()\[\]\\\'"])', x):
return repr(x)
return x
if is_sequence(x):
return uniq([
ngx_esc(v)
for v in x
])
if is_mapping(x):
return dict_remap_keys(
x, ngx_esc
)
if x is None:
return None
return ngx_esc(str(x))
def merge_dict_recurse(d1, d2: dict) -> dict: def merge_dict_recurse(d1, d2: dict) -> dict:
x = {} | d1 x = {} | d1
@ -290,6 +311,7 @@ J2CFG_FILTERS = [
is_sequence, is_sequence,
list_diff, list_diff,
list_intersect, list_intersect,
ngx_esc,
re_fullmatch, re_fullmatch,
re_fullmatch_negate, re_fullmatch_negate,
re_match, re_match,