From 3cf716644fe2105311c7f9aeadb8ba153f9ed174 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 2 Aug 2021 13:49:17 +0100 Subject: [PATCH] Always validate URI schema --- internal/langserver/handlers/command/init.go | 5 +++++ .../langserver/handlers/command/module_callers.go | 4 ++++ internal/langserver/handlers/command/validate.go | 5 +++++ internal/lsp/file_handler_unix_test.go | 5 +++-- internal/uri/uri.go | 5 +++++ internal/uri/uri_test.go | 12 ++++++++++++ 6 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 internal/uri/uri_test.go diff --git a/internal/langserver/handlers/command/init.go b/internal/langserver/handlers/command/init.go index de1dc5c76..3ac3b8e94 100644 --- a/internal/langserver/handlers/command/init.go +++ b/internal/langserver/handlers/command/init.go @@ -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) { @@ -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) diff --git a/internal/langserver/handlers/command/module_callers.go b/internal/langserver/handlers/command/module_callers.go index 13e78527e..73357bfc4 100644 --- a/internal/langserver/handlers/command/module_callers.go +++ b/internal/langserver/handlers/command/module_callers.go @@ -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 diff --git a/internal/langserver/handlers/command/validate.go b/internal/langserver/handlers/command/validate.go index fab24bfc1..35f8d2ac4 100644 --- a/internal/langserver/handlers/command/validate.go +++ b/internal/langserver/handlers/command/validate.go @@ -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) { @@ -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) diff --git a/internal/lsp/file_handler_unix_test.go b/internal/lsp/file_handler_unix_test.go index 3dba71e8b..6a35e4673 100644 --- a/internal/lsp/file_handler_unix_test.go +++ b/internal/lsp/file_handler_unix_test.go @@ -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" diff --git a/internal/uri/uri.go b/internal/uri/uri.go index 44e245a7b..0cb3c897b 100644 --- a/internal/uri/uri.go +++ b/internal/uri/uri.go @@ -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) } diff --git a/internal/uri/uri_test.go b/internal/uri/uri_test.go new file mode 100644 index 000000000..b9af80d66 --- /dev/null +++ b/internal/uri/uri_test.go @@ -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) + } +}