-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from fabiante/fix/api-validation
- Loading branch information
Showing
10 changed files
with
123 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package api | ||
|
||
import "github.com/gin-gonic/gin" | ||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func SetupRouting(r gin.IRouter, s *Server) { | ||
r.Use(validPathVar("domain", regexNamed)) | ||
r.Use(validPathVar("name", regexNamed)) | ||
|
||
// Resolve endpoints | ||
r.GET("/r/:domain/:name", s.Resolve) | ||
|
||
// Admin endpoints | ||
r.PUT("/a/domains/:domain/purls/:name", s.Save) | ||
r.PUT("/a/domains/:domain/purls/:name", s.SavePURL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
var regexNamed *regexp.Regexp | ||
|
||
func init() { | ||
// regexNamed is used to validate everything that has a name. See OpenAPI | ||
// for more information. | ||
regexNamed = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) | ||
} | ||
|
||
// validPathVar is a middleware that validates a path variable against a | ||
// regular expression. If the path variable does not match the regular | ||
// expression, the middleware aborts the request with status 400. | ||
func validPathVar(key string, regex *regexp.Regexp) gin.HandlerFunc { | ||
return func(context *gin.Context) { | ||
if !regex.MatchString(context.Param(key)) { | ||
err := fmt.Sprintf("path variable %q does not match regex %s", key, regex.String()) | ||
context.AbortWithStatusJSON(400, err) | ||
return | ||
} | ||
context.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package specs | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/fabiante/persurl/tests/dsl" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAdministration(t *testing.T, admin dsl.AdminAPI) { | ||
t.Run("administration", func(t *testing.T) { | ||
t.Run("can't create invalid PURL", func(t *testing.T) { | ||
invalid := []*dsl.PURL{ | ||
// empty | ||
dsl.NewPURL("", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL("valid", "", mustParseURL("example.com")), | ||
// whitespace | ||
dsl.NewPURL("a b", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL("valid", "a b", mustParseURL("example.com")), | ||
// url encoded whitespace | ||
dsl.NewPURL("a%20b", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL("valid", "a%20b", mustParseURL("example.com")), | ||
// random characters | ||
dsl.NewPURL("^", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL("~", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL(":", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL(",", "valid", mustParseURL("example.com")), | ||
dsl.NewPURL("`", "valid", mustParseURL("example.com")), | ||
} | ||
|
||
for i, purl := range invalid { | ||
t.Run(fmt.Sprintf("invalid[%d]", i), func(t *testing.T) { | ||
err := admin.CreatePurl(purl) | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, dsl.ErrBadRequest) | ||
}) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters