Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path regex check to locks #174

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rules/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rules
import (
"fmt"
"os"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -166,10 +167,16 @@ func (e *v3Engine) SetWatcherWrapper(watcherWrapper WrapWatcher) {
e.watcherWrapper = watcherWrapper
}

// valid path patterns must be alphanumeric and may only contain select special characters (:/"'_.,*=-)
var validPath = regexp.MustCompile(`^[[:alnum:] \#\:\/\"\'\_\.\,\*\=\-]*$`)

func (e *v3Engine) AddRule(rule DynamicRule,
lockPattern string,
callback V3RuleTaskCallback,
options ...RuleOption) {
if !validPath.MatchString(lockPattern) || !strings.Contains(lockPattern, "lock") {
e.logger.Fatal("Path contains an invalid character or does not contain \"lock\"")
}
e.addRuleWithIface(rule, lockPattern, callback, options...)
}

Expand Down
2 changes: 1 addition & 1 deletion v3enginetest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func main() {
doneFalse := "false"
doneRule, err := rules.NewEqualsLiteralRule(donePath, &doneFalse)
check(err)
engine.AddRule(doneRule, "/rulesEngineDone/:id", func(task *rules.V3RuleTask) {
engine.AddRule(doneRule, "/rulesEngineDone/:id/lock", func(task *rules.V3RuleTask) {
path := task.Attr.Format(donePath)
doneTrue := "true"
_, err := kv.Put(task.Context, path, doneTrue)
Expand Down