mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Small fixes: capital letters, display file reads, script data limit, scans
- 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
11 files changed
+78
-29
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
+16
-5
@@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
+20
-5
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user