Skip to content

Commit

Permalink
feat(wasm) disallow some characters from module names
Browse files Browse the repository at this point in the history
We will use `:` for Proxy-Wasm metrics prefix separator.

Also disallow spaces and tabs.
  • Loading branch information
casimiro authored and thibaultcha committed Oct 29, 2024
1 parent bee8b84 commit 3434919
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/DIRECTIVES.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ module
Load a Wasm module from disk.

- `name` is expected to be unique since it will be used to refer to this module.
It also cannot contain tabs, spaces, or colons.
- `path` must point to a bytecode file whose format is `.wasm` (binary) or
`.wat` (text).
- `config` is an optional configuration string passed to `on_vm_start` when
Expand Down
14 changes: 14 additions & 0 deletions src/wasm/ngx_wasm_directives.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ ngx_wasm_core_shm_generic_directive(ngx_conf_t *cf, ngx_command_t *cmd,
char *
ngx_wasm_core_module_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
size_t i;
ngx_int_t rc;
ngx_str_t *value, *name, *path;
ngx_str_t *config = NULL;
Expand All @@ -272,6 +273,19 @@ ngx_wasm_core_module_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

for (i = 0; i < name->len; i++) {
if (name->data[i] == ' '
|| name->data[i] == '\t'
|| name->data[i] == ':')
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"[wasm] invalid module name \"%V\":"
" character not allowed \"%c\"",
name, name->data[i]);
return NGX_CONF_ERROR;
}
}

if (!path->len) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"[wasm] invalid module path \"%V\"", path);
Expand Down
66 changes: 54 additions & 12 deletions t/01-wasm/directives/001-module_directive.t
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ qr/\[emerg\] .*? invalid number of arguments in "module" directive/



=== TEST 5: module directive - bad name
=== TEST 5: module directive - empty name
--- main_config
wasm {
module '' $TEST_NGINX_HTML_DIR/a.wat;
Expand All @@ -93,7 +93,49 @@ qr/\[emerg\] .*? invalid module name ""/



=== TEST 6: module directive - bad path
=== TEST 6: module directive - bad name, space
--- main_config
wasm {
module 'bad name' $TEST_NGINX_HTML_DIR/a.wat;
}
--- error_log eval
qr/\[emerg\] .*? invalid module name "bad name": character not allowed " "/
--- no_error_log
[error]
[crit]
--- must_die



=== TEST 7: module directive - bad name, tab
--- main_config
wasm {
module 'bad\tname' $TEST_NGINX_HTML_DIR/a.wat;
}
--- error_log eval
qr/\[emerg\] .*? invalid module name "bad\tname": character not allowed "\t"/
--- no_error_log
[error]
[crit]
--- must_die



=== TEST 8: module directive - bad name, colon
--- main_config
wasm {
module 'bad:name' $TEST_NGINX_HTML_DIR/a.wat;
}
--- error_log eval
qr/\[emerg\] .*? invalid module name "bad:name": character not allowed ":"/
--- no_error_log
[error]
[crit]
--- must_die



=== TEST 9: module directive - bad path
--- main_config
wasm {
module a '';
Expand All @@ -107,7 +149,7 @@ qr/\[emerg\] .*? invalid module path ""/



=== TEST 7: module directive - already defined
=== TEST 10: module directive - already defined
--- main_config
wasm {
module a $TEST_NGINX_HTML_DIR/a.wat;
Expand All @@ -125,7 +167,7 @@ qr/\[emerg\] .*? "a" module already defined/



=== TEST 8: module directive - no such path
=== TEST 11: module directive - no such path
--- main_config
wasm {
module a $TEST_NGINX_HTML_DIR/none.wat;
Expand All @@ -139,7 +181,7 @@ qr/\[emerg\] .*? \[wasm\] open\(\) ".*?none\.wat" failed \(2: No such file or di



=== TEST 9: module directive - no .wat bytes - wasmtime, wasmer
=== TEST 12: module directive - no .wat bytes - wasmtime, wasmer
--- skip_eval: 4: !( $::nginxV =~ m/wasmtime/ || $::nginxV =~ m/wasmer/ )
--- main_config
wasm {
Expand All @@ -158,7 +200,7 @@ qr/\[emerg\] .*? \[wasm\] open\(\) ".*?none\.wat" failed \(2: No such file or di



=== TEST 10: module directive - no .wat bytes - v8
=== TEST 13: module directive - no .wat bytes - v8
--- skip_eval: 4: $::nginxV !~ m/v8/
--- main_config
wasm {
Expand All @@ -177,7 +219,7 @@ qr/\[emerg\] .*? \[wasm\] open\(\) ".*?none\.wat" failed \(2: No such file or di



=== TEST 11: module directive - no .wasm bytes
=== TEST 14: module directive - no .wasm bytes
--- main_config
wasm {
module a $TEST_NGINX_HTML_DIR/a.wasm;
Expand All @@ -195,7 +237,7 @@ qr/\[emerg\] .*? \[wasm\] open\(\) ".*?none\.wat" failed \(2: No such file or di



=== TEST 12: module directive - invalid .wat module - wasmtime, wasmer
=== TEST 15: module directive - invalid .wat module - wasmtime, wasmer
--- skip_eval: 4: !( $::nginxV =~ m/wasmtime/ || $::nginxV =~ m/wasmer/ )
--- main_config
wasm {
Expand All @@ -217,7 +259,7 @@ qr/\[emerg\] .*? \[wasm\] open\(\) ".*?none\.wat" failed \(2: No such file or di



=== TEST 13: module directive - invalid .wat module - v8
=== TEST 16: module directive - invalid .wat module - v8
--- skip_eval: 4: $::nginxV !~ m/v8/
--- main_config
wasm {
Expand All @@ -239,7 +281,7 @@ qr/\[emerg\] .*? \[wasm\] open\(\) ".*?none\.wat" failed \(2: No such file or di



=== TEST 14: module directive - invalid module (NYI import types)
=== TEST 17: module directive - invalid module (NYI import types)
'daemon off' must be set to check exit_code is 2
Valgrind mode already writes 'daemon off'
HUP mode does not catch the worker exit_code
Expand All @@ -264,7 +306,7 @@ qr/\[alert\] .*? \[wasm\] NYI: module import type not supported/



=== TEST 15: module directive - invalid module (missing host function in env)
=== TEST 18: module directive - invalid module (missing host function in env)
'daemon off' must be set to check exit_code is 2
Valgrind mode already writes 'daemon off'
HUP mode does not catch the worker exit_code
Expand All @@ -291,7 +333,7 @@ qq{



=== TEST 16: module directive - invalid module (missing WASI function)
=== TEST 19: module directive - invalid module (missing WASI function)
'daemon off' must be set to check exit_code is 2
Valgrind mode already writes 'daemon off'
HUP mode does not catch the worker exit_code
Expand Down

0 comments on commit 3434919

Please sign in to comment.