Skip to content

Commit 3a63e6e

Browse files
committed
security(engine)!: restrict execution of halt/1
1 parent 7935f05 commit 3a63e6e

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The following customizations have been made to adapt the original `ichiban/prolo
4242
- Removed support for trigonometric functions (`sin`, `cos`, `tan`, `asin`, `acos`, `atan`).
4343
- Introduced VM hooks for enhanced Prolog execution control.
4444
- Added support for the `Dict` term.
45+
- `halt/0` and `halt/1` are forbidden and will throw an error.
4546

4647
## License
4748

engine/builtin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,9 @@ func PeekChar(vm *VM, streamOrAlias, char Term, k Cont, env *Env) *Promise {
19601960
}
19611961
}
19621962

1963-
var osExit = os.Exit
1963+
var osExit = func(_ int) {
1964+
panic("halt/1 is not allowed")
1965+
}
19641966

19651967
// Halt exits the process with exit code of n.
19661968
func Halt(_ *VM, n Term, k Cont, env *Env) *Promise {

engine/builtin_test.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestCall(t *testing.T) {
3838
panic(errors.New("told you"))
3939
})
4040
vm.Register0(NewAtom("do_not_call_exception"), func(*VM, Cont, *Env) *Promise {
41-
panic(Exception{NewAtom("error").Apply(NewAtom("panic_error"), NewAtom("told you"))})
41+
panic(Exception{NewAtom("error").Apply(NewAtom("panic_error").Apply(NewAtom("told you")))})
4242
})
4343
vm.Register0(NewAtom("do_not_call_misc_error"), func(*VM, Cont, *Env) *Promise {
4444
panic(42)
@@ -5527,20 +5527,11 @@ func TestPeekChar(t *testing.T) {
55275527

55285528
func Test_Halt(t *testing.T) {
55295529
t.Run("ok", func(t *testing.T) {
5530-
var exitCalled bool
5531-
osExit = func(code int) {
5532-
assert.Equal(t, 2, code)
5533-
exitCalled = true
5534-
}
5535-
defer func() {
5536-
osExit = os.Exit
5537-
}()
5538-
5539-
ok, err := Halt(nil, Integer(2), Success, nil).Force(context.Background())
5540-
assert.NoError(t, err)
5541-
assert.True(t, ok)
5542-
5543-
assert.True(t, exitCalled)
5530+
ok, err := Delay(func(ctx context.Context) *Promise {
5531+
return Halt(nil, Integer(2), Success, nil)
5532+
}).Force(context.Background())
5533+
assert.EqualError(t, err, "error(panic_error(halt/1 is not allowed))")
5534+
assert.False(t, ok)
55445535
})
55455536

55465537
t.Run("n is a variable", func(t *testing.T) {

0 commit comments

Comments
 (0)