Skip to content

Commit

Permalink
Implement safe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Apr 26, 2024
1 parent 777e37e commit 3bf865d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
5 changes: 2 additions & 3 deletions jsonte/functions/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package functions
import (
"fmt"
"github.com/Bedrock-OSS/go-burrito/burrito"
"io/ioutil"
"os"
"strings"
)
Expand Down Expand Up @@ -46,7 +45,7 @@ func GenerateDocs() error {
if err != nil {
return burrito.WrapErrorf(err, "Failed to create %s directory", group.Name)
}
err = ioutil.WriteFile(fmt.Sprintf("%s-functions/index.md", group.Name), []byte(fmt.Sprintf(`---
err = os.WriteFile(fmt.Sprintf("%s-functions/index.md", group.Name), []byte(fmt.Sprintf(`---
layout: page
title: %[1]s
parent: JSON Templating Engine
Expand All @@ -69,7 +68,7 @@ has_children: true
if fn.IsUnsafe {
summary += "\n\n**This method is marked as unsafe. It can be disabled in certain environments.**"
}
err = ioutil.WriteFile(fmt.Sprintf("%s-functions/%s.md", group.Name, fn.Name), []byte(fmt.Sprintf(`---
err = os.WriteFile(fmt.Sprintf("%s-functions/%s.md", group.Name, fn.Name), []byte(fmt.Sprintf(`---
layout: page
grand_parent: JSON Templating Engine
parent: %[2]s
Expand Down
5 changes: 5 additions & 0 deletions jsonte/functions/function_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var initialized = false
var cacheAll = false
var cacheAllBucket = "all-cache"

var SafeMode = false

func Init() {
if !initialized {
RegisterMathFunctions()
Expand Down Expand Up @@ -207,6 +209,9 @@ func callFunctionImpl(name string, fns []JsonFunction, args []types.JsonType, re
return nil, burrito.WrapErrorf(nil, "Ambiguous function call for \"%s\". Matched: %s", name, strings.Join(matched, ", "))
} else {
fn := matching[0]
if SafeMode && fn.IsUnsafe {
return nil, burrito.WrappedErrorf("Function \"%s\" is marked as unsafe and is disabled in safe mode", name)
}
key := ""
if cacheAll {
keyObject := types.AsObject(utils.ToNavigableMap(
Expand Down
2 changes: 1 addition & 1 deletion jsonte/functions/minecraft_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func listLatestFiles(p string, m utils.NavigableMap[string, string], version *ty
result := map[string]string{}
keys := m.Keys()
for i := len(keys) - 1; i >= 0; i-- {
if !version.IsEmpty() {
if version != nil && !version.IsEmpty() {
ver, err := types.ParseSemverString(keys[i])
if err != nil {
continue
Expand Down
5 changes: 4 additions & 1 deletion jsonte/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
)

// LibraryInit initializes jsonte in library mode
func LibraryInit(level zapcore.Level) {
// level is the logging level
// safeMode is whether to run in safe mode, which disables unsafe functions
func LibraryInit(level zapcore.Level, safeMode bool) {
utils.InitLogging(level)
color.NoColor = true
types.Init()
functions.Init()
functions.SafeMode = safeMode
}

// FetchCache caches the vanilla packs
Expand Down

0 comments on commit 3bf865d

Please sign in to comment.