Skip to content

Commit

Permalink
Always validate URI schema (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Aug 4, 2021
1 parent 9acdc67 commit 4cf6afa
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/langserver/handlers/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/hashicorp/terraform-ls/internal/uri"
)

func TerraformInitHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) {
Expand All @@ -20,6 +21,10 @@ func TerraformInitHandler(ctx context.Context, args cmd.CommandArgs) (interface{
return nil, fmt.Errorf("%w: expected module uri argument to be set", code.InvalidParams.Err())
}

if !uri.IsURIValid(dirUri) {
return nil, fmt.Errorf("URI %q is not valid", dirUri)
}

dh := ilsp.FileHandlerFromDirURI(lsp.DocumentURI(dirUri))

modMgr, err := lsctx.ModuleManager(ctx)
Expand Down
4 changes: 4 additions & 0 deletions internal/langserver/handlers/command/module_callers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func ModuleCallersHandler(ctx context.Context, args cmd.CommandArgs) (interface{
return nil, fmt.Errorf("%w: expected module uri argument to be set", code.InvalidParams.Err())
}

if !uri.IsURIValid(modUri) {
return nil, fmt.Errorf("URI %q is not valid", modUri)
}

modPath, err := uri.PathFromURI(modUri)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions internal/langserver/handlers/command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/hashicorp/terraform-ls/internal/uri"
)

func TerraformValidateHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) {
Expand All @@ -21,6 +22,10 @@ func TerraformValidateHandler(ctx context.Context, args cmd.CommandArgs) (interf
return nil, fmt.Errorf("%w: expected module uri argument to be set", code.InvalidParams.Err())
}

if !uri.IsURIValid(dirUri) {
return nil, fmt.Errorf("URI %q is not valid", dirUri)
}

dh := ilsp.FileHandlerFromDirURI(lsp.DocumentURI(dirUri))

modMgr, err := lsctx.ModuleManager(ctx)
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/file_handler_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ func TestFileHandler_valid_unix(t *testing.T) {
}

func TestFileHandler_valid_unixDir(t *testing.T) {
fh := FileHandlerFromDirURI(lsp.DocumentURI("/valid/path/to"))
uri := "file:///valid/path/to"
fh := FileHandlerFromDirURI(lsp.DocumentURI(uri))
if !fh.Valid() {
t.Fatalf("Expected %q to be valid", "/valid/path/to")
t.Fatalf("Expected %q to be valid", uri)
}

expectedDir := "/valid/path/to"
Expand Down
5 changes: 5 additions & 0 deletions internal/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ func parseUri(uri string) (string, error) {
return "", err
}

if u.Scheme != "file" {
return "", fmt.Errorf("unexpected scheme %q in URI %q",
u.Scheme, uri)
}

return url.PathUnescape(u.Path)
}
12 changes: 12 additions & 0 deletions internal/uri/uri_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uri

import (
"testing"
)

func TestIsURIValid_invalid(t *testing.T) {
uri := "output:extension-output-%232"
if IsURIValid(uri) {
t.Fatalf("Expected %q to be invalid", uri)
}
}

0 comments on commit 4cf6afa

Please sign in to comment.