fix(builtin): guard against missing args in builtins#943
Draft
thevilledev wants to merge 1 commit intoexpr-lang:masterfrom
Draft
fix(builtin): guard against missing args in builtins#943thevilledev wants to merge 1 commit intoexpr-lang:masterfrom
thevilledev wants to merge 1 commit intoexpr-lang:masterfrom
Conversation
The type checker skips argument validation for calls where the callee has unknown type (e.g. method calls on any-typed values). This allowed builtins like join() to reach runtime with zero arguments, panicking on args[0] access. Add runtime len(args) guards to a number of builtin functions that previously accessed args elements without bounds checking. Also fix date() to check len(args) a second time after stripping a leading *time.Location argument. Relates to OSS-Fuzz finding in expr-lang#941. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Relates to #941.
OSS-Fuzz finding has a fuzzed expression which calls
join()with zero arguments inside a method call on an any-typed value (e.g.fn().N(join())). The type checker'scallNode()skips argument validation when the callee resolves to an unknown type, so the invalid call passes compilation. At runtimejoin()accessesargs[0]on an empty slice, producing a Go runtime panic instead of a clean error.I found similar issues from other builtins, relying solely on compile-time validation that can be bypassed.
Changes
Add runtime
len(args)guards to a number of builtin functions. Each now returns a descriptive error.Added the new error format to fuzzing harness allowlist (
not enough arguments to call x).I also removed some autogenerated test lines that called
firstandlastwith wrong arity. These tests were passing because ofdefer recover()silently swallowing the panic.Further comments
I think there is a chance to fix this in the checker, to provide compile-time rejection. But I guess it'll be quite complicated because of optional chaining on unknown types for a number of different expressions. This fix is the conservative kind just to have clean and descriptive runtime errors.