Skip to content

Commit

Permalink
Normalize paths within fs.FS
Browse files Browse the repository at this point in the history
The shrt command provides an fs.FS rooted at "/" to the ShrtHandler.
Files are accessed within the FS as relative paths. As a consequence,
paths passed into the ShrtHandler Config must be relative to the root
of the file system. This is equivalent to saying paths must be
absolute, but must trim the leading "/".

This commit modifies the env subcommand to satisfy this requirement.
Paths provided to override SHRT_DBPATH must now be absolute.
  • Loading branch information
djmoch committed Nov 3, 2023
1 parent ba2537d commit 512d53e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
9 changes: 7 additions & 2 deletions cmd/shrt/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ func runEnvW(args []string) {

for k, v := range envToWrite {
if k == base.SHRTENV {
log.Print(base.SHRTENV, " can only be set using the OS environment")
log.Println(base.SHRTENV, "can only be set using the OS environment")
continue
}
if k == base.SHRT_DBPATH && !filepath.IsAbs(v) {
log.Println(base.SHRT_DBPATH, "must be an absolute path ... ignoring")
continue
}
curEnv[k] = v
Expand Down Expand Up @@ -182,7 +186,8 @@ func ConfigFromEnv() shrt.Config {
Suffix: envOrDefault(base.SHRT_SUFFIX, suffixDefault),
RdrName: envOrDefault(base.SHRT_RDRNAME, rdrNameDefault),
BareRdr: envOrDefault(base.SHRT_BARERDR, bareRdrDefault),
DbPath: envOrDefault(base.SHRT_DBPATH, dbPathDefault),
// Trim the leading / to satisfy fs.FS
DbPath: strings.TrimPrefix(envOrDefault(base.SHRT_DBPATH, dbPathDefault), "/"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/shrt/internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestRunEnvUW(t *testing.T) {
if _, ok := envMap[base.SHRT_DBPATH]; ok {
t.Fatalf("%s key found in %s", base.SHRT_DBPATH, envPath)
}
runEnvW([]string{base.SHRT_DBPATH + "=bar"})
runEnvW([]string{base.SHRT_DBPATH + "=/bar"})
envMap = readEnvFile(envPath)
if _, ok := envMap[base.SHRT_DBPATH]; !ok {
t.Fatalf("%s key not found in %s", base.SHRT_DBPATH, envPath)
Expand Down
1 change: 1 addition & 0 deletions cmd/shrt/internal/env/env_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// See LICENSE file for copyright and license details
//go:build unix

package env

Expand Down
4 changes: 2 additions & 2 deletions cmd/shrt/internal/env/env_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
package env

const (
envDefault = "C:\\ProgramData\\shrt\\config"
dbPathDefault = "C:\\ProgramData\\shrt\\shrt.db"
envDefault = `C:\ProgramData\shrt\config`
dbPathDefault = `C:\ProgramData\shrt\shrt.db`
)
2 changes: 1 addition & 1 deletion cmd/shrt/internal/env/testdata/env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SHRT_SRVNAME=foo
SHRT_DBPATH=bar
SHRT_DBPATH=/bar
14 changes: 5 additions & 9 deletions cmd/shrt/internal/serve/lockdown_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ package serve

import (
"fmt"
"path/filepath"

"golang.org/x/sys/unix"
)

func init() {
lockdown = func(dbPath string) {
path, err := filepath.Abs(dbPath)
path := "/" + dbPath
err := unix.Unveil(path, "r")
if err != nil {
panic(fmt.Sprint("provided path cannot be made absolute", dbPath))
panic(fmt.Sprint("lockdown: ", err))
}
err = unix.Unveil(path, "r")
err = unix.Pledge("stdio rpath dns inet flock", "")
if err != nil {
panic(fmt.Sprint("lockdown: %s", err))
}
unix.Pledge("stdio rpath dns inet flock", "")
if err != nil {
panic(fmt.Sprint("lockdown: %s", err))
panic(fmt.Sprint("lockdown: ", err))
}
}
}
3 changes: 1 addition & 2 deletions cmd/shrt/internal/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"net/url"
"os"
"strings"

"djmo.ch/go-shrt"
"djmo.ch/go-shrt/cmd/shrt/internal/base"
Expand Down Expand Up @@ -51,7 +50,7 @@ func runServe(ctx context.Context) {

shrtfile := shrt.NewShrtFile()
fsys := os.DirFS("/").(fs.StatFS)
f, err := fsys.Open(strings.TrimPrefix(cfg.DbPath, "/"))
f, err := fsys.Open(cfg.DbPath)
if err != nil {
log.Println("fs error:", err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions shrtfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (s *ShrtFile) ReadShrtFile(f fs.File) error {
s.mux.Lock()
defer s.mux.Unlock()

s.m = make(map[string]ShrtEntry)

scnr := bufio.NewScanner(f)

for scnr.Scan() {
Expand Down

0 comments on commit 512d53e

Please sign in to comment.