mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
feat!: simplified rule with regex instead of boolean
BREAKING CHANGE: the "regex" field in the rule is now a regular expression instead of a boolean.
This commit is contained in:
@@ -110,8 +110,7 @@ users:
|
|||||||
modify: true
|
modify: true
|
||||||
# With this rule, the user CAN modify all files ending with .js. It uses
|
# With this rule, the user CAN modify all files ending with .js. It uses
|
||||||
# a regular expression.
|
# a regular expression.
|
||||||
- path: "^*.js$"
|
- regex: "^.+\.js$"
|
||||||
regex: true
|
|
||||||
modify: true
|
modify: true
|
||||||
|
|
||||||
# CORS configuration
|
# CORS configuration
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ module github.com/hacdias/webdav/v5
|
|||||||
go 1.22
|
go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/go-viper/mapstructure/v2 v2.0.0
|
||||||
github.com/rs/cors v1.11.0
|
github.com/rs/cors v1.11.0
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
|
|||||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||||
|
github.com/go-viper/mapstructure/v2 v2.0.0 h1:dhn8MZ1gZ0mzeodTG3jt5Vj/o87xZKuNAprG2mQfMfc=
|
||||||
|
github.com/go-viper/mapstructure/v2 v2.0.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
|
|||||||
+6
-1
@@ -6,6 +6,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-viper/mapstructure/v2"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@@ -101,7 +102,11 @@ func ParseConfig(filename string, flags *pflag.FlagSet) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg := &Config{}
|
cfg := &Config{}
|
||||||
err = v.Unmarshal(cfg)
|
err = v.Unmarshal(cfg, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
|
||||||
|
mapstructure.StringToTimeDurationHookFunc(),
|
||||||
|
mapstructure.StringToSliceHookFunc(","),
|
||||||
|
mapstructure.TextUnmarshallerHookFunc(),
|
||||||
|
)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-7
@@ -176,11 +176,9 @@ auth: false
|
|||||||
scope: /
|
scope: /
|
||||||
modify: true
|
modify: true
|
||||||
rules:
|
rules:
|
||||||
- path: '^.+\.js$'
|
- regex: '^.+\.js$'
|
||||||
regex: true
|
|
||||||
modify: true
|
modify: true
|
||||||
- path: /public/access/
|
- path: /public/access/
|
||||||
regex: false
|
|
||||||
modify: true`
|
modify: true`
|
||||||
|
|
||||||
cfg := writeAndParseConfig(t, content, ".yaml")
|
cfg := writeAndParseConfig(t, content, ".yaml")
|
||||||
@@ -189,12 +187,12 @@ rules:
|
|||||||
require.Len(t, cfg.Rules, 2)
|
require.Len(t, cfg.Rules, 2)
|
||||||
|
|
||||||
require.Empty(t, cfg.Rules[0].Path)
|
require.Empty(t, cfg.Rules[0].Path)
|
||||||
require.NotNil(t, cfg.Rules[0].Regexp)
|
require.NotNil(t, cfg.Rules[0].Regex)
|
||||||
require.True(t, cfg.Rules[0].Regexp.MatchString("/my/path/to/file.js"))
|
require.True(t, cfg.Rules[0].Regex.MatchString("/my/path/to/file.js"))
|
||||||
require.False(t, cfg.Rules[0].Regexp.MatchString("/my/path/to/file.ts"))
|
require.False(t, cfg.Rules[0].Regex.MatchString("/my/path/to/file.ts"))
|
||||||
|
|
||||||
require.NotEmpty(t, cfg.Rules[1].Path)
|
require.NotEmpty(t, cfg.Rules[1].Path)
|
||||||
require.Nil(t, cfg.Rules[1].Regexp)
|
require.Nil(t, cfg.Rules[1].Regex)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigEnv(t *testing.T) {
|
func TestConfigEnv(t *testing.T) {
|
||||||
|
|||||||
+1
-2
@@ -161,8 +161,7 @@ users:
|
|||||||
- username: basic
|
- username: basic
|
||||||
password: basic
|
password: basic
|
||||||
rules:
|
rules:
|
||||||
- path: "^.+.js$"
|
- regex: "^.+.js$"
|
||||||
regex: true
|
|
||||||
modify: false
|
modify: false
|
||||||
- path: "/b"
|
- path: "/b"
|
||||||
modify: false
|
modify: false
|
||||||
|
|||||||
+6
-13
@@ -1,6 +1,7 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -15,23 +16,15 @@ var readMethods = []string{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Rule struct {
|
type Rule struct {
|
||||||
Regex bool
|
|
||||||
Allow bool
|
Allow bool
|
||||||
Modify bool
|
Modify bool
|
||||||
Path string
|
Path string
|
||||||
// TODO: remove Regex and replace by this. It encodes
|
Regex *regexp.Regexp
|
||||||
Regexp *regexp.Regexp `mapstructure:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rule) Validate() error {
|
func (r *Rule) Validate() error {
|
||||||
if r.Regex {
|
if r.Regex != nil && r.Path != "" {
|
||||||
rp, err := regexp.Compile(r.Path)
|
return errors.New("invalid rule: cannot define both regex and path")
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid rule: %w", err)
|
|
||||||
}
|
|
||||||
r.Regexp = rp
|
|
||||||
r.Path = ""
|
|
||||||
r.Regex = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -39,8 +32,8 @@ func (r *Rule) Validate() error {
|
|||||||
|
|
||||||
// Matches checks if [Rule] matches the given path.
|
// Matches checks if [Rule] matches the given path.
|
||||||
func (r *Rule) Matches(path string) bool {
|
func (r *Rule) Matches(path string) bool {
|
||||||
if r.Regexp != nil {
|
if r.Regex != nil {
|
||||||
return r.Regexp.MatchString(path)
|
return r.Regex.MatchString(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.HasPrefix(path, r.Path)
|
return strings.HasPrefix(path, r.Path)
|
||||||
|
|||||||
Reference in New Issue
Block a user