casbin icon indicating copy to clipboard operation
casbin copied to clipboard

keyMatch2("/project/1/member", "/project/1") should return false

Open heww opened this issue 7 years ago • 6 comments

keyMatch2("/project/1/member", "/project/1") should return false

heww avatar Jan 28 '19 16:01 heww

Hi @heww , thanks for the report.

I saw your commit at: https://github.com/goharbor/harbor/commit/1da0a66fe5b8b5bf1945f1d4a66b1df29bb323bc and used that code. It has been fixed in: https://github.com/casbin/casbin/commit/4aa316fb68d44d4cc5ddd98d7e9ec80bc0a8b081. So you can still use the built-in KeyMatch2() now instead of customizing a function with the HEAD of Casbin.

hsluoyz avatar Jan 29 '19 02:01 hsluoyz

I do not know why at the beginning keyMatch2("/project/1/member", "/project/1") will return true?

And before the fix, KeyMatch2 will make key /project/:pid/repo/:rid to be used as ^^/project/[^/]+/repo/[^/]+$$ regex. See more https://play.golang.org/p/yF4m0g0fX2w

Not only keyMatch2, and keyMatch3 may have this problem too.

I think more documents needed for keyMatch2 and keyMatch3.

We will update to new release version which included these fix.

heww avatar Jan 29 '19 04:01 heww

Good point. The duplicated starting char and ending char resulted from the for loop:

func KeyMatch2(key1 string, key2 string) bool {
	key2 = strings.Replace(key2, "/*", "/.*", -1)

	re := regexp.MustCompile(`(.*):[^/]+(.*)`)
	for {
		if !strings.Contains(key2, "/:") {
			break
		}

		key2 = re.ReplaceAllString(key2, "$1[^/]+$2")
	}

	return RegexMatch(key1, "^" + key2 + "$")
}

So your fix also had a good shot for this one :)

BTW, I think keyMatch3() doesn't suffer the same issue, see: https://play.golang.org/p/8SG4uoweTvh

hsluoyz avatar Jan 29 '19 12:01 hsluoyz

FYI, Casbin 1.8.1 has been released: https://github.com/casbin/casbin/releases/tag/v1.8.1

hsluoyz avatar Jan 29 '19 13:01 hsluoyz

I just run KeyMatch3("/abc", "/") using v1.8.1 and it incorrectly returns true. Should I open a new issue to get this fixed for KeyMatch3 as well?

dtarico-suplari avatar Jun 14 '19 22:06 dtarico-suplari

Hi @dtarico-suplari , please open a new issue and link it to this one if you think they are related.

hsluoyz avatar Jun 15 '19 01:06 hsluoyz