Skip to content
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

fix: avoid crash on invalid URIs #969

Merged
merged 1 commit into from
Jun 27, 2022
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
17 changes: 17 additions & 0 deletions internal/langserver/handlers/did_change_workspace_folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,31 @@ import (
"github.com/creachadair/jrpc2"
"github.com/hashicorp/terraform-ls/internal/document"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/uri"
)

func (svc *service) DidChangeWorkspaceFolders(ctx context.Context, params lsp.DidChangeWorkspaceFoldersParams) error {
for _, removed := range params.Event.Removed {
if !uri.IsURIValid(removed.URI) {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
" This is most likely bug, please report it.", removed.URI),
})
continue
}
svc.removeIndexedModule(ctx, removed.URI)
}

for _, added := range params.Event.Added {
if !uri.IsURIValid(added.URI) {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
" This is most likely bug, please report it.", added.URI),
})
continue
}
svc.indexNewModule(ctx, added.URI)
}

Expand Down
20 changes: 18 additions & 2 deletions internal/langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@ package handlers

import (
"context"
"fmt"

"github.com/creachadair/jrpc2"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
"github.com/hashicorp/terraform-ls/internal/uri"
)

func (svc *service) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpenTextDocumentParams) error {
dh := ilsp.HandleFromDocumentURI(params.TextDocument.URI)
docURI := string(params.TextDocument.URI)

// URIs are always checked during initialize request, but
// we still allow single-file mode, therefore invalid URIs
// can still land here, so we check for those.
if !uri.IsURIValid(docURI) {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
" This is most likely bug, please report it.", docURI),
})
return fmt.Errorf("invalid URI: %s", docURI)
}

dh := document.HandleFromURI(docURI)

err := svc.stateStore.DocumentStore.OpenDocument(dh, params.TextDocument.LanguageID,
int(params.TextDocument.Version), []byte(params.TextDocument.Text))
Expand Down
26 changes: 19 additions & 7 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams)
svc.server = jrpc2.ServerFromContext(ctx)

setupTelemetry(expClientCaps, svc, ctx, properties)
defer svc.telemetry.SendEvent(ctx, "initialize", properties)

if params.ClientInfo.Name != "" {
err = ilsp.SetClientName(ctx, params.ClientInfo.Name)
Expand Down Expand Up @@ -126,9 +127,11 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams)
})
}
} else {
if !uri.IsURIValid(string(params.RootURI)) {
rootURI := string(params.RootURI)
if !uri.IsURIValid(rootURI) {
properties["root_uri"] = "invalid"
return serverCaps, fmt.Errorf("URI %q is not valid", params.RootURI)
return serverCaps, fmt.Errorf("Unsupported or invalid URI: %q "+
"This is most likely bug, please report it.", rootURI)
}

err := svc.setupWalker(ctx, params, cfgOpts)
Expand Down Expand Up @@ -162,7 +165,6 @@ func setupTelemetry(expClientCaps lsp.ExpClientCapabilities, svc *service, ctx c
}
svc.logger.Printf("telemetry enabled (version: %d)", tv)
}
defer svc.telemetry.SendEvent(ctx, "initialize", properties)
}

func getTelemetryProperties(out *settings.DecodedOptions) map[string]interface{} {
Expand Down Expand Up @@ -238,7 +240,8 @@ func initializeResult(ctx context.Context) lsp.InitializeResult {
}

func (svc *service) setupWalker(ctx context.Context, params lsp.InitializeParams, options *settings.Options) error {
root := document.DirHandleFromURI(string(params.RootURI))
rootURI := string(params.RootURI)
root := document.DirHandleFromURI(rootURI)

err := lsctx.SetRootDirectory(ctx, root.Path())
if err != nil {
Expand All @@ -261,15 +264,24 @@ func (svc *service) setupWalker(ctx context.Context, params lsp.InitializeParams
}

if len(params.WorkspaceFolders) > 0 {
for _, folderPath := range params.WorkspaceFolders {
modPath := document.DirHandleFromURI(folderPath.URI)
for _, folder := range params.WorkspaceFolders {
if !uri.IsURIValid(folder.URI) {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
" This is most likely bug, please report it.", folder.URI),
})
continue
}

modPath := document.DirHandleFromURI(folder.URI)

err := svc.stateStore.WalkerPaths.EnqueueDir(modPath)
if err != nil {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring workspace folder %s: %s."+
" This is most likely bug, please report it.", folderPath.URI, err),
" This is most likely bug, please report it.", folder.URI, err),
})
continue
}
Expand Down