QuickJS: disabling eval() and Function() in qjs_new_context().
This properly disables eval() after previous attempt in c773ebcaad
(0.8.5). In QuickJS buint-in C level eval API, which is used by njs, is
linked to eval() in JS code. To disable only the JS function
manual modification of global object is required.
Thomas P. [Wed, 7 Aug 2024 09:47:08 +0000 (11:47 +0200)]
Modules: added nocache flag for js_set variables.
This commit adds support for an additional `nocache` flag in `js_set`
directives. If set, the resulting nginx variable will have no_cacheable set
to 1. This enables us to dynamically recompute a variable if the context
changed (for example, in case of an internal redirection).
In case of multiple calls in a location, users should cache the result in a
rewrite variable: `set $cached_variable $js_variable;`
Elijah Zupancic [Tue, 20 Aug 2024 18:41:44 +0000 (11:41 -0700)]
Add badges to README.md
This change adds two badges indicating the current project status and level of
support offered. These badges are standardized across many nginx projects.
Dmitry Volyntsev [Fri, 21 Jun 2024 00:11:24 +0000 (17:11 -0700)]
Fixed maybe-uninitialized warning in error creation.
Ensuring that buf is always initialized in njs_throw_error_va()
and njs_error_fmt_new(), by requiring fmt to always be non NULL.
This fixes GCC warnings like:
169 | njs_unicode_decode_t ctx;
| ^
In function ‘njs_utf8_length’,
inlined from ‘njs_error_new’ at src/njs_error.c:39:14,
inlined from ‘njs_throw_error_va’ at src/njs_error.c:69:5:
src/njs_utf8.h:141:12: error: ‘buf’ may be used uninitialized
[-Werror=maybe-uninitialized]
141 | return njs_utf8_stream_length(&ctx, p, len, 1, 1, NULL);
Dmitry Volyntsev [Sat, 22 Jun 2024 00:58:32 +0000 (17:58 -0700)]
Fixed ‘ctx.codepoint’ may be used uninitialized.
When building by GCC 13 with -O3 and -flto flags the following
warning was reported:
In function ‘njs_utf8_decode’,
inlined from ‘njs_text_encoder_encode_into’ at
src/njs_encoding.c:214:14:
src/njs_utf8.c:191:42: error: ‘ctx.codepoint’ may be used
uninitialized [-Werror=maybe-uninitialized]
191 | ctx->codepoint = (ctx->codepoint << 6) | (c & 0x3F);
Dmitry Volyntsev [Fri, 21 Jun 2024 00:26:14 +0000 (17:26 -0700)]
Fixed ‘length’ may be used uninitialized in Array.prototype.pop().
When building by GCC with -O3 and -flto flags the following
warning was reported:
src/njs_array.c: In function ‘njs_array_prototype_pop’:
src/njs_array.c:1009:8: error: ‘length’ may be used uninitialized in
this function [-Werror=maybe-uninitialized]
1009 | if (length == 0) {
| ^
Returning a specific code in njs_value_to_number() helps GCC
to infer that there are only 2 return values are possible and
both of them are handled.
Dmitry Volyntsev [Fri, 31 May 2024 05:22:48 +0000 (22:22 -0700)]
HTTP: fixed r.subrequest() error handling.
Previously, when at least 2 subrequests were scheduled they both
succeed, but the callback for the second threw an exception
heap-use-after-free happened: a nested chain of
ngx_http_run_posted_requests() calls and terminating request in the
inner call left outer calls with already freed request pointer.
The optional timeout argument overrides the timeout specified with
the shared_dict_zone directive for the effected key and operation
only. The timeout is specified in milliseconds.
This is useful when the majority of keys are expected to require
unique timeouts.
Dmitry Volyntsev [Thu, 30 May 2024 05:23:55 +0000 (22:23 -0700)]
Test262: fixed flaky fs tests.
Previously, two tests running in parallel could occasionally generate
identical file names because Math.random() was used for part of the file
name, leading to one of the tests failing.
The fix is to use a single global counter to generate file names,
ensuring deterministic and unique file names for each test.
Dmitry Volyntsev [Fri, 24 May 2024 05:50:34 +0000 (22:50 -0700)]
Fetch: fixed heap-buffer-overflow in Headers.get().
Previously, when more than one header with the same name added to a
Headers object and Headers.get() was used to get the the duplicate
header heap-buffer-overflow occured. The overflow occurred due to an
incorrect calculation of the combined header value's length.
The issue was introduced in c43261bad627 (0.7.10).
Dmitry Volyntsev [Thu, 23 May 2024 06:08:15 +0000 (23:08 -0700)]
Fixed retval handling after an exception.
Previously, some functions set a retval too early. If this happened
before an exception a partially created object in inconsistent state
may be visible outside the affected functions.
The following functions were fixed:
Object.prototype.valueOf()
Array.prototype.toSpliced()
Array.prototype.toReversed()
Array.prototype.toSorted()
Dmitry Volyntsev [Mon, 20 May 2024 23:44:10 +0000 (16:44 -0700)]
HTTP: fixed handling of 0 length request body.
Previously, when r.requestBuffer was passed as a body argument to
ngx.fetch() or r.subrequest() then exception was thrown "Error: invalid
Request body" when the request body had 0 length.
Dmitry Volyntsev [Sat, 18 May 2024 04:54:50 +0000 (21:54 -0700)]
Change: removed byte strings API.
These functions are unsafe because they produce byte strings.
Byte strings may not work as expected with the existing JS methods.
The following functions were removed:
- njs_vm_value_string_set() use njs_vm_value_string_create() as a
drop-in replacement.
- njs_vm_value_string_alloc() use njs_chb_t and
njs_vm_value_string_create_chb() instead.
Previously, the function might fail to return the last part of the
compressed content. This problem is more visible when output size > 1024
or when chunkSize < the content size.
Modules: improved checking for duplicate js_set variables.
Since 6fb1aca4eeaf (0.8.4) the identical js_set variables introduced as
a part of an include file that is shared amongst multiple vhosts are
rejected during configuration parsing.
The patch ignores duplicate js_set variables when they refer to the same
JS function.
Make the test more robust against changes in nginx, specifically cf890df37bb6 (Stream: socket peek in preread phase).
The filter callbacks may be called multiple times by nginx and the exact
number is not specified. The new test avoids relying on the exact number
of calls from nginx.
Tests: adapt stream_js_preload_object.t to nginx changes.
Make the test more robust against changes in nginx, specifically cf890df37bb6 (Stream: socket peek in preread phase).
The filter callbacks may be called multiple times by nginx and the exact
number is not specified. The new test avoids relying on the exact number
of calls from nginx.