From 2e19c50e9e1d087dc85531428acd954b296bdc9f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 22:32:39 +0000 Subject: [PATCH] Small fixes: capital letters, display file reads, script data limit, scans MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - app: capital upper-cases the first letter, not its first byte, so an error beginning "é" is not mangled. - screens: readFile reads the whole file (one Read may stop short) and still refuses one grown past MaxSize. - script: data.set's limit check and write are done under one lock, so sessions setting new names at once cannot pass the limit. - store: the scan helpers return nil, not a half-filled value, with an error (scanUser already did). - The validate and edit-user "Lock" actions are capitalized like the others; door's unused yn helper removed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr --- internal/app/app_test.go | 9 +++++++++ internal/app/files.go | 5 ++++- internal/app/people.go | 2 +- internal/app/sysop.go | 3 +-- internal/door/dropfile.go | 8 -------- internal/screens/screens.go | 14 +++++++++++--- internal/script/api.go | 21 ++++++++++++++++----- internal/store/chan.go | 10 ++++++++-- internal/store/sshkeys.go | 5 ++++- internal/store/store.go | 25 ++++++++++++++++++++----- internal/store/tokens.go | 5 ++++- 11 files changed, 78 insertions(+), 29 deletions(-) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index cc60f17..d283535 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -685,3 +685,12 @@ func TestNodelist(t *testing.T) { t.Errorf("sent %q", sent) } } + +// TestCapital: the first letter upper-cased, however many bytes it is. +func TestCapital(t *testing.T) { + for in, want := range map[string]string{"": "", "abc": "Abc", "élan": "Élan", "über": "Über", "1x": "1x"} { + if got := capital(in); got != want { + t.Errorf("%q: %q, want %q", in, got, want) + } + } +} diff --git a/internal/app/files.go b/internal/app/files.go index b0999fb..652181d 100644 --- a/internal/app/files.go +++ b/internal/app/files.go @@ -5,6 +5,8 @@ import ( "fmt" "strconv" "strings" + "unicode" + "unicode/utf8" "github.com/Neo-Desktop/bbs-sec/internal/bbs" "github.com/Neo-Desktop/bbs-sec/internal/files" @@ -362,7 +364,8 @@ func capital(s string) string { if s == "" { return s } - return strings.ToUpper(s[:1]) + s[1:] + r, n := utf8.DecodeRuneInString(s) + return string(unicode.ToUpper(r)) + s[n:] // the first letter, however many bytes } // fileDetail: a file's details, and its uploader's (or a sysop's) changes diff --git a/internal/app/people.go b/internal/app/people.go index dea6afd..a0a475b 100644 --- a/internal/app/people.go +++ b/internal/app/people.go @@ -517,7 +517,7 @@ func (s *Session) validate() error { Actions: []view.Action{ {ID: "validate", Label: fmt.Sprintf("Validate (level %d)", s.B.Conf().ValidatedLevel), Key: 'V', PF: 5, Short: "Validate"}, {ID: "level", Label: "Level...", Key: 'L', PF: 6, Short: "Level"}, - {ID: "lock", Label: "lock", Key: 'K', PF: 9, Short: "Lock"}, + {ID: "lock", Label: "Lock", Key: 'K', PF: 9}, {ID: "skip", Label: "Skip", Key: 'S', PF: 8}}}) if err != nil { return err diff --git a/internal/app/sysop.go b/internal/app/sysop.go index e9cdc2a..9467e23 100644 --- a/internal/app/sysop.go +++ b/internal/app/sysop.go @@ -229,8 +229,7 @@ func (s *Session) editUser(id int64) error { {ID: "flags", Label: "Flags", Key: 'F', PF: 6}}} if u.ID != s.User.ID { p.Actions = append(p.Actions, - view.Action{ID: "lock", Label: map[bool]string{true: "Unlock", false: "lock"}[u.Locked], Key: 'K', PF: 9, - Short: map[bool]string{true: "Unlock", false: "Lock"}[u.Locked]}, + view.Action{ID: "lock", Label: map[bool]string{true: "Unlock", false: "Lock"}[u.Locked], Key: 'K', PF: 9}, view.Action{ID: "sysop", Label: map[bool]string{true: "Sysop no more", false: "Sysop"}[u.Sysop], Key: 'S', PF: 10}) } if !u.External { diff --git a/internal/door/dropfile.go b/internal/door/dropfile.go index 447839b..694d7ee 100644 --- a/internal/door/dropfile.go +++ b/internal/door/dropfile.go @@ -69,14 +69,6 @@ func names(n string) (string, string) { return f[0], strings.Join(f[1:], " ") } -// yn is a flag as Y or N. -func yn(b bool) string { - if b { - return "Y" - } - return "N" -} - // onezero is a flag as 1 or 0. func onezero(b bool) string { if b { diff --git a/internal/screens/screens.go b/internal/screens/screens.go index bf2f088..c67021d 100644 --- a/internal/screens/screens.go +++ b/internal/screens/screens.go @@ -16,6 +16,7 @@ package screens import ( "errors" + "io" "os" "path/filepath" "regexp" @@ -128,9 +129,16 @@ func readFile(path string) ([]byte, error) { if fi.Size() > MaxSize { return nil, errors.New("too big") } - b := make([]byte, fi.Size()) - n, err := f.Read(b) - return b[:n], err + // all of it (one Read may stop short), and no more than MaxSize should + // it have grown since + b, err := io.ReadAll(io.LimitReader(f, MaxSize+1)) + if err != nil { + return nil, err + } + if len(b) > MaxSize { + return nil, errors.New("too big") + } + return b, nil } // stripSAUCE drops what follows ^Z: a SAUCE record, comments. diff --git a/internal/script/api.go b/internal/script/api.go index b58ccbf..9bf5a81 100644 --- a/internal/script/api.go +++ b/internal/script/api.go @@ -6,6 +6,7 @@ import ( "fmt" "strconv" "strings" + "sync" "time" "unicode/utf8" @@ -530,6 +531,9 @@ func (r *run) checkKey(k string) { } } +// dataMu keeps data.set's limit check and its write together. +var dataMu sync.Mutex + // dataObject is the script's data, kept for everyone (user 0) or for that user: // get (undefined if there is none), set as JSON (up to maxValue bytes, and a // limit on the names), remove, and keys, the names with a prefix. @@ -566,12 +570,19 @@ func (r *run) dataObject(user int64) *goja.Object { if len(b) > maxValue { r.throw("data.set: the value is over %d bytes", maxValue) } - if _, had, _ := r.b.Store.ScriptGet(r.name, user, k); !had { - if n, _ := r.b.Store.ScriptCount(r.name, user); n >= most { - r.throw("data.set: that is the %d names a script keeps", most) + // the count and the set together, so that callers setting new names + // at once cannot pass the limit between them + dataMu.Lock() + err := func() error { + if _, had, _ := r.b.Store.ScriptGet(r.name, user, k); !had { + if n, _ := r.b.Store.ScriptCount(r.name, user); n >= most { + return fmt.Errorf("data.set: that is the %d names a script keeps", most) + } } - } - if err := r.b.Store.ScriptSet(r.name, user, k, string(b)); err != nil { + return r.b.Store.ScriptSet(r.name, user, k, string(b)) + }() + dataMu.Unlock() + if err != nil { r.throw("%v", err) } }) diff --git a/internal/store/chan.go b/internal/store/chan.go index fe69534..46a0b17 100644 --- a/internal/store/chan.go +++ b/internal/store/chan.go @@ -125,8 +125,11 @@ func scanBoard(row interface{ Scan(...any) error }) (*Board, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } b.Created = unix(created) - return &b, err + return &b, nil } // CreateBoard adds an image board, as b has it (ID, LastNo, Created @@ -221,11 +224,14 @@ func scanPost(row interface{ Scan(...any) error }, more ...any) (*BoardPost, err if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } p.Created, p.Bumped = unix(created), unix(bumped) if archived != 0 { p.Archived = unix(archived) } - return &p, err + return &p, nil } // posts are the posts a query of postCols finds. diff --git a/internal/store/sshkeys.go b/internal/store/sshkeys.go index 9009f27..dacaade 100644 --- a/internal/store/sshkeys.go +++ b/internal/store/sshkeys.go @@ -50,8 +50,11 @@ func scanSSHKey(row interface{ Scan(...any) error }) (*SSHKey, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } k.Created, k.LastUsed = unix(created), unix(last) - return &k, err + return &k, nil } // SSHKeyByFingerprint finds a key. diff --git a/internal/store/store.go b/internal/store/store.go index 9c77a85..3f21353 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -393,7 +393,10 @@ func scanArea(row interface{ Scan(...any) error }) (*Area, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } - return &a, err + if err != nil { + return nil, err // nothing half scanned + } + return &a, nil } // Areas lists the message areas. @@ -437,8 +440,11 @@ func scanMessage(row interface{ Scan(...any) error }) (*Message, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } m.Created = unix(created) - return &m, err + return &m, nil } // Post adds a message to an area. @@ -597,8 +603,11 @@ func scanMail(row interface{ Scan(...any) error }) (*Message, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } m.Created = unix(created) - return &m, err + return &m, nil } // SendMail delivers a private message. @@ -721,7 +730,10 @@ func scanFileArea(row interface{ Scan(...any) error }) (*FileArea, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } - return &a, err + if err != nil { + return nil, err // nothing half scanned + } + return &a, nil } // FileAreas lists the file areas. @@ -764,8 +776,11 @@ func scanFile(row interface{ Scan(...any) error }) (*File, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } f.Created = unix(created) - return &f, err + return &f, nil } // AddFile records a file; ErrExists if the area has one of that name. diff --git a/internal/store/tokens.go b/internal/store/tokens.go index 4e5f056..34e84f6 100644 --- a/internal/store/tokens.go +++ b/internal/store/tokens.go @@ -46,8 +46,11 @@ func scanToken(row interface{ Scan(...any) error }) (*Token, error) { if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } + if err != nil { + return nil, err // nothing half scanned + } t.Created, t.Expires, t.LastUsed = unix(created), unix(expires), unix(last) - return &t, err + return &t, nil } // TokenByHash finds a token, if it has not expired.