Skip to content

Commit

Permalink
verify polling URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendrixMSFT committed Jun 8, 2021
1 parent 72a12ea commit c23a499
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions sdk/armcore/internal/pollers/async/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package async

import (
"errors"
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/sdk/armcore/internal/pollers"
Expand Down Expand Up @@ -42,6 +43,9 @@ func New(resp *azcore.Response, finalState string, pollerID string) (*Poller, er
if asyncURL == "" {
return nil, errors.New("response is missing Azure-AsyncOperation header")
}
if !pollers.IsValidURL(asyncURL) {
return nil, fmt.Errorf("invalid polling URL %s", asyncURL)
}
p := &Poller{
Type: pollers.MakeID(pollerID, "async"),
AsyncURL: asyncURL,
Expand Down
7 changes: 6 additions & 1 deletion sdk/armcore/internal/pollers/loc/loc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package loc

import (
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/sdk/armcore/internal/pollers"
Expand All @@ -27,9 +28,13 @@ type Poller struct {
// New creates a new Poller from the provided initial response.
func New(resp *azcore.Response, pollerID string) (*Poller, error) {
azcore.Log().Write(azcore.LogLongRunningOperation, "Using Location poller.")
locURL := resp.Header.Get(pollers.HeaderLocation)
if !pollers.IsValidURL(locURL) {
return nil, fmt.Errorf("invalid polling URL %s", locURL)
}
p := &Poller{
Type: pollers.MakeID(pollerID, "loc"),
PollURL: resp.Header.Get(pollers.HeaderLocation),
PollURL: locURL,
CurState: "InProgress",
}
return p, nil
Expand Down
7 changes: 7 additions & 0 deletions sdk/armcore/internal/pollers/pollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand Down Expand Up @@ -108,6 +109,12 @@ func GetProvisioningState(resp *azcore.Response) (string, error) {
return "", ErrNoProvisioningState
}

// IsValidURL verifies that the URL is valid and absolute.
func IsValidURL(s string) bool {
u, err := url.Parse(s)
return err == nil && u.IsAbs()
}

// MakeID returns the unique poller identifier in the format pollerID;poller.
func MakeID(pollerID string, kind string) string {
return fmt.Sprintf("%s;%s", pollerID, kind)
Expand Down
9 changes: 9 additions & 0 deletions sdk/armcore/internal/pollers/pollers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,12 @@ func TestMakeID(t *testing.T) {
t.Fatalf("unexpected poller kind %s", p)
}
}

func TestIsValidURL(t *testing.T) {
if IsValidURL("/foo") {
t.Fatal("unexpected valid URL")
}
if !IsValidURL("https://foo.bar/baz") {
t.Fatal("expected valid URL")
}
}

0 comments on commit c23a499

Please sign in to comment.