Skip to content

Commit

Permalink
fix: Ensure that service IDs provided to API operations are alphanume…
Browse files Browse the repository at this point in the history
…ric. (#543)

* feat: validate service id to be alphanumeric

* use english character set only for alphanumeric check
  • Loading branch information
domcyrus authored Sep 5, 2024
1 parent 81f6471 commit 5245c1a
Show file tree
Hide file tree
Showing 125 changed files with 2,772 additions and 563 deletions.
20 changes: 10 additions & 10 deletions fastly/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type ListACLsInput struct {

// ListACLs retrieves all resources.
func (c *Client) ListACLs(i *ListACLsInput) ([]*ACL, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -60,8 +60,8 @@ type CreateACLInput struct {

// CreateACL creates a new resource.
func (c *Client) CreateACL(i *CreateACLInput) (*ACL, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -96,8 +96,8 @@ func (c *Client) DeleteACL(i *DeleteACLInput) error {
if i.Name == "" {
return ErrMissingName
}
if i.ServiceID == "" {
return ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return err
}
if i.ServiceVersion == 0 {
return ErrMissingServiceVersion
Expand Down Expand Up @@ -135,8 +135,8 @@ func (c *Client) GetACL(i *GetACLInput) (*ACL, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -173,8 +173,8 @@ func (c *Client) UpdateACL(i *UpdateACLInput) (*ACL, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down
24 changes: 12 additions & 12 deletions fastly/acl_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (c *Client) ListACLEntries(i *ListACLEntriesInput) ([]*ACLEntry, error) {
if i.ACLID == "" {
return nil, ErrMissingACLID
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
p := c.GetACLEntries(&GetACLEntriesInput{
ACLID: i.ACLID,
Expand Down Expand Up @@ -111,8 +111,8 @@ func (c *Client) GetACLEntry(i *GetACLEntryInput) (*ACLEntry, error) {
if i.EntryID == "" {
return nil, ErrMissingID
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}

path := fmt.Sprintf("/service/%s/acl/%s/entry/%s", i.ServiceID, i.ACLID, i.EntryID)
Expand Down Expand Up @@ -152,8 +152,8 @@ func (c *Client) CreateACLEntry(i *CreateACLEntryInput) (*ACLEntry, error) {
if i.ACLID == "" {
return nil, ErrMissingACLID
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}

path := fmt.Sprintf("/service/%s/acl/%s/entry", i.ServiceID, i.ACLID)
Expand Down Expand Up @@ -190,8 +190,8 @@ func (c *Client) DeleteACLEntry(i *DeleteACLEntryInput) error {
if i.EntryID == "" {
return ErrMissingEntryID
}
if i.ServiceID == "" {
return ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return err
}

path := fmt.Sprintf("/service/%s/acl/%s/entry/%s", i.ServiceID, i.ACLID, i.EntryID)
Expand Down Expand Up @@ -240,8 +240,8 @@ func (c *Client) UpdateACLEntry(i *UpdateACLEntryInput) (*ACLEntry, error) {
if i.EntryID == "" {
return nil, ErrMissingID
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}

path := fmt.Sprintf("/service/%s/acl/%s/entry/%s", i.ServiceID, i.ACLID, i.EntryID)
Expand Down Expand Up @@ -292,8 +292,8 @@ func (c *Client) BatchModifyACLEntries(i *BatchModifyACLEntriesInput) error {
if i.ACLID == "" {
return ErrMissingACLID
}
if i.ServiceID == "" {
return ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return err
}
if len(i.Entries) > BatchModifyMaximumOperations {
return ErrMaxExceededEntries
Expand Down
51 changes: 51 additions & 0 deletions fastly/acl_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ func TestClient_ListACLEntries_validation(t *testing.T) {
if err != ErrMissingServiceID {
t.Errorf("bad Service ID: %s", err)
}

_, err = testClient.ListACLEntries(&ListACLEntriesInput{
ACLID: "123",
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad Service ID: %s", err)
}
}

func TestClient_CreateACLEntry_validation(t *testing.T) {
Expand All @@ -197,6 +205,14 @@ func TestClient_CreateACLEntry_validation(t *testing.T) {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.CreateACLEntry(&CreateACLEntryInput{
ACLID: "123",
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}
}

func TestClient_GetACLEntry_validation(t *testing.T) {
Expand All @@ -221,6 +237,15 @@ func TestClient_GetACLEntry_validation(t *testing.T) {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetACLEntry(&GetACLEntryInput{
ACLID: "123",
EntryID: "456",
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}
}

func TestClient_UpdateACLEntry_validation(t *testing.T) {
Expand All @@ -245,6 +270,15 @@ func TestClient_UpdateACLEntry_validation(t *testing.T) {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

_, err = testClient.UpdateACLEntry(&UpdateACLEntryInput{
ACLID: "123",
EntryID: "456",
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}
}

func TestClient_DeleteACLEntry_validation(t *testing.T) {
Expand All @@ -269,6 +303,15 @@ func TestClient_DeleteACLEntry_validation(t *testing.T) {
if err != ErrMissingServiceID {
t.Errorf("bad error: %s", err)
}

err = testClient.DeleteACLEntry(&DeleteACLEntryInput{
ACLID: "123",
EntryID: "456",
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}
}

func TestClient_BatchModifyACLEntries_validation(t *testing.T) {
Expand All @@ -286,6 +329,14 @@ func TestClient_BatchModifyACLEntries_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}

err = testClient.BatchModifyACLEntries(&BatchModifyACLEntriesInput{
ACLID: "123",
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}

oversizedACLEntries := make([]*BatchACLEntry, BatchModifyMaximumOperations+1)
err = testClient.BatchModifyACLEntries(&BatchModifyACLEntriesInput{
ACLID: "123",
Expand Down
41 changes: 41 additions & 0 deletions fastly/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func TestClient_ListACLs_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}

_, err = testClient.ListACLs(&ListACLsInput{
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}

_, err = testClient.ListACLs(&ListACLsInput{
ServiceID: "foo",
ServiceVersion: 0,
Expand All @@ -150,6 +157,13 @@ func TestClient_CreateACL_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}

_, err = testClient.CreateACL(&CreateACLInput{
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}

_, err = testClient.CreateACL(&CreateACLInput{
ServiceID: "foo",
ServiceVersion: 0,
Expand Down Expand Up @@ -177,6 +191,15 @@ func TestClient_GetACL_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetACL(&GetACLInput{
Name: "test",
ServiceVersion: 1,
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetACL(&GetACLInput{
Name: "test",
ServiceID: "foo",
Expand Down Expand Up @@ -205,6 +228,15 @@ func TestClient_UpdateACL_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}

_, err = testClient.UpdateACL(&UpdateACLInput{
Name: "test",
ServiceVersion: 1,
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}

_, err = testClient.UpdateACL(&UpdateACLInput{
Name: "test",
ServiceID: "foo",
Expand Down Expand Up @@ -233,6 +265,15 @@ func TestClient_DeleteACL_validation(t *testing.T) {
t.Errorf("bad error: %s", err)
}

err = testClient.DeleteACL(&DeleteACLInput{
Name: "test",
ServiceVersion: 0,
ServiceID: "not-alphanumeric",
})
if err != ErrServiceIDNotAlphaNumeric {
t.Errorf("bad error: %s", err)
}

err = testClient.DeleteACL(&DeleteACLInput{
Name: "test",
ServiceID: "foo",
Expand Down
20 changes: 10 additions & 10 deletions fastly/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type ListBackendsInput struct {

// ListBackends retrieves all resources.
func (c *Client) ListBackends(i *ListBackendsInput) ([]*Backend, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -150,8 +150,8 @@ type CreateBackendInput struct {

// CreateBackend creates a new resource.
func (c *Client) CreateBackend(i *CreateBackendInput) (*Backend, error) {
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -186,8 +186,8 @@ func (c *Client) GetBackend(i *GetBackendInput) (*Backend, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -284,8 +284,8 @@ func (c *Client) UpdateBackend(i *UpdateBackendInput) (*Backend, error) {
if i.Name == "" {
return nil, ErrMissingName
}
if i.ServiceID == "" {
return nil, ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return nil, err
}
if i.ServiceVersion == 0 {
return nil, ErrMissingServiceVersion
Expand Down Expand Up @@ -320,8 +320,8 @@ func (c *Client) DeleteBackend(i *DeleteBackendInput) error {
if i.Name == "" {
return ErrMissingName
}
if i.ServiceID == "" {
return ErrMissingServiceID
if err := validateServiceID(i.ServiceID); err != nil {
return err
}
if i.ServiceVersion == 0 {
return ErrMissingServiceVersion
Expand Down
Loading

0 comments on commit 5245c1a

Please sign in to comment.