Skip to content

Commit

Permalink
fix: avoid crash on invalid URIs (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jun 27, 2022
1 parent 8b55f1d commit 4c1ddc0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
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

0 comments on commit 4c1ddc0

Please sign in to comment.