Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safeguard around js caching api #1146

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]
### Changed
- JS localcachePut now only accepts primitive types, other values will throw an error.

## [3.19.0] 2023-11-11
### Added
Expand Down
10 changes: 9 additions & 1 deletion server/runtime_javascript_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -8004,7 +8004,15 @@ func (n *runtimeJavascriptNakamaModule) localcachePut(r *goja.Runtime) func(goja
panic(r.NewTypeError("ttl must be 0 or more"))
}

n.localCache.Put(key, value.Export(), ttl)
v := value.Export()

switch v.(type) {
case string, int64, float64, bool:
default:
panic(r.NewTypeError("unsupported value type: must be string, numeric or boolean"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance this would cause panics in existing modules that use types outside of this list?
I.e. would it make sense to issue a warning/error instead of panicking to prevent breaking backward compatibility?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goja types that are passed by reference (e.g.: a JS object) carry with them internally a reference to the original VM where they were instantiated, if these objects are manipulated concurrently it can cause the VM to panic.

}

n.localCache.Put(key, v, ttl)

return goja.Undefined()
}
Expand Down