Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package services
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// NotesConfig points at the directory of markdown notes mounted into the
|
|
// container.
|
|
type NotesConfig struct {
|
|
Dir string
|
|
}
|
|
|
|
// Notes serves files out of a single directory, guarding against path
|
|
// traversal. It is admin-only at the route level (see main.go).
|
|
type Notes struct {
|
|
Config NotesConfig
|
|
}
|
|
|
|
// InitNotes builds the Notes service.
|
|
func InitNotes(config *NotesConfig) *Notes {
|
|
return &Notes{
|
|
Config: *config,
|
|
}
|
|
}
|
|
|
|
// ParsePath appends the separator before the prefix check, so a sibling such
|
|
// as /backend/notes-private can't pass.
|
|
func (notes *Notes) ParsePath(path string) (string, error) {
|
|
// The route is /notes/*path, so a bare /notes/ arrives as "" or "/";
|
|
// treat both as a request for the wiki index.
|
|
if path == "" || path == "/" {
|
|
path = "Index.md"
|
|
}
|
|
|
|
baseDir, err := filepath.Abs(notes.Config.Dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fullPath := filepath.Join(baseDir, path)
|
|
fullPath, err = filepath.Abs(fullPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Enforce directory boundary
|
|
if !strings.HasPrefix(fullPath, baseDir+string(os.PathSeparator)) {
|
|
return "", errors.New("Invalid path")
|
|
}
|
|
|
|
return fullPath, nil
|
|
}
|