Skip to content

fix: correct URL validation and centralize logic #421

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 6 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 4 additions & 10 deletions provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package provider

import (
"context"
"net/url"
"github.com/coder/terraform-provider-coder/v2/provider/helpers"
"regexp"

"github.com/google/uuid"
Expand Down Expand Up @@ -93,15 +93,9 @@ func appResource() *schema.Resource {
Description: "A URL to an icon that will display in the dashboard. View built-in " +
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
"built-in icon with `\"${data.coder_workspace.me.access_url}/icon/<path>\"`.",
ForceNew: true,
Optional: true,
ValidateFunc: func(i any, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
ForceNew: true,
Optional: true,
ValidateFunc: helpers.ValidateURL,
},
"slug": {
Type: schema.TypeString,
Expand Down
57 changes: 57 additions & 0 deletions provider/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,61 @@ func TestApp(t *testing.T) {
})
}
})

t.Run("Icon", func(t *testing.T) {
t.Parallel()

cases := []struct {
name string
icon string
expectError *regexp.Regexp
}{
{
name: "Empty",
icon: "",
},
{
name: "ValidURL",
icon: "/icon/region.svg",
},
{
name: "InvalidURL",
icon: "/icon%.svg",
expectError: regexp.MustCompile("invalid URL escape"),
},
}

for _, c := range cases {
c := c

t.Run(c.name, func(t *testing.T) {
t.Parallel()

config := fmt.Sprintf(`
provider "coder" {}
resource "coder_agent" "dev" {
os = "linux"
arch = "amd64"
}
resource "coder_app" "code-server" {
agent_id = coder_agent.dev.id
slug = "code-server"
display_name = "Testing"
url = "http://localhost:13337"
open_in = "slim-window"
icon = "%s"
}
`, c.icon)

resource.Test(t, resource.TestCase{
ProviderFactories: coderFactory(),
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: config,
ExpectError: c.expectError,
}},
})
})
}
})
}
19 changes: 19 additions & 0 deletions provider/helpers/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package helpers

import (
"fmt"
"net/url"
)

// ValidateURL checks that the given value is a valid URL string.
// Example: for `icon = "/icon/region.svg"`, value is `/icon/region.svg` and label is `icon`.
func ValidateURL(value interface{}, label string) ([]string, []error) {
val, ok := value.(string)
if !ok {
return nil, []error{fmt.Errorf("expected %q to be a string", label)}
}
if _, err := url.Parse(val); err != nil {
return nil, []error{err}
}
return nil, nil
}
15 changes: 4 additions & 11 deletions provider/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package provider

import (
"context"
"net/url"

"github.com/coder/terraform-provider-coder/v2/provider/helpers"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -56,15 +55,9 @@ func metadataResource() *schema.Resource {
Description: "A URL to an icon that will display in the dashboard. View built-in " +
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
"built-in icon with `\"${data.coder_workspace.me.access_url}/icon/<path>\"`.",
ForceNew: true,
Optional: true,
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
ForceNew: true,
Optional: true,
ValidateFunc: helpers.ValidateURL,
},
"daily_cost": {
Type: schema.TypeInt,
Expand Down
26 changes: 7 additions & 19 deletions provider/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"github.com/coder/terraform-provider-coder/v2/provider/helpers"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -223,15 +223,9 @@ func parameterDataSource() *schema.Resource {
Description: "A URL to an icon that will display in the dashboard. View built-in " +
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
"built-in icon with `\"${data.coder_workspace.me.access_url}/icon/<path>\"`.",
ForceNew: true,
Optional: true,
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
ForceNew: true,
Optional: true,
ValidateFunc: helpers.ValidateURL,
},
"option": {
Type: schema.TypeList,
Expand Down Expand Up @@ -263,15 +257,9 @@ func parameterDataSource() *schema.Resource {
Description: "A URL to an icon that will display in the dashboard. View built-in " +
"icons [here](https://github.com/coder/coder/tree/main/site/static/icon). Use a " +
"built-in icon with `\"${data.coder_workspace.me.access_url}/icon/<path>\"`.",
ForceNew: true,
Optional: true,
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
ForceNew: true,
Optional: true,
ValidateFunc: helpers.ValidateURL,
},
},
},
Expand Down
36 changes: 29 additions & 7 deletions provider/parameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,19 @@ func TestParameter(t *testing.T) {
data "coder_parameter" "region" {
name = "Region"
type = "string"
default = "2"
default = "1"
option {
name = "1"
value = "1"
icon = "/icon/code.svg"
description = "Something!"
}
option {
name = "2"
value = "2"
}
}
`,
Check: func(state *terraform.ResourceState) {
for key, expected := range map[string]string{
"name": "Region",
"option.#": "2",
"option.#": "1",
"option.0.name": "1",
"option.0.value": "1",
"option.0.icon": "/icon/code.svg",
Expand Down Expand Up @@ -665,7 +661,33 @@ data "coder_parameter" "region" {
}
`,
ExpectError: regexp.MustCompile("ephemeral parameter requires the default property"),
}} {
}, {
Name: "InvalidIconURL",
Config: `
data "coder_parameter" "region" {
name = "Region"
type = "string"
icon = "/icon%.svg"
}
`,
ExpectError: regexp.MustCompile("invalid URL escape"),
}, {
Name: "OptionInvalidIconURL",
Config: `
data "coder_parameter" "region" {
name = "Region"
type = "string"
option {
name = "1"
value = "1"
icon = "/icon%.svg"
description = "Something!"
}
}
`,
ExpectError: regexp.MustCompile("invalid URL escape"),
},
} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
Expand Down
11 changes: 3 additions & 8 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"github.com/coder/terraform-provider-coder/v2/provider/helpers"
"net/url"
"reflect"
"strings"
Expand All @@ -26,14 +27,8 @@ func New() *schema.Provider {
Optional: true,
// The "CODER_AGENT_URL" environment variable is used by default
// as the Access URL when generating scripts.
DefaultFunc: schema.EnvDefaultFunc("CODER_AGENT_URL", "https://mydeployment.coder.com"),
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
DefaultFunc: schema.EnvDefaultFunc("CODER_AGENT_URL", "https://mydeployment.coder.com"),
ValidateFunc: helpers.ValidateURL,
},
},
ConfigureContextFunc: func(c context.Context, resourceData *schema.ResourceData) (interface{}, diag.Diagnostics) {
Expand Down
Loading