Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): tighten file handling, backups and voice setup (#3881)

Hampuse625e1c011eb
diff --git a/fluxer_rexctl/cmd/backup.go b/fluxer_rexctl/cmd/backup.go
index f2435d39f..9fed190ea 100644
--- a/fluxer_rexctl/cmd/backup.go
+++ b/fluxer_rexctl/cmd/backup.go
@@ -276,10 +276,10 @@ func runBackupVerify(ctx context.Context, app *App, arg string) error {
return backupFileErr(path, err)
}
if m.WithoutUploads() {
- s.Done("The backup is complete. It has %s on %s without uploads, made %s.", m.Domain, fluxerName(m.Release), backupTime(m.CreatedAt))
+ s.Done("The backup is complete. It has %s on %s without uploads, made %s.", ui.Inline(m.Domain), fluxerName(ui.Inline(m.Release)), backupTime(m.CreatedAt))
return nil
}
- s.Done("The backup is complete. It has %s on %s, made %s.", m.Domain, fluxerName(m.Release), backupTime(m.CreatedAt))
+ s.Done("The backup is complete. It has %s on %s, made %s.", ui.Inline(m.Domain), fluxerName(ui.Inline(m.Release)), backupTime(m.CreatedAt))
return nil
})
}
diff --git a/fluxer_rexctl/cmd/bundle.go b/fluxer_rexctl/cmd/bundle.go
index 30e9fd584..9a8923a61 100644
--- a/fluxer_rexctl/cmd/bundle.go
+++ b/fluxer_rexctl/cmd/bundle.go
@@ -4,11 +4,11 @@ package cmd
import (
"fmt"
- "os"
"path/filepath"
"github.com/spf13/cobra"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
@@ -35,13 +35,13 @@ func newBundleCmd(app *App) *cobra.Command {
if err := stack.Bundle(out, images.Images); err != nil {
return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")
}
- if err := os.WriteFile(filepath.Join(out, ".env.example"), []byte(instance.BundleEnv()), 0o644); err != nil {
+ if err := envfile.WriteFileAtomic(filepath.Join(out, ".env.example"), []byte(instance.BundleEnv()), false); err != nil {
return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")
}
- if err := os.WriteFile(filepath.Join(out, "voice", "voice.env.example"), []byte(instance.VoiceBundleEnv()), 0o644); err != nil {
+ if err := envfile.WriteFileAtomic(filepath.Join(out, "voice", "voice.env.example"), []byte(instance.VoiceBundleEnv()), false); err != nil {
return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")
}
- if err := os.WriteFile(filepath.Join(out, "voice", "edge.env.example"), []byte(instance.VoiceEdgeBundleEnv()), 0o644); err != nil {
+ if err := envfile.WriteFileAtomic(filepath.Join(out, "voice", "edge.env.example"), []byte(instance.VoiceEdgeBundleEnv()), false); err != nil {
return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")
}
return nil
diff --git a/fluxer_rexctl/cmd/email.go b/fluxer_rexctl/cmd/email.go
index d3320862d..c7be83e01 100644
--- a/fluxer_rexctl/cmd/email.go
+++ b/fluxer_rexctl/cmd/email.go
@@ -97,7 +97,7 @@ func smtpReason(message string) string {
if certificateFailed(message) {
return "Your email server's certificate isn't trusted. It may be self-signed."
}
- return sentenceOf(message)
+ return sentenceOf(ui.Inline(message))
}
func newEmailCmd(app *App) *cobra.Command {
diff --git a/fluxer_rexctl/cmd/home.go b/fluxer_rexctl/cmd/home.go
index 3bb702935..ea91ad913 100644
--- a/fluxer_rexctl/cmd/home.go
+++ b/fluxer_rexctl/cmd/home.go
@@ -32,11 +32,11 @@ func runHome(_ context.Context, app *App, c *cobra.Command) error {
inst, err := instance.Find(dir)
switch {
case err == nil:
- homeGreet(app, fmt.Sprintf("Your instance is at %s.", inst.Origin()))
+ homeGreet(app, fmt.Sprintf("Your instance is at %s.", ui.Inline(inst.Origin())))
homeCommands(app, c, homeInstalled)
case errors.Is(err, instance.ErrLegacy):
- homeGreet(app, fmt.Sprintf("There's a Fluxer instance in %s.", dir))
- app.UI.Info("Run %s to take it over.", app.takeOverCommand())
+ homeGreet(app, fmt.Sprintf("There's a Fluxer instance in %s.", ui.Inline(dir)))
+ app.UI.Info("Run %s to take it over.", ui.Inline(app.takeOverCommand()))
app.UI.Info("")
homeCommands(app, c, homeLegacy)
default:
diff --git a/fluxer_rexctl/cmd/upgrade.go b/fluxer_rexctl/cmd/upgrade.go
index b7f46c30b..e15fe501e 100644
--- a/fluxer_rexctl/cmd/upgrade.go
+++ b/fluxer_rexctl/cmd/upgrade.go
@@ -543,7 +543,7 @@ func writeUpgradeText(inst *instance.Instance, to, text string) string {
if loggedThisAttempt(inst, to) {
flag = os.O_CREATE | os.O_WRONLY | os.O_APPEND
}
- f, err := os.OpenFile(path, flag, 0o600)
+ f, err := openStateLog(inst, upgradeLog(to), flag)
if err != nil {
return ""
}
@@ -554,6 +554,15 @@ func writeUpgradeText(inst *instance.Instance, to, text string) string {
return path
}
+func openStateLog(inst *instance.Instance, name string, flag int) (*os.File, error) {
+ root, err := os.OpenRoot(inst.Dir)
+ if err != nil {
+ return nil, err
+ }
+ defer root.Close()
+ return root.OpenFile(filepath.Join(instance.StateDirName, name), flag, 0o600)
+}
+
func printUpgradePlan(ctx context.Context, app *App, inst *instance.Instance, f *upgradeFlags, needSelf bool, from, version string) {
_, to := versionPair(from, version)
if needSelf {
@@ -1362,7 +1371,7 @@ func writeFailureLog(ctx context.Context, inst *instance.Instance, p *engine.Pro
}
}
path := inst.Path(instance.StateDirName, name)
- f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
+ f, err := openStateLog(inst, name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
if err != nil {
return ""
}
@@ -1386,11 +1395,15 @@ func writeBackupLog(inst *instance.Instance, name string, cause error) string {
if errors.As(cause, &dump) && dump.Output != "" {
text += "\n" + dump.Output + "\n"
}
- path := inst.Path(instance.StateDirName, name)
- if err := os.WriteFile(path, []byte(text), 0o600); err != nil {
+ f, err := openStateLog(inst, name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC)
+ if err != nil {
return ""
}
- return path
+ defer f.Close()
+ if _, err := f.WriteString(text); err != nil {
+ return ""
+ }
+ return inst.Path(instance.StateDirName, name)
}
func notRunning(ctx context.Context, p *engine.Project) []string {
diff --git a/fluxer_rexctl/cmd/voice.go b/fluxer_rexctl/cmd/voice.go
index 88f731744..69e9ede30 100644
--- a/fluxer_rexctl/cmd/voice.go
+++ b/fluxer_rexctl/cmd/voice.go
@@ -499,7 +499,7 @@ func testRemoteVoiceServer(c *cobra.Command, app *App, id string) error {
}
results = append(results, transportResult{Path: p, Err: err})
}
- app.UI.Info("Tested %s at %s from this machine.", id, endpoint)
+ app.UI.Info("Tested %s at %s from this machine.", id, ui.Inline(endpoint))
if !reportTransports(app, results, false) {
switch {
case slices.ContainsFunc(results, func(r transportResult) bool { return errors.Is(r.Err, voice.ErrTokenRejected) }):
diff --git a/fluxer_rexctl/cmd/voice_add.go b/fluxer_rexctl/cmd/voice_add.go
index b45d060d8..3e57f8ac9 100644
--- a/fluxer_rexctl/cmd/voice_add.go
+++ b/fluxer_rexctl/cmd/voice_add.go
@@ -335,6 +335,9 @@ func planVoiceAdd(ctx context.Context, app *App, e *engine.Engine, inst *instanc
for range f.Count {
id := f.ID
if id == "" {
+ if !voice.UsableRegion(region) {
+ return nil, ui.Refused(fmt.Sprintf("rexctl can't make a server ID from the region ID %q.", region), "Pass --id with a server ID.")
+ }
id = voice.NextID(region, ids)
} else if slices.Contains(ids, id) {
return nil, ui.Refused(fmt.Sprintf("A voice server or Compose project with the ID %s already exists.", id), "Pass --id with another ID.")
diff --git a/fluxer_rexctl/cmd/voice_join.go b/fluxer_rexctl/cmd/voice_join.go
index 8d8cecb00..6457f82cf 100644
--- a/fluxer_rexctl/cmd/voice_join.go
+++ b/fluxer_rexctl/cmd/voice_join.go
@@ -377,6 +377,28 @@ func planJoin(ctx context.Context, app *App, client *voice.Client, e *engine.Eng
}
func chooseJoinID(preview *voice.Preview, flagID string, inst *instance.Instance, taken []string) (string, error) {
+ id, err := pickJoinID(preview, flagID, inst, taken)
+ if err != nil {
+ return "", err
+ }
+ if err := checkJoinID(preview, id); err != nil {
+ return "", err
+ }
+ return id, nil
+}
+
+func checkJoinID(preview *voice.Preview, id string) error {
+ if id != voice.EdgeID && voice.ValidID(id) {
+ return nil
+ }
+ fix := "Pass --id with lowercase letters, digits and dashes."
+ if preview.ServerIDPinned {
+ fix = "Make a new token on the Fluxer machine."
+ }
+ return ui.Refused(fmt.Sprintf("rexctl can't use the server ID %q.", id), fix)
+}
+
+func pickJoinID(preview *voice.Preview, flagID string, inst *instance.Instance, taken []string) (string, error) {
id := preview.ServerID
switch {
case flagID != "" && preview.ServerIDPinned && flagID != id:
@@ -908,6 +930,9 @@ func joinWithRetries(ctx context.Context, app *App, client *voice.Client, e *eng
return nil, joinError(err, plan.domain, saved.ID)
}
next := voice.NextID(plan.preview.Region.ID, append(voiceIDsInUse(ctx, e, inst, voice.JoinedProject("")), saved.ID))
+ if err := checkJoinID(plan.preview, next); err != nil {
+ return nil, err
+ }
app.UI.Info("%s is taken on %s. Trying %s.", saved.ID, plan.domain, next)
if err := renameJoinedServer(ctx, app, e, inst, saved, next, plan); err != nil {
return nil, err
diff --git a/fluxer_rexctl/internal/backup/archive.go b/fluxer_rexctl/internal/backup/archive.go
index 70171fd32..38b21c55e 100644
--- a/fluxer_rexctl/internal/backup/archive.go
+++ b/fluxer_rexctl/internal/backup/archive.go
@@ -15,8 +15,10 @@ import (
"io"
"os"
"path"
+ "slices"
"strings"
"time"
+ "unicode"
"github.com/klauspost/compress/zstd"
)
@@ -195,6 +197,14 @@ func trailer(manifest []byte) []byte {
}
func ReadManifest(path string) (*Manifest, error) {
+ data, err := readTrailer(path)
+ if err != nil {
+ return nil, err
+ }
+ return parseManifest(data)
+}
+
+func readTrailer(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
@@ -226,7 +236,7 @@ func ReadManifest(path string) (*Manifest, error) {
if binary.LittleEndian.Uint32(frame[0:4]) != skippableMagic || binary.LittleEndian.Uint32(frame[4:8]) != uint32(n)+trailerSize {
return nil, ErrNotBackup
}
- return parseManifest(frame[8:])
+ return frame[8:], nil
}
func parseManifest(data []byte) (*Manifest, error) {
@@ -234,6 +244,9 @@ func parseManifest(data []byte) (*Manifest, error) {
if err := json.Unmarshal(data, &m); err != nil {
return nil, ErrNotBackup
}
+ if slices.ContainsFunc([]string{m.Domain, m.Project, m.Release, m.Reason}, func(s string) bool { return strings.ContainsFunc(s, unicode.IsControl) }) {
+ return nil, ErrNotBackup
+ }
if m.Format < 1 {
return nil, ErrNotBackup
}
@@ -288,27 +301,34 @@ func checkName(name string) error {
}
func Verify(path string) (*Manifest, error) {
- head, err := ReadManifest(path)
+ data, err := readTrailer(path)
+ if err != nil {
+ return nil, err
+ }
+ m, err := parseManifest(data)
if err != nil {
- return head, err
+ return m, err
}
seen := map[string]Entry{}
- var manifest, sumsData []byte
+ var manifestSum, sumsSum []byte
err = walkArchive(path, func(hdr *tar.Header, r io.Reader) error {
if hdr.Typeflag != tar.TypeReg {
return nil
}
switch hdr.Name {
case ManifestName:
- b, err := io.ReadAll(io.LimitReader(r, maxManifestSize))
- manifest = b
+ if hdr.Size != int64(len(data)) {
+ return fmt.Errorf("%w: the index doesn't match the manifest", ErrDamaged)
+ }
+ sum, err := hashReader(r)
+ manifestSum = sum
return err
case SumsName:
- b, err := io.ReadAll(io.LimitReader(r, maxManifestSize))
- sumsData = b
+ sum, err := hashReader(r)
+ sumsSum = sum
return err
}
- if manifest != nil {
+ if manifestSum != nil {
return fmt.Errorf("%w: %s comes after the manifest", ErrDamaged, hdr.Name)
}
h := sha256.New()
@@ -323,14 +343,13 @@ func Verify(path string) (*Manifest, error) {
return nil
})
if err != nil {
- return head, err
+ return m, err
}
- if manifest == nil || sumsData == nil {
- return head, fmt.Errorf("%w: the manifest is missing", ErrDamaged)
+ if manifestSum == nil || sumsSum == nil {
+ return m, fmt.Errorf("%w: the manifest is missing", ErrDamaged)
}
- m, err := parseManifest(manifest)
- if err != nil {
- return head, err
+ if want := sha256.Sum256(data); !bytes.Equal(manifestSum, want[:]) {
+ return m, fmt.Errorf("%w: the index doesn't match the manifest", ErrDamaged)
}
if len(m.Entries) != len(seen) {
return m, fmt.Errorf("%w: the manifest lists %d files and the archive has %d", ErrDamaged, len(m.Entries), len(seen))
@@ -344,19 +363,18 @@ func Verify(path string) (*Manifest, error) {
return m, fmt.Errorf("%w: %s doesn't match its checksum", ErrDamaged, want.Path)
}
}
- if !bytes.Equal(sumsData, sums(append(m.Entries, entryFor(ManifestName, manifest)))) {
+ if want := sha256.Sum256(sums(append(m.Entries, entryFor(ManifestName, data)))); !bytes.Equal(sumsSum, want[:]) {
return m, fmt.Errorf("%w: %s doesn't match the manifest", ErrDamaged, SumsName)
}
- if !sameManifest(head, m) {
- return m, fmt.Errorf("%w: the index doesn't match the manifest", ErrDamaged)
- }
return m, nil
}
-func sameManifest(a, b *Manifest) bool {
- x, err1 := json.Marshal(a)
- y, err2 := json.Marshal(b)
- return err1 == nil && err2 == nil && bytes.Equal(x, y)
+func hashReader(r io.Reader) ([]byte, error) {
+ h := sha256.New()
+ if _, err := io.Copy(h, r); err != nil {
+ return nil, fmt.Errorf("%w: %v", ErrDamaged, err)
+ }
+ return h.Sum(nil), nil
}
func volumeOf(name string) (string, string, bool) {
diff --git a/fluxer_rexctl/internal/backup/restore.go b/fluxer_rexctl/internal/backup/restore.go
index 0c02cc16b..23fa1438e 100644
--- a/fluxer_rexctl/internal/backup/restore.go
+++ b/fluxer_rexctl/internal/backup/restore.go
@@ -154,7 +154,7 @@ func Restore(ctx context.Context, path, dir string, e *engine.Engine, opts Resto
}
progress(StagePulling)
- if err := pullFromBackup(ctx, path, dir, e, data, opts.OnEvent); err != nil {
+ if err := pullFromBackup(ctx, path, dir, e, m, data, opts.OnEvent); err != nil {
return nil, err
}
@@ -467,7 +467,7 @@ func removeStaleImages(ctx context.Context, p *engine.Project, stale []string) {
}
}
-func pullFromBackup(ctx context.Context, path, dir string, e *engine.Engine, data uint64, onEvent func(engine.Event)) error {
+func pullFromBackup(ctx context.Context, path, dir string, e *engine.Engine, m *Manifest, data uint64, onEvent func(engine.Event)) error {
staging, err := os.MkdirTemp("", "rexctl-restore-")
if err != nil {
return err
@@ -480,6 +480,9 @@ func pullFromBackup(ctx context.Context, path, dir string, e *engine.Engine, dat
if err != nil {
return err
}
+ if inst.Project() != m.Project {
+ return fmt.Errorf("%w: the backup's state names project %s and its manifest names %s", ErrDamaged, inst.Project(), m.Project)
+ }
p, err := deploy.Load(ctx, inst, e)
if err != nil {
return &PullError{Err: err}
diff --git a/fluxer_rexctl/internal/checks/sso_provider.go b/fluxer_rexctl/internal/checks/sso_provider.go
index c69f2102d..6d0bc3ccf 100644
--- a/fluxer_rexctl/internal/checks/sso_provider.go
+++ b/fluxer_rexctl/internal/checks/sso_provider.go
@@ -12,6 +12,8 @@ import (
const ssoProbeSeconds = 8
+const ssoDiscoveryMaxBytes = 256 << 10
+
func init() {
Register(Check{ID: "sso-provider", Phases: Installed, Run: runSSOProvider})
}
@@ -22,6 +24,7 @@ type ssoProbeInput struct {
Discovery string `json:"discovery"`
Token string `json:"token"`
Timeout int `json:"timeout_ms"`
+ MaxBytes int `json:"max_bytes"`
}
type ssoProbeOutput struct {
@@ -70,7 +73,16 @@ const connect = (u) => new Promise((resolve, reject) => {
}
if (res.status < 200 || res.status >= 300) done({ ok: false, stage: "discovery", host: discoveryHost, status: res.status });
let doc;
- try { doc = await res.json(); } catch (e) { doc = null; }
+ try {
+ const chunks = [];
+ let size = 0;
+ for await (const c of res.body) {
+ size += c.length;
+ if (size > input.max_bytes) { chunks.length = 0; break; }
+ chunks.push(c);
+ }
+ doc = size > input.max_bytes ? null : JSON.parse(Buffer.concat(chunks).toString("utf8"));
+ } catch (e) { doc = null; }
if (!doc || typeof doc !== "object" || typeof doc.issuer !== "string") done({ ok: false, stage: "discovery", host: discoveryHost, code: "NOT_DISCOVERY" });
if (!token && typeof doc.token_endpoint === "string") token = doc.token_endpoint;
}
@@ -102,7 +114,7 @@ func ssoProvider(ctx context.Context, e *Env, run execFunc) Result {
if issuer == "" {
return skip("")
}
- in := ssoProbeInput{Token: trimmed(cfg.Sso.TokenUrl), Timeout: ssoProbeSeconds * 1000}
+ in := ssoProbeInput{Token: trimmed(cfg.Sso.TokenUrl), Timeout: ssoProbeSeconds * 1000, MaxBytes: ssoDiscoveryMaxBytes}
if trimmed(cfg.Sso.AuthorizationUrl) == "" || in.Token == "" || trimmed(cfg.Sso.JwksUrl) == "" || trimmed(cfg.Sso.UserinfoUrl) == "" {
in.Discovery = discoveryURL(issuer)
if in.Discovery == "" {
diff --git a/fluxer_rexctl/internal/engine/apply.go b/fluxer_rexctl/internal/engine/apply.go
index 2f0ca52e6..42dba9f00 100644
--- a/fluxer_rexctl/internal/engine/apply.go
+++ b/fluxer_rexctl/internal/engine/apply.go
@@ -273,7 +273,7 @@ func (p *Project) stop(ctx context.Context, timeout *time.Duration, services []s
} else if err != nil {
return err
}
- if marker := p.stopMarker(); marker != "" && os.WriteFile(marker, []byte(strings.Join(services, "\n")), 0o600) == nil {
+ if marker := p.stopMarker(); marker != "" && p.writeStopMarker(services) == nil {
defer os.Remove(marker)
}
if err := c.svc.Stop(ctx, p.Name, api.StopOptions{Project: p.modelOrNil(), Services: services, Timeout: timeout}); err != nil {
@@ -295,6 +295,23 @@ func (p *Project) stopMarker() string {
return filepath.Join(dir, "stopping")
}
+func (p *Project) writeStopMarker(services []string) error {
+ root, err := os.OpenRoot(p.Dir)
+ if err != nil {
+ return err
+ }
+ defer root.Close()
+ f, err := root.OpenFile(filepath.Join(".rexctl", "stopping"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
+ if err != nil {
+ return err
+ }
+ _, err = f.WriteString(strings.Join(services, "\n"))
+ if cerr := f.Close(); err == nil {
+ err = cerr
+ }
+ return err
+}
+
func (p *Project) finishStop(ctx context.Context, c *conn) error {
marker := p.stopMarker()
if marker == "" {
diff --git a/fluxer_rexctl/internal/engine/connect.go b/fluxer_rexctl/internal/engine/connect.go
index e0e574b13..2272a463a 100644
--- a/fluxer_rexctl/internal/engine/connect.go
+++ b/fluxer_rexctl/internal/engine/connect.go
@@ -20,11 +20,13 @@ import (
"time"
"github.com/docker/cli/cli/command"
+ "github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/flags"
"github.com/docker/compose/v5/pkg/api"
"github.com/docker/compose/v5/pkg/compose"
"github.com/moby/moby/client"
"github.com/sirupsen/logrus"
+ "github.com/spf13/pflag"
)
const probeTimeout = 5 * time.Second
@@ -263,7 +265,7 @@ func dial(ctx context.Context, host string) (*Engine, *conn, error) {
if err != nil {
return nil, nil, &Error{Op: "connect", Class: ClassUnreachable, Err: err}
}
- if err := cli.Initialize(&flags.ClientOptions{Hosts: []string{host}}); err != nil {
+ if err := cli.Initialize(clientOptions(host)); err != nil {
return nil, nil, &Error{Op: "connect", Class: ClassUnreachable, Err: err}
}
c := &conn{cli: cli}
@@ -316,6 +318,53 @@ func dial(ctx context.Context, host string) (*Engine, *conn, error) {
return e, c, nil
}
+func clientOptions(host string) *flags.ClientOptions {
+ if !strings.HasPrefix(host, "tcp://") {
+ return &flags.ClientOptions{Hosts: []string{host}}
+ }
+ if name := contextFor(host); name != "" {
+ return &flags.ClientOptions{Context: name}
+ }
+ env := flags.NewClientOptions()
+ fs := pflag.NewFlagSet("docker", pflag.ContinueOnError)
+ env.InstallFlags(fs)
+ env.SetDefaultOptions(fs)
+ return &flags.ClientOptions{Hosts: []string{host}, TLS: env.TLS, TLSVerify: env.TLSVerify, TLSOptions: env.TLSOptions}
+}
+
+func contextFor(host string) string {
+ if os.Getenv("DOCKER_HOST") == host {
+ return ""
+ }
+ cli, err := command.NewDockerCli(command.WithOutputStream(io.Discard), command.WithErrorStream(io.Discard))
+ if err != nil {
+ return ""
+ }
+ if err := cli.Initialize(flags.NewClientOptions()); err != nil {
+ return ""
+ }
+ s := cli.ContextStore()
+ names := []string{cli.CurrentContext()}
+ if list, err := s.List(); err == nil {
+ for _, m := range list {
+ names = append(names, m.Name)
+ }
+ }
+ for _, name := range names {
+ if name == command.DefaultContextName {
+ continue
+ }
+ meta, err := s.GetMetadata(name)
+ if err != nil {
+ continue
+ }
+ if ep, err := docker.EndpointFromContext(meta); err == nil && ep.Host == host {
+ return name
+ }
+ }
+ return ""
+}
+
func platform(arch string) string {
switch strings.ToLower(arch) {
case "x86_64", "amd64":
diff --git a/fluxer_rexctl/internal/engine/events.go b/fluxer_rexctl/internal/engine/events.go
index 819637a70..47cdcf2b0 100644
--- a/fluxer_rexctl/internal/engine/events.go
+++ b/fluxer_rexctl/internal/engine/events.go
@@ -6,7 +6,9 @@ import (
"context"
"strings"
"sync"
+ "unicode"
+ "github.com/charmbracelet/x/ansi"
"github.com/docker/compose/v5/pkg/api"
)
@@ -52,18 +54,19 @@ func (b *bus) On(events ...api.Resource) {
}
func toEvent(project string, r api.Resource) Event {
- kind, name, _ := strings.Cut(r.ID, " ")
+ id := plain(r.ID)
+ kind, name, _ := strings.Cut(id, " ")
ev := Event{
- ID: r.ID,
- Parent: r.ParentID,
+ ID: id,
+ Parent: plain(r.ParentID),
Kind: strings.ToLower(kind),
- Text: r.Text,
+ Text: plain(r.Text),
Status: eventStatus(r.Status),
Current: r.Current,
Total: r.Total,
}
if r.Status == api.Error && r.Details != "" {
- ev.Text = r.Details
+ ev.Text = plain(r.Details)
}
if ev.Kind == "container" {
ev.Service = serviceOf(project, name)
@@ -71,6 +74,15 @@ func toEvent(project string, r api.Resource) Event {
return ev
}
+func plain(s string) string {
+ return strings.Map(func(r rune) rune {
+ if r != '\t' && r != '\n' && unicode.IsControl(r) {
+ return -1
+ }
+ return r
+ }, ansi.Strip(s))
+}
+
func serviceOf(project, container string) string {
s := strings.TrimPrefix(container, "/")
if project != "" {
diff --git a/fluxer_rexctl/internal/engine/pull.go b/fluxer_rexctl/internal/engine/pull.go
index d4da7ca63..ef075330a 100644
--- a/fluxer_rexctl/internal/engine/pull.go
+++ b/fluxer_rexctl/internal/engine/pull.go
@@ -10,6 +10,7 @@ import (
"github.com/distribution/reference"
"github.com/docker/cli/cli/command"
+ "github.com/moby/moby/api/types/jsonstream"
"github.com/moby/moby/client"
)
@@ -59,7 +60,7 @@ func (p *Project) pullMissing(ctx context.Context, onEvent func(Event)) error {
if ctx.Err() != nil {
return ctx.Err()
}
- emit(Event{ID: id, Kind: "image", Text: err.Error(), Status: EventError})
+ emit(Event{ID: id, Kind: "image", Text: plain(err.Error()), Status: EventError})
errs = append(errs, fmt.Errorf("%s: %w", ref, err))
failed = append(failed, ref)
if registryDown(err) {
@@ -109,12 +110,12 @@ func pullOne(ctx context.Context, c *conn, ref, id string, emit func(Event)) err
return err
}
if msg.Error != nil {
- return msg.Error
+ return &jsonstream.Error{Code: msg.Error.Code, Message: plain(msg.Error.Message)}
}
if msg.ID == "" || strings.HasPrefix(msg.Status, "Pulling from") {
continue
}
- ev := Event{ID: msg.ID, Parent: id, Kind: "layer", Text: msg.Status, Status: EventWorking}
+ ev := Event{ID: plain(msg.ID), Parent: id, Kind: "layer", Text: plain(msg.Status), Status: EventWorking}
if msg.Progress != nil {
ev.Current, ev.Total = msg.Progress.Current, msg.Progress.Total
}
diff --git a/fluxer_rexctl/internal/host/docker.go b/fluxer_rexctl/internal/host/docker.go
index 857de15fd..729ff6e13 100644
--- a/fluxer_rexctl/internal/host/docker.go
+++ b/fluxer_rexctl/internal/host/docker.go
@@ -21,6 +21,18 @@ var ErrRelogin = errors.New("host: log out and back in to use docker")
var dockerDistros = []string{"ubuntu", "debian", "raspbian", "fedora", "centos", "rhel"}
+var scriptClient = &http.Client{
+ CheckRedirect: func(req *http.Request, via []*http.Request) error {
+ if req.URL.Scheme != "https" {
+ return fmt.Errorf("%s redirected to non-https %s", DockerInstallScript, req.URL.Redacted())
+ }
+ if len(via) >= 10 {
+ return errors.New("stopped after 10 redirects")
+ }
+ return nil
+ },
+}
+
func CanInstallDocker(info *Info) bool {
if info == nil || info.OS != "linux" || info.WSL {
return false
@@ -74,7 +86,7 @@ func downloadScript(ctx context.Context) (string, error) {
if err != nil {
return "", err
}
- resp, err := http.DefaultClient.Do(req)
+ resp, err := scriptClient.Do(req)
if err != nil {
return "", err
}
diff --git a/fluxer_rexctl/internal/host/sys.go b/fluxer_rexctl/internal/host/sys.go
index e748ccc6d..4cd86b01e 100644
--- a/fluxer_rexctl/internal/host/sys.go
+++ b/fluxer_rexctl/internal/host/sys.go
@@ -131,7 +131,7 @@ func parseSTUN(msg, txid []byte) (net.IP, error) {
case stunMappedAddress:
mapped = stunAddress(val, false, txid)
}
- attrs = attrs[4+(alen+3)&^3:]
+ attrs = attrs[min(4+(alen+3)&^3, len(attrs)):]
}
if mapped != nil {
return mapped, nil
diff --git a/fluxer_rexctl/internal/host/sys_linux.go b/fluxer_rexctl/internal/host/sys_linux.go
index 986e8b3d5..b9000cc6f 100644
--- a/fluxer_rexctl/internal/host/sys_linux.go
+++ b/fluxer_rexctl/internal/host/sys_linux.go
@@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"syscall"
+ "unicode"
)
func Memory() (uint64, error) {
@@ -143,9 +144,16 @@ func socketOwner(inodes map[string]bool) string {
if err != nil {
return ""
}
- return strings.TrimSpace(string(comm))
+ return strings.TrimSpace(strings.Map(dropControl, string(comm)))
}
}
}
return ""
}
+
+func dropControl(r rune) rune {
+ if unicode.IsControl(r) {
+ return -1
+ }
+ return r
+}
diff --git a/fluxer_rexctl/internal/instance/adopt.go b/fluxer_rexctl/internal/instance/adopt.go
index dabeab8d0..1a5a07836 100644
--- a/fluxer_rexctl/internal/instance/adopt.go
+++ b/fluxer_rexctl/internal/instance/adopt.go
@@ -180,6 +180,12 @@ func ResolveComposeFiles(dir string, env map[string]string, environ []string) ([
}
func PlanAdopt(l *Legacy) (*AdoptPlan, error) {
+ if !projectName.MatchString(l.Project) {
+ return nil, &AdoptError{
+ Msg: fmt.Sprintf("rexctl can't use the Compose project name %q.", l.Project),
+ Fix: "Set COMPOSE_PROJECT_NAME in .env to lowercase letters, digits, dashes and underscores, starting with a letter or digit.",
+ }
+ }
p := &AdoptPlan{
Dir: l.Dir,
Project: l.Project,
@@ -284,7 +290,7 @@ func adoptFiles(ctx context.Context, p *AdoptPlan) error {
if p.customFrom != "" {
dst := filepath.Join(p.Dir, CustomCompose)
if p.customFrom != dst {
- if within(p.Dir, p.customFrom) {
+ if resolvedWithin(p.Dir, filepath.Dir(p.customFrom)) {
if err := l.moveTo(p.customFrom, dst); err != nil {
return err
}
@@ -467,7 +473,9 @@ func (l *Legacy) copyTo(src, dst string) error {
if existing, err := os.ReadFile(dst); err == nil && bytes.Equal(existing, data) {
return nil
}
- return envfile.WriteFileAtomic(dst, normalizeLF(data), false)
+ info, err := os.Stat(src)
+ private := err == nil && info.Mode().Perm()&0o004 == 0
+ return envfile.WriteFileAtomic(dst, normalizeLF(data), private)
}
func loadLegacyEnv(path string) (*envfile.File, bool, error) {
@@ -564,6 +572,15 @@ func appendUnique(list []string, v string) []string {
return append(list, v)
}
+func resolvedWithin(dir, path string) bool {
+ realDir, err := filepath.EvalSymlinks(dir)
+ if err != nil {
+ return false
+ }
+ realPath, err := filepath.EvalSymlinks(path)
+ return err == nil && within(realDir, realPath)
+}
+
func within(dir, path string) bool {
rel, err := filepath.Rel(dir, path)
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) && !filepath.IsAbs(rel)
diff --git a/fluxer_rexctl/internal/instance/drift.go b/fluxer_rexctl/internal/instance/drift.go
index 117c7594e..e03dfd81e 100644
--- a/fluxer_rexctl/internal/instance/drift.go
+++ b/fluxer_rexctl/internal/instance/drift.go
@@ -302,9 +302,16 @@ func UnseenEdits(inst *Instance) []string {
}
func SeenEdits(inst *Instance) {
- path := inst.Path(StateDirName, EditedDir, seenMarker)
+ root, err := os.OpenRoot(inst.Dir)
+ if err != nil {
+ return
+ }
+ defer root.Close()
+ name := filepath.Join(StateDirName, EditedDir, seenMarker)
now := time.Now()
- if os.Chtimes(path, now, now) != nil {
- _ = os.WriteFile(path, nil, 0o600)
+ if root.Chtimes(name, now, now) != nil {
+ if f, err := root.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600); err == nil {
+ _ = f.Close()
+ }
}
}
diff --git a/fluxer_rexctl/internal/instance/eject.go b/fluxer_rexctl/internal/instance/eject.go
index f18af6f37..a055ca779 100644
--- a/fluxer_rexctl/internal/instance/eject.go
+++ b/fluxer_rexctl/internal/instance/eject.go
@@ -89,13 +89,20 @@ func Eject(inst *Instance, o EjectOptions) error {
func writePlain(inst *Instance) (map[string]string, error) {
out := map[string]string{}
for _, name := range stack.FileNames() {
- data, err := os.ReadFile(inst.Generated(name))
+ info, err := os.Lstat(inst.Generated(name))
if errors.Is(err, fs.ErrNotExist) {
continue
}
if err != nil {
return nil, err
}
+ if !info.Mode().IsRegular() {
+ continue
+ }
+ data, err := os.ReadFile(inst.Generated(name))
+ if err != nil {
+ return nil, err
+ }
data = stack.Plain(name, data)
if err := envfile.WriteFileAtomic(inst.Path(name), data, false); err != nil {
return nil, err
diff --git a/fluxer_rexctl/internal/instance/import.go b/fluxer_rexctl/internal/instance/import.go
index 47d1e2a45..d9d1021c5 100644
--- a/fluxer_rexctl/internal/instance/import.go
+++ b/fluxer_rexctl/internal/instance/import.go
@@ -476,7 +476,14 @@ func diskSet(dir, release string, profiles map[string]map[string]string) (*stack
return newSet(release, files, profiles)
}
+func validRelease(release string) bool {
+ return release == "" || (filepath.IsLocal(release) && release == filepath.Base(release))
+}
+
func writeReleaseSet(dir, release string) error {
+ if !validRelease(release) {
+ return fmt.Errorf("%w: %s", ErrNoReleaseSet, release)
+ }
target := filepath.Join(dir, StateDirName, ReleasesDir, release, StackDir)
if isDir(target) {
return nil
@@ -515,6 +522,9 @@ func recordRendered(dir string, s *State) error {
}
func releaseSet(dir, release string) (*stack.Set, error) {
+ if !validRelease(release) {
+ return nil, fmt.Errorf("%w: %s", ErrNoReleaseSet, release)
+ }
set, err := stack.LoadSet(filepath.Join(dir, StateDirName, ReleasesDir, release, StackDir))
if err == nil {
return set, nil
diff --git a/fluxer_rexctl/internal/instance/layout.go b/fluxer_rexctl/internal/instance/layout.go
index f5177db75..e7127d8e1 100644
--- a/fluxer_rexctl/internal/instance/layout.go
+++ b/fluxer_rexctl/internal/instance/layout.go
@@ -21,10 +21,7 @@ const generatedSchema = 3
const LayoutSchema = generatedSchema
-var (
- euid = os.Geteuid
- chown = os.Lchown
-)
+var euid = os.Geteuid
func (i *Instance) OldLayout() bool {
return i.State != nil && i.State.rootLayout
@@ -85,7 +82,7 @@ func savedSchema(path string) (int, error) {
func copyGenerated(i *Instance) (map[string]string, error) {
gen := stack.GeneratedPath(i.Dir)
- if err := os.MkdirAll(gen, 0o755); err != nil {
+ if err := stack.MakeGenerated(gen); err != nil {
return nil, err
}
if err := i.giveToOwner(gen); err != nil {
@@ -126,6 +123,11 @@ func (i *Instance) DropRootCopies(ctx context.Context, started bool) error {
}
changed := false
for name, hash := range i.State.LeftInRoot {
+ if !slices.Contains(stack.FileNames(), name) {
+ delete(i.State.LeftInRoot, name)
+ changed = true
+ continue
+ }
_, mounted := stack.MountedFiles[name]
if mounted && (!started || i.rootMounted(ctx, name)) {
continue
diff --git a/fluxer_rexctl/internal/instance/owner_unix.go b/fluxer_rexctl/internal/instance/owner_unix.go
index 01a0c8192..084676cb5 100644
--- a/fluxer_rexctl/internal/instance/owner_unix.go
+++ b/fluxer_rexctl/internal/instance/owner_unix.go
@@ -6,6 +6,7 @@ package instance
import (
"os"
+ "path/filepath"
"syscall"
)
@@ -29,25 +30,50 @@ func (i *Instance) giveToOwner(path string) error {
if euid() != 0 {
return nil
}
- info, err := os.Stat(i.Dir)
+ r, err := os.OpenRoot(i.Dir)
if err != nil {
return err
}
- st, ok := info.Sys().(*syscall.Stat_t)
- if !ok || st.Uid == 0 {
+ defer r.Close()
+ info, err := r.Stat(".")
+ if err != nil {
+ return err
+ }
+ uid, gid, ok := fileIDs(info)
+ if !ok || uid == 0 {
return nil
}
- return chown(path, int(st.Uid), int(st.Gid))
-}
-
-func dirIDs(dir string) (int, int, bool) {
- info, err := os.Stat(dir)
+ rel, err := filepath.Rel(i.Dir, path)
if err != nil {
- return 0, 0, false
+ return err
}
+ return claimForOwner(r, rel, uid, gid)
+}
+
+func fileIDs(info os.FileInfo) (int, int, bool) {
st, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return 0, 0, false
}
return int(st.Uid), int(st.Gid), true
}
+
+func claimForOwner(r *os.Root, name string, uid, gid int) error {
+ f, err := r.OpenFile(name, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
+ if err != nil {
+ return nil
+ }
+ defer f.Close()
+ info, err := f.Stat()
+ if err != nil {
+ return err
+ }
+ st, ok := info.Sys().(*syscall.Stat_t)
+ if !ok || st.Uid != 0 {
+ return nil
+ }
+ if !info.IsDir() && (!info.Mode().IsRegular() || st.Nlink != 1) {
+ return nil
+ }
+ return f.Chown(uid, gid)
+}
diff --git a/fluxer_rexctl/internal/instance/owner_windows.go b/fluxer_rexctl/internal/instance/owner_windows.go
index 9bdcb01c4..baf9c9b0e 100644
--- a/fluxer_rexctl/internal/instance/owner_windows.go
+++ b/fluxer_rexctl/internal/instance/owner_windows.go
@@ -12,4 +12,6 @@ func ownerOf(os.FileInfo) (int, bool) { return 0, false }
func (i *Instance) giveToOwner(string) error { return nil }
-func dirIDs(string) (int, int, bool) { return 0, 0, false }
+func fileIDs(os.FileInfo) (int, int, bool) { return 0, 0, false }
+
+func claimForOwner(*os.Root, string, int, int) error { return nil }
diff --git a/fluxer_rexctl/internal/instance/ownership.go b/fluxer_rexctl/internal/instance/ownership.go
index 00c0f0353..f272665ff 100644
--- a/fluxer_rexctl/internal/instance/ownership.go
+++ b/fluxer_rexctl/internal/instance/ownership.go
@@ -15,35 +15,46 @@ type Foreign struct {
Dir string
}
-func (i *Instance) ownDirs() []string {
- return []string{i.Path(StateDirName), i.Path(BackupsDir)}
-}
-
func (i *Instance) GiveFilesToOwner() error {
if euid() != 0 {
return nil
}
- uid, gid, ok := dirIDs(i.Dir)
+ r, err := os.OpenRoot(i.Dir)
+ if err != nil {
+ return nil
+ }
+ defer r.Close()
+ info, err := r.Stat(".")
+ if err != nil {
+ return nil
+ }
+ uid, gid, ok := fileIDs(info)
if !ok || uid == 0 {
return nil
}
var errs []error
- for _, dir := range i.ownDirs() {
- err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
+ for _, name := range []string{StateDirName, BackupsDir} {
+ top, err := r.Lstat(name)
+ if err != nil {
+ if !errors.Is(err, fs.ErrNotExist) {
+ errs = append(errs, err)
+ }
+ continue
+ }
+ if !top.IsDir() {
+ continue
+ }
+ err = fs.WalkDir(r.FS(), name, func(path string, d fs.DirEntry, err error) error {
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
- info, err := d.Info()
- if err != nil {
+ if !d.IsDir() && !d.Type().IsRegular() {
return nil
}
- if owner, ok := ownerOf(info); ok && owner == 0 {
- return chownIDs(path, uid, gid)
- }
- return nil
+ return claimForOwner(r, path, uid, gid)
})
if err != nil && !errors.Is(err, fs.ErrNotExist) {
errs = append(errs, err)
@@ -75,10 +86,3 @@ func (i *Instance) ForeignFile() (Foreign, bool) {
})
return found, found.Path != ""
}
-
-func chownIDs(path string, uid, gid int) error {
- if err := chown(path, uid, gid); err != nil && !errors.Is(err, os.ErrNotExist) {
- return err
- }
- return nil
-}
diff --git a/fluxer_rexctl/internal/instance/state.go b/fluxer_rexctl/internal/instance/state.go
index 528d1b244..5594255ec 100644
--- a/fluxer_rexctl/internal/instance/state.go
+++ b/fluxer_rexctl/internal/instance/state.go
@@ -17,6 +17,7 @@ import (
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"
)
const StateSchema = generatedSchema + 2
@@ -230,10 +231,18 @@ func parseStateRepairing(data []byte, dir string, repairing []string) (*State, e
if head.Project == "" {
return nil, fmt.Errorf("instance: the state in %s names no project", dir)
}
+ if !projectName.MatchString(head.Project) {
+ return nil, fmt.Errorf("instance: the state in %s names the project %q: %w", dir, head.Project, ErrBadProject)
+ }
s, err := parseState(data, dir, repairing)
if err != nil {
return nil, err
}
+ for _, v := range s.Voice {
+ if !voice.SafeDirName(v.ID) {
+ return nil, fmt.Errorf("instance: the state in %s has a voice server with the ID %q, which isn't a folder name", dir, v.ID)
+ }
+ }
s.rootLayout = head.Schema < generatedSchema
s.normalize()
return s, nil
diff --git a/fluxer_rexctl/internal/instance/state_test.go b/fluxer_rexctl/internal/instance/state_test.go
index 43fb93583..3440e5f28 100644
--- a/fluxer_rexctl/internal/instance/state_test.go
+++ b/fluxer_rexctl/internal/instance/state_test.go
@@ -29,3 +29,15 @@ func TestParseStateSplitsListenAddress(t *testing.T) {
t.Fatalf("config set is locked out: %v", err)
}
}
+
+func TestParseStateRefusesInvalidNames(t *testing.T) {
+ if _, err := ParseState([]byte(`{"schema":5,"project":"Fluxer Prod"}`), "/data/fluxer"); !errors.Is(err, ErrBadProject) {
+ t.Fatalf("project: err %v", err)
+ }
+ if _, err := ParseState([]byte(`{"schema":5,"project":"fluxer","voice":[{"id":"a/b"}]}`), "/data/fluxer"); err == nil {
+ t.Fatal("voice server ID: no error")
+ }
+ if _, err := ParseState([]byte(`{"schema":5,"project":"fluxer","voice":[{"id":"EU.West_server-1"}]}`), "/data/fluxer"); err != nil {
+ t.Fatalf("voice server ID: %v", err)
+ }
+}
diff --git a/fluxer_rexctl/internal/settings/mask.go b/fluxer_rexctl/internal/settings/mask.go
index 7c8b79811..e1a891abc 100644
--- a/fluxer_rexctl/internal/settings/mask.go
+++ b/fluxer_rexctl/internal/settings/mask.go
@@ -13,7 +13,7 @@ import (
const Masked = "set"
-var secretParts = []string{"PASSWORD", "SECRET", "_TOKEN", "PRIVATE_KEY", "COOKIE", "API_KEY", "PROXY_KEY", "BLUESKY_KEYS", "SECRETS_BASE64"}
+var secretParts = []string{"PASSWORD", "SECRET", "_TOKEN", "PRIVATE_KEY", "COOKIE", "API_KEY", "PROXY_KEY", "BLUESKY_KEYS", "SECRETS_BASE64", "TRUSTED_CALLERS", "SIGNING_KEY", "MASTER_KEY"}
func SecretName(name string) bool {
for _, p := range secretParts {
diff --git a/fluxer_rexctl/internal/stack/bundle.go b/fluxer_rexctl/internal/stack/bundle.go
index a117b4dda..5c1b66f60 100644
--- a/fluxer_rexctl/internal/stack/bundle.go
+++ b/fluxer_rexctl/internal/stack/bundle.go
@@ -3,10 +3,13 @@
package stack
import (
+ "errors"
"io/fs"
"os"
"path/filepath"
"strings"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"
)
const BundleProject = "fluxer"
@@ -25,12 +28,12 @@ func Bundle(dir string, images map[string]string) error {
files = append(files, renderedFile{OverlayFiles[o], data})
}
for _, d := range madeDirs {
- if err := os.MkdirAll(filepath.Join(dir, d), 0o755); err != nil {
+ if err := makeBundleDir(filepath.Join(dir, d)); err != nil {
return err
}
}
for _, f := range files {
- if err := os.WriteFile(filepath.Join(dir, f.name), f.data, 0o644); err != nil {
+ if err := envfile.WriteFileAtomic(filepath.Join(dir, f.name), f.data, false); err != nil {
return err
}
}
@@ -40,7 +43,7 @@ func Bundle(dir string, images map[string]string) error {
}
target := filepath.Join(dir, filepath.FromSlash(name))
if d.IsDir() {
- return os.MkdirAll(target, 0o755)
+ return makeBundleDir(target)
}
data, err := ReadFile(set, name)
if err != nil {
@@ -51,6 +54,20 @@ func Bundle(dir string, images map[string]string) error {
return err
}
}
- return os.WriteFile(target, data, 0o644)
+ return envfile.WriteFileAtomic(target, data, false)
})
}
+
+func makeBundleDir(path string) error {
+ if err := os.MkdirAll(path, 0o755); err != nil {
+ return err
+ }
+ info, err := os.Lstat(path)
+ if err != nil {
+ return err
+ }
+ if !info.IsDir() {
+ return errors.New("stack: " + path + " is not a plain folder")
+ }
+ return nil
+}
diff --git a/fluxer_rexctl/internal/stack/layersizes/main.go b/fluxer_rexctl/internal/stack/layersizes/main.go
index 43ab16aba..dc6db5699 100644
--- a/fluxer_rexctl/internal/stack/layersizes/main.go
+++ b/fluxer_rexctl/internal/stack/layersizes/main.go
@@ -35,6 +35,7 @@ const (
mediaManifest = "application/vnd.oci.image.manifest.v1+json"
mediaDockerV2 = "application/vnd.docker.distribution.manifest.v2+json"
parallelLayers = 4
+ maxManifest = 4 << 20
)
func main() {
@@ -316,7 +317,7 @@ func (c *client) login(ctx context.Context, r repo, challenge string) error {
Token string `json:"token"`
AccessToken string `json:"access_token"`
}
- if err := json.NewDecoder(res.Body).Decode(&body); err != nil {
+ if err := json.NewDecoder(io.LimitReader(res.Body, maxManifest)).Decode(&body); err != nil {
return err
}
tok := body.Token
@@ -335,10 +336,13 @@ func (c *client) manifest(ctx context.Context, r repo, ref string) (manifest, st
return manifest{}, "", err
}
defer res.Body.Close()
- data, err := io.ReadAll(res.Body)
+ data, err := io.ReadAll(io.LimitReader(res.Body, maxManifest+1))
if err != nil {
return manifest{}, "", err
}
+ if len(data) > maxManifest {
+ return manifest{}, "", fmt.Errorf("manifest %s is larger than %d bytes", ref, maxManifest)
+ }
var m manifest
if err := json.Unmarshal(data, &m); err != nil {
return manifest{}, "", err
@@ -375,7 +379,7 @@ func (c *client) diffIDs(ctx context.Context, r repo, digest string) ([]string,
DiffIDs []string `json:"diff_ids"`
} `json:"rootfs"`
}
- if err := json.NewDecoder(res.Body).Decode(&cfg); err != nil {
+ if err := json.NewDecoder(io.LimitReader(res.Body, maxManifest)).Decode(&cfg); err != nil {
return nil, err
}
return cfg.RootFS.DiffIDs, nil
diff --git a/fluxer_rexctl/internal/stack/stack.go b/fluxer_rexctl/internal/stack/stack.go
index 0cec80143..550977a8a 100644
--- a/fluxer_rexctl/internal/stack/stack.go
+++ b/fluxer_rexctl/internal/stack/stack.go
@@ -343,7 +343,11 @@ func RenderAt(set *Set, selected []Overlay, images map[string]string, project, d
}
}
gen := FilesDir(dir, root)
- if err := os.MkdirAll(gen, 0o755); err != nil {
+ if root {
+ if err := os.MkdirAll(gen, 0o755); err != nil {
+ return nil, err
+ }
+ } else if err := MakeGenerated(gen); err != nil {
return nil, err
}
changed, stale, err := plan(gen, files, overlays)
@@ -386,7 +390,19 @@ func restartNeeded(changed []string) []string {
return out
}
-const tmpSuffix = ".rexctl-tmp"
+func MakeGenerated(path string) error {
+ if err := os.MkdirAll(path, 0o755); err != nil {
+ return err
+ }
+ info, err := os.Lstat(path)
+ if err != nil {
+ return err
+ }
+ if !info.IsDir() {
+ return fmt.Errorf("stack: %s is not a directory", path)
+ }
+ return nil
+}
func writeAll(dir string, files []renderedFile) error {
var staged []string
@@ -396,9 +412,8 @@ func writeAll(dir string, files []renderedFile) error {
}
}
for _, f := range files {
- tmp := filepath.Join(dir, f.name+tmpSuffix)
- if err := writeSynced(tmp, f.data); err != nil {
- os.Remove(tmp)
+ tmp, err := stage(dir, f.name, f.data)
+ if err != nil {
cleanup()
return err
}
@@ -412,10 +427,14 @@ func writeAll(dir string, files []renderedFile) error {
var done []previous
restore := func() {
for _, p := range done {
- if p.exists {
- writeSynced(p.path, p.data)
- } else {
+ if !p.exists {
os.Remove(p.path)
+ continue
+ }
+ if tmp, err := stage(filepath.Dir(p.path), filepath.Base(p.path), p.data); err == nil {
+ if os.Rename(tmp, p.path) != nil {
+ os.Remove(tmp)
+ }
}
}
}
@@ -438,18 +457,25 @@ func writeAll(dir string, files []renderedFile) error {
return nil
}
-func writeSynced(path string, data []byte) error {
- f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
+func stage(dir, name string, data []byte) (string, error) {
+ f, err := os.CreateTemp(dir, "."+name+".rexctl-tmp-*")
if err != nil {
- return err
+ return "", err
}
- if _, err := f.Write(data); err != nil {
- f.Close()
- return err
+ path := f.Name()
+ err = f.Chmod(0o644)
+ if err == nil {
+ _, err = f.Write(data)
}
- if err := f.Sync(); err != nil {
- f.Close()
- return err
+ if err == nil {
+ err = f.Sync()
+ }
+ if closeErr := f.Close(); err == nil {
+ err = closeErr
+ }
+ if err != nil {
+ os.Remove(path)
+ return "", err
}
- return f.Close()
+ return path, nil
}
diff --git a/fluxer_rexctl/internal/ui/rex.go b/fluxer_rexctl/internal/ui/rex.go
index 3727d127a..6fcc60227 100644
--- a/fluxer_rexctl/internal/ui/rex.go
+++ b/fluxer_rexctl/internal/ui/rex.go
@@ -81,7 +81,7 @@ func (u *UI) Rex(lines ...string) {
b.WriteString("\n")
for i, row := range art {
if i >= first && i-first < len(lines) {
- row += " " + lines[i-first]
+ row += " " + printable(lines[i-first])
}
b.WriteString(row + "\n")
}
diff --git a/fluxer_rexctl/internal/ui/ui.go b/fluxer_rexctl/internal/ui/ui.go
index 17c1c14cd..f64b87027 100644
--- a/fluxer_rexctl/internal/ui/ui.go
+++ b/fluxer_rexctl/internal/ui/ui.go
@@ -170,11 +170,11 @@ func (u *UI) glyphLine(glyph string, style lipgloss.Style, msg, fix string) {
var b strings.Builder
b.WriteString(style.Render(glyph))
b.WriteString(" ")
- b.WriteString(msg)
+ b.WriteString(printable(msg))
b.WriteString("\n")
if fix != "" {
b.WriteString(" ")
- b.WriteString(fix)
+ b.WriteString(printable(fix))
b.WriteString("\n")
}
u.emit(b.String())
@@ -206,7 +206,7 @@ func (u *UI) Detail(format string, args ...any) {
if !u.opts.Verbose {
return
}
- u.emit(u.theme.Dim.Render(fmt.Sprintf(format, args...)) + "\n")
+ u.emit(u.theme.Dim.Render(printable(fmt.Sprintf(format, args...))) + "\n")
}
func (u *UI) Result(s string) {
@@ -214,6 +214,7 @@ func (u *UI) Result(s string) {
}
func (u *UI) Highlight(s string) {
+ s = Inline(s)
if !u.stdoutTTY {
fmt.Fprintln(u.opts.Stdout, s)
return
@@ -234,6 +235,12 @@ func (u *UI) JSON(v any) error {
}
func (u *UI) Table(headers []string, rows [][]string) {
+ headers = cells(headers)
+ clean := make([][]string, len(rows))
+ for i, r := range rows {
+ clean[i] = cells(r)
+ }
+ rows = clean
widths := make([]int, len(headers))
for i, h := range headers {
widths[i] = ansi.StringWidth(h)
@@ -326,9 +333,9 @@ func (u *UI) ReportError(err error) {
}
if ee.Code == ExitUsage {
var b strings.Builder
- b.WriteString(msg + "\n")
+ b.WriteString(printable(msg) + "\n")
if ee.Fix != "" {
- b.WriteString(ee.Fix + "\n")
+ b.WriteString(printable(ee.Fix) + "\n")
}
u.emit(b.String())
} else {
@@ -351,6 +358,32 @@ func (u *UI) ReportError(err error) {
}
}
+func printable(s string) string {
+ return strings.Map(func(r rune) rune {
+ if r == '\n' || r == '\t' || !unicode.IsControl(r) {
+ return r
+ }
+ return -1
+ }, ansi.Strip(s))
+}
+
+func Inline(s string) string {
+ return strings.Map(func(r rune) rune {
+ if r == '\n' || r == '\t' {
+ return ' '
+ }
+ return r
+ }, printable(s))
+}
+
+func cells(row []string) []string {
+ out := make([]string, len(row))
+ for i, c := range row {
+ out[i] = Inline(c)
+ }
+ return out
+}
+
func sentence(s string) string {
s = strings.TrimSpace(s)
if s == "" {
diff --git a/fluxer_rexctl/internal/voice/voice.go b/fluxer_rexctl/internal/voice/voice.go
index 7ca68d046..f68df1828 100644
--- a/fluxer_rexctl/internal/voice/voice.go
+++ b/fluxer_rexctl/internal/voice/voice.go
@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
)
@@ -48,10 +49,15 @@ const (
EdgeNone = "none"
)
-var idPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}$`)
+var (
+ idPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}$`)
+ regionPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_.-]{0,62}$`)
+)
func ValidID(id string) bool { return idPattern.MatchString(id) }
+func UsableRegion(id string) bool { return regionPattern.MatchString(id) }
+
func JoinedProject(id string) string { return MachineProject + "-" + id }
func ProjectName(instanceProject, id string) string {
@@ -60,6 +66,10 @@ func ProjectName(instanceProject, id string) string {
func ServerDir(dir, id string) string { return filepath.Join(dir, ServersDir, id) }
+func SafeDirName(id string) bool {
+ return id != "" && id != "." && id != ".." && !strings.ContainsAny(id, `/\`)
+}
+
func Alias(id string) string { return "voice-" + id }
func Key(id string) string { return KeyPrefix + id }
@@ -174,7 +184,10 @@ func (e Env) File() []byte {
}
func envValue(v string) string {
- if strings.ContainsAny(v, " \"'{}[]$#\\") && !strings.Contains(v, "'") {
+ switch {
+ case strings.ContainsAny(v, "'\n\r") || strings.HasSuffix(v, `\`):
+ return envfile.Quote(v)
+ case strings.ContainsAny(v, " \"{}[]$#\\"):
return "'" + v + "'"
}
return v
diff --git a/fluxer_rexctl/internal/voice/voice_test.go b/fluxer_rexctl/internal/voice/voice_test.go
new file mode 100644
index 000000000..0ba480e83
--- /dev/null
+++ b/fluxer_rexctl/internal/voice/voice_test.go
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package voice
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/compose-spec/compose-go/v2/dotenv"
+)
+
+func TestEnvFileValuesStayOnOneLine(t *testing.T) {
+ cases := map[string]Env{
+ "plain": {ID: "eu-server-1", Secret: "deadbeef", WebhookURL: "http://api:8080/webhooks/livekit?a=1&b=2"},
+ "quoted turn": {ID: "eu-server-1", TurnServers: `[{"host":"t","port":3478,"credential":"pa'ss$HOME #x"}]`},
+ "multi-line webhook": {ID: "eu-server-1", WebhookURL: "http://api\nsecond line"},
+ "trailing backslash": {ID: "eu-server-1", Secret: `ab c\`},
+ "crlf": {ID: "eu-server-1", Secret: "a\r\nb\"c\\d$e"},
+ }
+ for name, e := range cases {
+ t.Run(name, func(t *testing.T) {
+ want := e.Values()
+ got, err := dotenv.ParseWithLookup(bytes.NewReader(e.File()), func(string) (string, bool) { return "", false })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(got) != len(want) {
+ t.Fatalf("parsed %d keys, want %d: %v", len(got), len(want), got)
+ }
+ for k, v := range want {
+ if got[k] != v {
+ t.Errorf("%s = %q, want %q", k, got[k], v)
+ }
+ }
+ })
+ }
+}