Skip to content

Commit bc5e59c

Browse files
authored
Merge pull request zyedidia#3618 from Neko-Box-Coder/LockConfig
Removing the ability for plugins to modify settings.json and bindings.json. Adding an option to reject plugins to bind keys.
2 parents 9183fbe + 4f1d2bb commit bc5e59c

File tree

6 files changed

+55
-21
lines changed

6 files changed

+55
-21
lines changed

cmd/micro/initlua.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func luaImportMicroConfig() *lua.LTable {
7373
ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete))
7474
ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete))
7575
ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil))
76-
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKey))
76+
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
7777
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
7878
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
7979
ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory))
@@ -88,8 +88,8 @@ func luaImportMicroConfig() *lua.LTable {
8888
ulua.L.SetField(pkg, "RegisterCommonOption", luar.New(ulua.L, config.RegisterCommonOptionPlug))
8989
ulua.L.SetField(pkg, "RegisterGlobalOption", luar.New(ulua.L, config.RegisterGlobalOptionPlug))
9090
ulua.L.SetField(pkg, "GetGlobalOption", luar.New(ulua.L, config.GetGlobalOption))
91-
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOption))
92-
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNative))
91+
ulua.L.SetField(pkg, "SetGlobalOption", luar.New(ulua.L, action.SetGlobalOptionPlug))
92+
ulua.L.SetField(pkg, "SetGlobalOptionNative", luar.New(ulua.L, action.SetGlobalOptionNativePlug))
9393
ulua.L.SetField(pkg, "ConfigDir", luar.New(ulua.L, config.ConfigDir))
9494

9595
return pkg

internal/action/bindings.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,18 @@ func eventsEqual(e1 Event, e2 Event) bool {
261261
return e1 == e2
262262
}
263263

264+
// TryBindKeyPlug tries to bind a key for the plugin without writing to bindings.json.
265+
// This operation can be rejected by lockbindings to prevent unexpected actions by the user.
266+
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
267+
if l, ok := config.GlobalSettings["lockbindings"]; ok && l.(bool) {
268+
return false, errors.New("bindings is locked by the user")
269+
}
270+
return TryBindKey(k, v, overwrite, false)
271+
}
272+
264273
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
265-
// Returns true if the keybinding already existed and a possible error
266-
func TryBindKey(k, v string, overwrite bool) (bool, error) {
274+
// Returns true if the keybinding already existed or is binded successfully and a possible error
275+
func TryBindKey(k, v string, overwrite bool, writeToFile bool) (bool, error) {
267276
var e error
268277
var parsed map[string]any
269278

@@ -310,7 +319,12 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
310319

311320
txt, _ := json.MarshalIndent(parsed, "", " ")
312321
txt = append(txt, '\n')
313-
return true, writeFile(filename, txt)
322+
323+
if writeToFile {
324+
return true, writeFile(filename, txt)
325+
} else {
326+
return true, nil
327+
}
314328
}
315329
return false, e
316330
}

internal/action/command.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ func doSetGlobalOptionNative(option string, nativeValue any) error {
630630
return nil
631631
}
632632

633-
func SetGlobalOptionNative(option string, nativeValue any) error {
633+
func SetGlobalOptionNative(option string, nativeValue any, writeToFile bool) error {
634634
if err := config.OptionIsValid(option, nativeValue); err != nil {
635635
return err
636636
}
@@ -653,6 +653,10 @@ func SetGlobalOptionNative(option string, nativeValue any) error {
653653
delete(b.LocalSettings, option)
654654
}
655655

656+
if !writeToFile {
657+
return nil
658+
}
659+
656660
err := config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json"))
657661
if err != nil {
658662
if errors.Is(err, util.ErrOverwrite) {
@@ -665,7 +669,7 @@ func SetGlobalOptionNative(option string, nativeValue any) error {
665669
return nil
666670
}
667671

668-
func SetGlobalOption(option, value string) error {
672+
func SetGlobalOption(option, value string, writeToFile bool) error {
669673
if _, ok := config.GlobalSettings[option]; !ok {
670674
return config.ErrInvalidOption
671675
}
@@ -675,7 +679,15 @@ func SetGlobalOption(option, value string) error {
675679
return err
676680
}
677681

678-
return SetGlobalOptionNative(option, nativeValue)
682+
return SetGlobalOptionNative(option, nativeValue, writeToFile)
683+
}
684+
685+
func SetGlobalOptionNativePlug(option string, nativeValue any) error {
686+
return SetGlobalOptionNative(option, nativeValue, false)
687+
}
688+
689+
func SetGlobalOptionPlug(option, value string) error {
690+
return SetGlobalOption(option, value, false)
679691
}
680692

681693
// ResetCmd resets a setting to its default value
@@ -689,7 +701,7 @@ func (h *BufPane) ResetCmd(args []string) {
689701
defaults := config.DefaultAllSettings()
690702

691703
if _, ok := defaults[option]; ok {
692-
SetGlobalOptionNative(option, defaults[option])
704+
SetGlobalOptionNative(option, defaults[option], true)
693705
return
694706
}
695707
InfoBar.Error(config.ErrInvalidOption)
@@ -705,7 +717,7 @@ func (h *BufPane) SetCmd(args []string) {
705717
option := args[0]
706718
value := args[1]
707719

708-
err := SetGlobalOption(option, value)
720+
err := SetGlobalOption(option, value, true)
709721
if err == config.ErrInvalidOption {
710722
err := h.Buf.SetOption(option, value)
711723
if err != nil {
@@ -761,7 +773,7 @@ func (h *BufPane) toggleOption(option string, local bool) error {
761773
return err
762774
}
763775
} else {
764-
if err := SetGlobalOptionNative(option, newVal); err != nil {
776+
if err := SetGlobalOptionNative(option, newVal, true); err != nil {
765777
return err
766778
}
767779
}
@@ -844,7 +856,7 @@ func (h *BufPane) BindCmd(args []string) {
844856
return
845857
}
846858

847-
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true)
859+
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true)
848860
if err != nil {
849861
if errors.Is(err, util.ErrOverwrite) {
850862
screen.TermMessage(err)

internal/config/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ var DefaultGlobalOnlySettings = map[string]any{
119119
"helpsplit": "hsplit",
120120
"infobar": true,
121121
"keymenu": false,
122+
"lockbindings": false,
122123
"mouse": true,
123124
"multiopen": "tab",
124125
"parsecursor": false,

runtime/help/options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ Here are the available options:
227227

228228
default value: `false`
229229

230+
* `lockbindings`: prevent plugins and lua scripts from binding any keys.
231+
Any custom actions must be binded manually either via commands like `bind`
232+
or by modifying the `bindings.json` file.
233+
234+
default value: `false`
235+
230236
* `matchbrace`: show matching braces for '()', '{}', '[]' when the cursor
231237
is on a brace character or (if `matchbraceleft` is enabled) next to it.
232238

runtime/help/plugins.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ The packages and their contents are listed below (in Go type signatures):
174174
values afterwards
175175
- `NoComplete`: no autocompletion suggestions
176176

177-
- `TryBindKey(k, v string, overwrite bool) (bool, error)`: bind the key
178-
`k` to the string `v` in the `bindings.json` file. If `overwrite` is
179-
true, this will overwrite any existing binding to key `k`. Returns true
180-
if the binding was made, and a possible error (for example writing to
181-
`bindings.json` can cause an error).
177+
- `TryBindKey(k, v string, overwrite bool) (bool, error)`:
178+
bind the key `k` to the string `v`. If `overwrite` is true, this will
179+
overwrite any existing binding to key `k`.
180+
Returns true if the binding was made, and a possible error.
181+
This operation can be rejected by `lockbindings` to prevent undesired
182+
actions by the user.
182183

183184
- `Reload()`: reload configuration files.
184185

@@ -224,9 +225,9 @@ The packages and their contents are listed below (in Go type signatures):
224225
given plugin in the `GlobalSettings` map.
225226

226227
- `SetGlobalOption(option, value string) error`: sets an option to a
227-
given value. Same as using the `> set` command. This will try to convert
228-
the value into the proper type for the option. Can return an error if the
229-
option name is not valid, or the value can not be converted.
228+
given value. This will try to convert the value into the proper
229+
type for the option. Can return an error if the option name is not
230+
valid, or the value can not be converted.
230231

231232
- `SetGlobalOptionNative(option string, value any) error`: sets
232233
an option to a given value, where the type of value is the actual

0 commit comments

Comments
 (0)