Files
Reims-Go/internal/app/plugin_bridge_files.go
T
NwflowerandClaude Opus 5.5 fa3e1e2889 feat: add plugin views and expand the sandboxed page bridge
Plugins can add pages to the sidebar, VM tabs, Settings and dialogs. Their
pages can now use host forms and navigation, VM and settings access,
authorized file I/O, task progress, controlled HTTP requests, events and
private storage.

Plugin pages cannot change allow_ai_access, which stays the user's per-VM
consent. Closing a page cancels its unfinished tasks, and running plugin
tasks no longer block changing the data folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 23:11:23 -07:00

223 lines
5.9 KiB
Go

package app
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/Nwflower/reims-go/internal/plugin"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
const maxPanelFileBytes = 8 << 20
type pluginFileGrant struct {
PluginID string
Path string
Write bool
}
func (a *App) issuePluginFileGrant(pluginID, path string, write bool) (map[string]any, error) {
if strings.TrimSpace(path) == "" {
return map[string]any{"canceled": true}, nil
}
clean, err := filepath.Abs(path)
if err != nil {
return nil, err
}
var token [16]byte
if _, err := rand.Read(token[:]); err != nil {
return nil, err
}
id := hex.EncodeToString(token[:])
a.mu.Lock()
if a.pluginFiles == nil {
a.pluginFiles = make(map[string]pluginFileGrant)
}
a.pluginFiles[id] = pluginFileGrant{PluginID: pluginID, Path: clean, Write: write}
a.mu.Unlock()
result := map[string]any{"id": id, "name": filepath.Base(clean), "write": write}
if info, err := os.Stat(clean); err == nil {
result["size"] = info.Size()
} else if !write {
a.mu.Lock()
delete(a.pluginFiles, id)
a.mu.Unlock()
return nil, err
}
return result, nil
}
func (a *App) revokePluginFiles(pluginID string) {
a.mu.Lock()
defer a.mu.Unlock()
for id, grant := range a.pluginFiles {
if grant.PluginID == pluginID {
delete(a.pluginFiles, id)
}
}
}
func (a *App) pluginFilePath(pluginID, id string, write bool) (string, error) {
a.mu.Lock()
grant, ok := a.pluginFiles[id]
a.mu.Unlock()
if !ok || grant.PluginID != pluginID || (write && !grant.Write) {
return "", errors.New("file handle is not available to this plugin")
}
return grant.Path, nil
}
func panelFileEncoding(params map[string]any) (string, error) {
encoding, err := panelTextParam(params, "encoding", false, 16, false)
if err != nil {
return "", err
}
switch encoding {
case "", "utf8", "base64":
return encoding, nil
default:
return "", fmt.Errorf("unsupported encoding %q", encoding)
}
}
func (a *App) panelFileCall(mgr *plugin.Manager, pluginID, method string, params map[string]any) (any, error) {
if err := mgr.Broker().Authorize(pluginID, "fs:pick"); err != nil {
return nil, err
}
switch method {
case "file.pick", "file.save":
if a.ctx == nil {
return nil, errors.New("window context not ready")
}
title, err := panelTextParam(params, "title", false, 200, false)
if err != nil {
return nil, err
}
pattern, err := panelTextParam(params, "pattern", false, 200, false)
if err != nil {
return nil, err
}
if method == "file.pick" {
path, err := a.PickFile(title, pattern)
if err != nil {
return nil, err
}
return a.issuePluginFileGrant(pluginID, path, false)
}
defaultFilename, err := panelTextParam(params, "defaultFilename", false, 255, false)
if err != nil {
return nil, err
}
if defaultFilename != "" && (filepath.Base(defaultFilename) != defaultFilename || defaultFilename == "." || defaultFilename == "..") {
return nil, errors.New("defaultFilename must be a file name")
}
opts := wailsRuntime.SaveDialogOptions{Title: title, DefaultFilename: defaultFilename, CanCreateDirectories: true}
if pattern != "" {
opts.Filters = []wailsRuntime.FileFilter{{DisplayName: pattern, Pattern: pattern}}
}
path, err := wailsRuntime.SaveFileDialog(a.ctx, opts)
if err != nil {
return nil, err
}
return a.issuePluginFileGrant(pluginID, path, true)
case "file.read":
id, err := panelTextParam(params, "id", true, 64, false)
if err != nil {
return nil, err
}
path, err := a.pluginFilePath(pluginID, id, false)
if err != nil {
return nil, err
}
encoding, err := panelFileEncoding(params)
if err != nil {
return nil, err
}
info, err := os.Stat(path)
if err != nil {
return nil, err
}
if info.Size() > maxPanelFileBytes {
return nil, fmt.Errorf("file exceeds %d bytes", maxPanelFileBytes)
}
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
data, err := io.ReadAll(io.LimitReader(file, maxPanelFileBytes+1))
if err != nil {
return nil, err
}
if len(data) > maxPanelFileBytes {
return nil, fmt.Errorf("file exceeds %d bytes", maxPanelFileBytes)
}
result := map[string]any{"name": filepath.Base(path), "size": len(data)}
if encoding == "base64" {
result["encoding"] = "base64"
result["data"] = base64.StdEncoding.EncodeToString(data)
} else {
if !utf8.Valid(data) {
return nil, errors.New("file is not valid UTF-8")
}
result["encoding"] = "utf8"
result["data"] = string(data)
}
return result, nil
case "file.write":
id, err := panelTextParam(params, "id", true, 64, false)
if err != nil {
return nil, err
}
path, err := a.pluginFilePath(pluginID, id, true)
if err != nil {
return nil, err
}
encoding, err := panelFileEncoding(params)
if err != nil {
return nil, err
}
rawData, ok := params["data"].(string)
if !ok {
return nil, errors.New("data must be a string")
}
if encoding == "base64" && len(rawData) > (maxPanelFileBytes+2)/3*4 {
return nil, fmt.Errorf("encoded data exceeds %d bytes", maxPanelFileBytes)
}
if encoding != "base64" && len(rawData) > maxPanelFileBytes {
return nil, fmt.Errorf("data exceeds %d bytes", maxPanelFileBytes)
}
var data []byte
if encoding == "base64" {
data, err = base64.StdEncoding.DecodeString(rawData)
if err != nil {
return nil, fmt.Errorf("decode base64 data: %w", err)
}
} else {
data = []byte(rawData)
}
if len(data) > maxPanelFileBytes {
return nil, fmt.Errorf("data exceeds %d bytes", maxPanelFileBytes)
}
tmp := path + "." + id + ".reims-plugin-tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
return nil, err
}
if err := os.Rename(tmp, path); err != nil {
_ = os.Remove(tmp)
return nil, err
}
return map[string]any{"ok": true, "name": filepath.Base(path), "size": len(data)}, nil
default:
return nil, fmt.Errorf("unsupported plugin file method %q", method)
}
}