-
Notifications
You must be signed in to change notification settings - Fork 43
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 DID static validation without tests #291
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/cheqd/cheqd-node/x/cheqd/utils" | ||
"regexp" | ||
) | ||
|
||
var SplitDIDRegexp, _ = regexp.Compile(`did:([^:]+?)(:([^:]+?))?:([^:]+)$`) | ||
var DidNamespaceRegexp, _ = regexp.Compile(`^[a-zA-Z0-9]$`) | ||
// Base58 only allowed (without OolI and 0) | ||
var UniqueIDRegexp, _ = regexp.Compile(`^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]*$`) | ||
// That for groups: | ||
// Example: did:cheqd:testnet:fafdsffq11213343/path-to-s/ome-external-resource?query#key1???#123 | ||
askolesov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 1 - [^/?#]* - all the symbols except / and ? and # . This is the DID part (did:cheqd:testnet:fafdsffq11213343) | ||
// 2 - [^?#]* - all the symbols except ? and #. it means te section started from /, path-abempty (/path-to-s/ome-external-resource) | ||
// 3 - \?([^#]*) - group for `query` part but with ? symbol (?query) | ||
// 4 - [^#]* - group inside query string, match only exact query (query) | ||
// 5 - #([^#]+[\$]?) - group for fragment, starts with #, includes # (#key1???) | ||
askolesov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 6 - [^#]+[\$]? - fragment only (key1???) | ||
// {0,1} - means that number of fragments can be only 0 or 1 | ||
// Amount of query is not limited. | ||
askolesov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var SplitDIDURL, _ = regexp.Compile(`([^/?#]*)?([^?#]*)(\?([^#]*)){0,1}(#([^#]+$)){0,1}$`) | ||
var DIDPathAbemptyRegexp, _ = regexp.Compile(`^[/a-zA-Z0-9\-\.\_\~\%\!\$\&\'\(\)\*\+\,\;\=\:\@]+$`) | ||
var DIDQueryRegexp, _ = regexp.Compile(`^[/a-zA-Z0-9\-\.\_\~\%\!\$\&\'\(\)\*\+\,\;\=\:\@\/\?]*$`) | ||
var DIDFragmentRegexp, _ = regexp.Compile(`^[/a-zA-Z0-9\-\.\_\~\%\!\$\&\'\(\)\*\+\,\;\=\:\@\/\?]*$`) | ||
|
||
//[^/]+(/[^\?#]*)?(\?[^#]+)?(#.+)? | ||
//Old implementation | ||
//var DidForbiddenSymbolsRegexp, _ = regexp.Compile(`^[^#?&/\\]+$`) | ||
// | ||
//func SplitDidUrlIntoDidAndFragment(didUrl string) (string, string) { | ||
// fragments := strings.Split(didUrl, "#") | ||
// return fragments[0], fragments[1] | ||
//} | ||
// | ||
//func IsDidFragment(prefix string, didUrl string) bool { | ||
// if !strings.Contains(didUrl, "#") { | ||
// return false | ||
// } | ||
// | ||
// if didUrl[0] == '#' { | ||
// return true | ||
// } | ||
// | ||
// did, _ := SplitDidUrlIntoDidAndFragment(didUrl) | ||
// return IsValidDid(prefix, did) | ||
//} | ||
// | ||
//func IsFullDidFragment(prefix string, didUrl string) bool { | ||
// if !strings.Contains(didUrl, "#") { | ||
// return false | ||
// } | ||
// | ||
// did, _ := SplitDidUrlIntoDidAndFragment(didUrl) | ||
// return IsValidDid(prefix, did) | ||
//} | ||
// | ||
//func ResolveId(did string, methodId string) string { | ||
// result := methodId | ||
// | ||
// methodDid, methodFragment := SplitDidUrlIntoDidAndFragment(methodId) | ||
// if len(methodDid) == 0 { | ||
// result = did + "#" + methodFragment | ||
// } | ||
// | ||
// return result | ||
//} | ||
// | ||
//func IsNotValidDIDArray(prefix string, array []string) (bool, int) { | ||
// for i, did := range array { | ||
// if !IsValidDid(prefix, did) { | ||
// return true, i | ||
// } | ||
// } | ||
// | ||
// return false, 0 | ||
//} | ||
// | ||
//func IsNotValidDIDArrayFragment(prefix string, array []string) (bool, int) { | ||
// for i, did := range array { | ||
// if !IsDidFragment(prefix, did) { | ||
// return true, i | ||
// } | ||
// } | ||
// | ||
// return false, 0 | ||
//} | ||
// | ||
//func IsValidDid(prefix string, did string) bool { | ||
// if len(did) == 0 { | ||
// return false | ||
// } | ||
// | ||
// if !DidForbiddenSymbolsRegexp.MatchString(did) { | ||
// return false | ||
// } | ||
// | ||
// // FIXME: Empty namespace must be allowed even if namespace is set in state | ||
// // https://github.com/cheqd/cheqd-node/blob/main/architecture/adr-list/adr-002-cheqd-did-method.md#method-specific-identifier | ||
// return strings.HasPrefix(did, prefix) | ||
//} | ||
|
||
|
||
// DID | ||
|
||
func ValidateDID(did string, method string, allowedNamespaces []string) error { | ||
method, namespace, unique_id := SplitDID(did) | ||
|
||
// check method | ||
if method != method { | ||
return ErrStaticDIDBadMethod.Wrap(method) | ||
} | ||
// check namespaces | ||
if !DidNamespaceRegexp.MatchString(namespace) || !utils.Contains(allowedNamespaces, namespace) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move regexp matches to the split method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So validate will only compare namespace / method with passed parameters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it be cool to throw the exception while splitting the DID? From my point of view it's a phase of validation. |
||
return ErrStaticDIDNamespaceNotAllowed.Wrap(namespace) | ||
} | ||
// check unique-id | ||
err := ValidateUniqueId(unique_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one too |
||
|
||
return err | ||
} | ||
|
||
func ValidateUniqueId(unique_id string) error { | ||
// Length should be 16 or 32 symbols | ||
if len(unique_id) != 16 && len(unique_id) != 32 { | ||
return ErrStaticDIDBadUniqueIDLen.Wrap(unique_id) | ||
} | ||
// Base58 check | ||
if !UniqueIDRegexp.MatchString(unique_id) { | ||
return ErrStaticDIDNotBase58ID.Wrap(unique_id) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IsValidDID(did string, method string, allowedNamespaces []string) bool { | ||
err := ValidateDID(did, method, allowedNamespaces) | ||
return err == nil | ||
} | ||
|
||
// SplitDID panics if did is not valid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't panic :) Return an error |
||
func SplitDID(did string) (method string, namespace string, id string) { | ||
// Example: did:cheqd:testnet:base58str1ng1111 | ||
// match [0] - the whole string | ||
// match [1] - cheqd - method | ||
// match [2] - :testnet | ||
// match [3] - testnet - namespace | ||
// match [4] - base58str1ng1111 - id | ||
match := SplitDIDRegexp.FindStringSubmatch(did) | ||
if len(match) > 0 { | ||
return match[1], match[3], match[4] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's throw an error otherwise |
||
|
||
return "", "", "" | ||
} | ||
|
||
// SplitDIDUrl panics if did cannot be splitted properly | ||
func SplitDIDUrl(didUrl string) (did string, path string , query string, fragment string) { | ||
match := SplitDIDURL.FindStringSubmatch(didUrl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's check for matches count There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we check for matches count? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It matches only once for Golang regexps... I checked it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, The last changes in regexp doesn't match in case of fragment being |
||
return match[1], match[2], match[4], match[6] | ||
} | ||
|
||
|
||
// DIDUrl: did:namespace:id[/path][?query][#fragment] | ||
// TODO: Can path, query, fragment be set at the same time? | ||
// TODO: Is service -> id URI or DIDUrl? What should we support? | ||
// https://www.w3.org/TR/did-core/#did-url-syntax | ||
|
||
func ValidateDIDUrl(didUrl string, method string, allowedNamespaces []string) error { | ||
did, path, query, fragment := SplitDIDUrl(didUrl) | ||
// Validate DID | ||
err := ValidateDID(did, method, allowedNamespaces) | ||
if err != nil { | ||
return err | ||
} | ||
// Validate path | ||
err = ValidatePath(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And let's move those checks to the split method as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need to mix validation and splitting? |
||
if err != nil { | ||
return err | ||
} | ||
// Validate query | ||
err = ValidateQuery(query) | ||
if err != nil { | ||
return err | ||
} | ||
// Validate fragment | ||
err = ValidateFragment(fragment) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ValidateFragment(fragment string) error { | ||
if !DIDFragmentRegexp.MatchString(fragment) { | ||
return ErrStaticDIDURLFragmentNotValid.Wrap(fragment) | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateQuery(query string) error { | ||
if !DIDQueryRegexp.MatchString(query) { | ||
return ErrStaticDIDURLQueryNotValid.Wrap(query) | ||
} | ||
return nil | ||
} | ||
|
||
func ValidatePath(path string) error { | ||
if !DIDPathAbemptyRegexp.MatchString(path) { | ||
return ErrStaticDIDURLPathAbemptyNotValid.Wrap(path) | ||
} | ||
return nil | ||
} | ||
|
||
func IsValidDIDUrl(didUrl string, method string, allowedNamespaces []string) bool { | ||
err := ValidateDIDUrl(didUrl, method, allowedNamespaces) | ||
|
||
return nil == err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package types | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestIsDid(t *testing.T) { | ||
cases := []struct { | ||
valid bool | ||
did string | ||
}{ | ||
{true, "did:cheqd:test:wyywywywyw"}, | ||
{true, "did:cheqd:test:wyywywywyw:sdadasda"}, | ||
{false, "did1:cheqd:test:wyywywywyw:sdadasda"}, | ||
{false, "did:cheqd2:test:wyywywywyw:sdadasda"}, | ||
{false, "did:cheqd:test4:wyywywywyw:sdadasda"}, | ||
{false, ""}, | ||
{false, "did:cheqd"}, | ||
{false, "did:cheqd:test"}, | ||
{false, "did:cheqd:test:dsdasdad#weqweqwew"}, | ||
{false, "did:cheqd:test:sdasdasdasd/qeweqweqwee"}, | ||
{false, "did:cheqd:test:sdasdasdasd?=qeweqweqwee"}, | ||
{false, "did:cheqd:test:sdasdasdasd&qeweqweqwee"}, | ||
} | ||
|
||
for _, tc := range cases { | ||
isDid := IsValidDID(tc.did, "cheqd", []string{"testnet"}) | ||
|
||
if tc.valid { | ||
require.True(t, isDid) | ||
} else { | ||
require.False(t, isDid) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.