1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00

added filtering for protocol params

This commit is contained in:
Matthias Hannig 2016-12-06 13:02:12 +01:00
parent e06c02fffa
commit afe0297a9d
3 changed files with 96 additions and 1 deletions

50
endpoints/filter.go Normal file
View file

@ -0,0 +1,50 @@
package endpoints
import (
"fmt"
)
/*
* Parameter / Request Validation
*/
// Check if the value is not longer than a given length
func ValidateLength(value string, maxLength int) error {
if len(value) > maxLength {
return fmt.Errorf("Provided param value is too long.")
}
return nil
}
func ValidateCharset(value string, alphabet string) error {
for i := 0; i < len(value); i++ {
c := value[i]
ok := false
for j := 0; j < len(alphabet); j++ {
if alphabet[j] == c {
ok = true
break
}
}
if !ok {
return fmt.Errorf("Invalid character in param value")
}
}
return nil
}
func ValidateProtocolParam(value string) (string, error) {
// Check length
if err := ValidateLength(value, 80); err != nil {
return "", err
}
// Check input
allowed := "ID_AS:.abcdef1234567890"
if err := ValidateCharset(value, allowed); err != nil {
return "", err
}
return value, nil
}

39
endpoints/filter_test.go Normal file
View file

@ -0,0 +1,39 @@
package endpoints
import (
"testing"
)
func TestValidateProtocol(t *testing.T) {
validProtocols := []string{
"ID421_AS11171_123.8.127.19",
"ID429_AS12240_2222:7af8:8:05:01:30bb:0:1",
"AI421_AS11171_123..8..127..19",
}
invalidProtocols := []string{
"ID421_AS11171_123.8.127.lö19",
"Test123",
"ThisValueIsTooLong12345678901234567890123456789012345678901234567890123456789012345678901234567890",
}
// Valid protocol values
for _, param := range validProtocols {
t.Log("Testing valid protocol:", param)
_, err := ValidateProtocolParam(param)
if err != nil {
t.Error(param, "should be a valid protocol param")
}
}
// Invalid protocol values
for _, param := range invalidProtocols {
t.Log("Testing invalid protocol:", param)
_, err := ValidateProtocolParam(param)
if err == nil {
t.Error(param, "should be an invalid protocol param")
}
}
}

View file

@ -1,12 +1,18 @@
package endpoints
import (
"fmt"
"github.com/ecix/birdwatcher/bird"
"github.com/julienschmidt/httprouter"
)
func ProtoRoutes(ps httprouter.Params) (bird.Parsed, bool) {
return bird.RoutesProto(ps.ByName("protocol"))
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesProto(protocol)
}
func TableRoutes(ps httprouter.Params) (bird.Parsed, bool) {