Skip to content

fix: Use url.PathEncode for dynamic URL strings #344

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

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions account_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -119,6 +120,7 @@ func (c *Client) ListOAuthClients(ctx context.Context, opts *ListOptions) ([]OAu
// GetOAuthClient gets the OAuthClient with the provided ID
func (c *Client) GetOAuthClient(ctx context.Context, clientID string) (*OAuthClient, error) {
req := c.R(ctx).SetResult(&OAuthClient{})
clientID = url.PathEscape(clientID)
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
Expand Down Expand Up @@ -153,6 +155,9 @@ func (c *Client) UpdateOAuthClient(ctx context.Context, clientID string, opts OA
}

req := c.R(ctx).SetResult(&OAuthClient{}).SetBody(string(body))

clientID = url.PathEscape(clientID)

e := fmt.Sprintf("account/oauth-clients/%s", clientID)
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
Expand All @@ -164,6 +169,7 @@ func (c *Client) UpdateOAuthClient(ctx context.Context, clientID string, opts OA

// DeleteOAuthClient deletes the OAuthClient with the specified id
func (c *Client) DeleteOAuthClient(ctx context.Context, clientID string) error {
clientID = url.PathEscape(clientID)
e := fmt.Sprintf("account/oauth-clients/%s", clientID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
3 changes: 3 additions & 0 deletions account_user_grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
)

type GrantPermissionLevel string
Expand Down Expand Up @@ -68,6 +69,7 @@ type UserGrantsUpdateOptions struct {
}

func (c *Client) GetUserGrants(ctx context.Context, username string) (*UserGrants, error) {
username = url.PathEscape(username)
e := fmt.Sprintf("account/users/%s/grants", username)
req := c.R(ctx).SetResult(&UserGrants{})
r, err := coupleAPIErrors(req.Get(e))
Expand All @@ -84,6 +86,7 @@ func (c *Client) UpdateUserGrants(ctx context.Context, username string, opts Use
return nil, err
}

username = url.PathEscape(username)
e := fmt.Sprintf("account/users/%s/grants", username)
req := c.R(ctx).SetResult(&UserGrants{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
Expand Down
4 changes: 4 additions & 0 deletions account_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -105,6 +106,7 @@ func (c *Client) ListUsers(ctx context.Context, opts *ListOptions) ([]User, erro

// GetUser gets the user with the provided ID
func (c *Client) GetUser(ctx context.Context, userID string) (*User, error) {
userID = url.PathEscape(userID)
e := fmt.Sprintf("account/users/%s", userID)
req := c.R(ctx).SetResult(&User{})
r, err := coupleAPIErrors(req.Get(e))
Expand Down Expand Up @@ -140,6 +142,7 @@ func (c *Client) UpdateUser(ctx context.Context, userID string, opts UserUpdateO
return nil, err
}

userID = url.PathEscape(userID)
e := fmt.Sprintf("account/users/%s", userID)
req := c.R(ctx).SetResult(&User{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
Expand All @@ -152,6 +155,7 @@ func (c *Client) UpdateUser(ctx context.Context, userID string, opts UserUpdateO

// DeleteUser deletes the User with the specified id
func (c *Client) DeleteUser(ctx context.Context, userID string) error {
userID = url.PathEscape(userID)
e := fmt.Sprintf("account/users/%s", userID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
9 changes: 8 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ func (c *Client) updateHostURL() {
apiProto = c.apiProto
}

c.resty.SetHostURL(fmt.Sprintf("%s://%s/%s", apiProto, baseURL, apiVersion))
c.resty.SetHostURL(
fmt.Sprintf(
"%s://%s/%s",
apiProto,
baseURL,
url.PathEscape(apiVersion),
),
)
}

// SetRootCertificate adds a root certificate to the underlying TLS client config
Expand Down
3 changes: 3 additions & 0 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -236,6 +237,7 @@ func (c *Client) ListDatabaseEngines(ctx context.Context, opts *ListOptions) ([]

// GetDatabaseEngine returns a specific Database Engine. This endpoint is cached by default.
func (c *Client) GetDatabaseEngine(ctx context.Context, _ *ListOptions, engineID string) (*DatabaseEngine, error) {
engineID = url.PathEscape(engineID)
e := fmt.Sprintf("databases/engines/%s", engineID)

if result := c.getCachedResponse(e); result != nil {
Expand Down Expand Up @@ -279,6 +281,7 @@ func (c *Client) ListDatabaseTypes(ctx context.Context, opts *ListOptions) ([]Da

// GetDatabaseType returns a specific Database Type. This endpoint is cached by default.
func (c *Client) GetDatabaseType(ctx context.Context, _ *ListOptions, typeID string) (*DatabaseType, error) {
typeID = url.PathEscape(typeID)
e := fmt.Sprintf("databases/types/%s", typeID)

if result := c.getCachedResponse(e); result != nil {
Expand Down
6 changes: 6 additions & 0 deletions images.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"net/url"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -132,6 +133,8 @@ func (c *Client) ListImages(ctx context.Context, opts *ListOptions) ([]Image, er

// GetImage gets the Image with the provided ID
func (c *Client) GetImage(ctx context.Context, imageID string) (*Image, error) {
imageID = url.PathEscape(imageID)

e := fmt.Sprintf("images/%s", imageID)
req := c.R(ctx).SetResult(&Image{})
r, err := coupleAPIErrors(req.Get(e))
Expand Down Expand Up @@ -164,6 +167,8 @@ func (c *Client) UpdateImage(ctx context.Context, imageID string, opts ImageUpda
return nil, err
}

imageID = url.PathEscape(imageID)

e := fmt.Sprintf("images/%s", imageID)
req := c.R(ctx).SetResult(&Image{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
Expand All @@ -175,6 +180,7 @@ func (c *Client) UpdateImage(ctx context.Context, imageID string, opts ImageUpda

// DeleteImage deletes the Image with the specified id
func (c *Client) DeleteImage(ctx context.Context, imageID string) error {
imageID = url.PathEscape(imageID)
e := fmt.Sprintf("images/%s", imageID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
5 changes: 5 additions & 0 deletions instance_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
)

// InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance
Expand Down Expand Up @@ -77,6 +78,7 @@ func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*Ins

// GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address
func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) {
ipaddress = url.PathEscape(ipaddress)
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipaddress)
req := c.R(ctx).SetResult(&InstanceIP{})
r, err := coupleAPIErrors(req.Get(e))
Expand Down Expand Up @@ -116,6 +118,8 @@ func (c *Client) UpdateInstanceIPAddress(ctx context.Context, linodeID int, ipAd
return nil, err
}

ipAddress = url.PathEscape(ipAddress)

e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
Expand All @@ -126,6 +130,7 @@ func (c *Client) UpdateInstanceIPAddress(ctx context.Context, linodeID int, ipAd
}

func (c *Client) DeleteInstanceIPAddress(ctx context.Context, linodeID int, ipAddress string) error {
ipAddress = url.PathEscape(ipAddress)
e := fmt.Sprintf("linode/instances/%d/ips/%s", linodeID, ipAddress)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
2 changes: 2 additions & 0 deletions instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/url"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -409,6 +410,7 @@ func (c *Client) MigrateInstance(ctx context.Context, id int) error {
// simpleInstanceAction is a helper for Instance actions that take no parameters
// and return empty responses `{}` unless they return a standard error
func (c *Client) simpleInstanceAction(ctx context.Context, action string, linodeID int) error {
action = url.PathEscape(action)
e := fmt.Sprintf("linode/instances/%d/%s", linodeID, action)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
Expand Down
2 changes: 2 additions & 0 deletions kernels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linodego
import (
"context"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -64,6 +65,7 @@ func (c *Client) ListKernels(ctx context.Context, opts *ListOptions) ([]LinodeKe

// GetKernel gets the kernel with the provided ID. This endpoint is cached by default.
func (c *Client) GetKernel(ctx context.Context, kernelID string) (*LinodeKernel, error) {
kernelID = url.PathEscape(kernelID)
e := fmt.Sprintf("linode/kernels/%s", kernelID)

if result := c.getCachedResponse(e); result != nil {
Expand Down
2 changes: 2 additions & 0 deletions lke_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -169,6 +170,7 @@ func (c *Client) ListLKEVersions(ctx context.Context, opts *ListOptions) ([]LKEV

// GetLKEVersion gets details about a specific LKE Version. This endpoint is cached by default.
func (c *Client) GetLKEVersion(ctx context.Context, version string) (*LKEVersion, error) {
version = url.PathEscape(version)
e := fmt.Sprintf("lke/versions/%s", version)

if result := c.getCachedResponse(e); result != nil {
Expand Down
2 changes: 2 additions & 0 deletions lke_node_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -170,6 +171,7 @@ func (c *Client) DeleteLKENodePool(ctx context.Context, clusterID, poolID int) e

// DeleteLKENodePoolNode deletes a given node from a node pool
func (c *Client) DeleteLKENodePoolNode(ctx context.Context, clusterID int, nodeID string) error {
nodeID = url.PathEscape(nodeID)
e := fmt.Sprintf("lke/clusters/%d/nodes/%s", clusterID, nodeID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
2 changes: 2 additions & 0 deletions longview_subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linodego
import (
"context"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -50,6 +51,7 @@ func (c *Client) ListLongviewSubscriptions(ctx context.Context, opts *ListOption

// GetLongviewSubscription gets the template with the provided ID
func (c *Client) GetLongviewSubscription(ctx context.Context, templateID string) (*LongviewSubscription, error) {
templateID = url.PathEscape(templateID)
e := fmt.Sprintf("longview/subscriptions/%s", templateID)
req := c.R(ctx).SetResult(&LongviewSubscription{})
r, err := coupleAPIErrors(req.Get(e))
Expand Down
3 changes: 3 additions & 0 deletions network_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -78,6 +79,7 @@ func (c *Client) ListIPAddresses(ctx context.Context, opts *ListOptions) ([]Inst

// GetIPAddress gets the template with the provided ID
func (c *Client) GetIPAddress(ctx context.Context, id string) (*InstanceIP, error) {
id = url.PathEscape(id)
e := fmt.Sprintf("networking/ips/%s", id)
req := c.R(ctx).SetResult(&InstanceIP{})
r, err := coupleAPIErrors(req.Get(e))
Expand All @@ -94,6 +96,7 @@ func (c *Client) UpdateIPAddress(ctx context.Context, id string, opts IPAddressU
return nil, err
}

id = url.PathEscape(id)
e := fmt.Sprintf("networking/ips/%s", id)
req := c.R(ctx).SetResult(&InstanceIP{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
Expand Down
2 changes: 2 additions & 0 deletions network_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linodego
import (
"context"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -40,6 +41,7 @@ func (c *Client) ListIPv6Pools(ctx context.Context, opts *ListOptions) ([]IPv6Ra

// GetIPv6Pool gets the template with the provided ID
func (c *Client) GetIPv6Pool(ctx context.Context, id string) (*IPv6Range, error) {
id = url.PathEscape(id)
e := fmt.Sprintf("networking/ipv6/pools/%s", id)
req := c.R(ctx).SetResult(&IPv6Range{})
r, err := coupleAPIErrors(req.Get(e))
Expand Down
3 changes: 3 additions & 0 deletions network_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -48,6 +49,7 @@ func (c *Client) ListIPv6Ranges(ctx context.Context, opts *ListOptions) ([]IPv6R

// GetIPv6Range gets details about an IPv6 range
func (c *Client) GetIPv6Range(ctx context.Context, ipRange string) (*IPv6Range, error) {
ipRange = url.PathEscape(ipRange)
e := fmt.Sprintf("networking/ipv6/ranges/%s", ipRange)
req := c.R(ctx).SetResult(&IPv6Range{})
r, err := coupleAPIErrors(req.Get(e))
Expand Down Expand Up @@ -75,6 +77,7 @@ func (c *Client) CreateIPv6Range(ctx context.Context, opts IPv6RangeCreateOption

// DeleteIPv6Range deletes an IPv6 Range.
func (c *Client) DeleteIPv6Range(ctx context.Context, ipRange string) error {
ipRange = url.PathEscape(ipRange)
e := fmt.Sprintf("networking/ipv6/ranges/%s", ipRange)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
7 changes: 7 additions & 0 deletions object_storage_bucket_certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
)

type ObjectStorageBucketCert struct {
Expand All @@ -22,6 +23,8 @@ func (c *Client) UploadObjectStorageBucketCert(ctx context.Context, clusterID, b
return nil, err
}

clusterID = url.PathEscape(clusterID)
bucket = url.PathEscape(bucket)
e := fmt.Sprintf("object-storage/buckets/%s/%s/ssl", clusterID, bucket)
req := c.R(ctx).SetResult(&ObjectStorageBucketCert{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
Expand All @@ -33,6 +36,8 @@ func (c *Client) UploadObjectStorageBucketCert(ctx context.Context, clusterID, b

// GetObjectStorageBucketCert gets an ObjectStorageBucketCert
func (c *Client) GetObjectStorageBucketCert(ctx context.Context, clusterID, bucket string) (*ObjectStorageBucketCert, error) {
clusterID = url.PathEscape(clusterID)
bucket = url.PathEscape(bucket)
e := fmt.Sprintf("object-storage/buckets/%s/%s/ssl", clusterID, bucket)
req := c.R(ctx).SetResult(&ObjectStorageBucketCert{})
r, err := coupleAPIErrors(req.Get(e))
Expand All @@ -44,6 +49,8 @@ func (c *Client) GetObjectStorageBucketCert(ctx context.Context, clusterID, buck

// DeleteObjectStorageBucketCert deletes an ObjectStorageBucketCert
func (c *Client) DeleteObjectStorageBucketCert(ctx context.Context, clusterID, bucket string) error {
clusterID = url.PathEscape(clusterID)
bucket = url.PathEscape(bucket)
e := fmt.Sprintf("object-storage/buckets/%s/%s/ssl", clusterID, bucket)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
Expand Down
Loading