Skip to content

Commit 4c1ddc0

Browse files
authored
fix: avoid crash on invalid URIs (#969)
1 parent 8b55f1d commit 4c1ddc0

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

internal/langserver/handlers/did_change_workspace_folders.go

+17
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,31 @@ import (
77
"github.com/creachadair/jrpc2"
88
"github.com/hashicorp/terraform-ls/internal/document"
99
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
10+
"github.com/hashicorp/terraform-ls/internal/uri"
1011
)
1112

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

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

internal/langserver/handlers/did_open.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@ package handlers
22

33
import (
44
"context"
5+
"fmt"
56

7+
"github.com/creachadair/jrpc2"
68
"github.com/hashicorp/terraform-ls/internal/document"
79
"github.com/hashicorp/terraform-ls/internal/job"
8-
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
910
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
1011
"github.com/hashicorp/terraform-ls/internal/state"
1112
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
1213
"github.com/hashicorp/terraform-ls/internal/terraform/module"
1314
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
15+
"github.com/hashicorp/terraform-ls/internal/uri"
1416
)
1517

1618
func (svc *service) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpenTextDocumentParams) error {
17-
dh := ilsp.HandleFromDocumentURI(params.TextDocument.URI)
19+
docURI := string(params.TextDocument.URI)
20+
21+
// URIs are always checked during initialize request, but
22+
// we still allow single-file mode, therefore invalid URIs
23+
// can still land here, so we check for those.
24+
if !uri.IsURIValid(docURI) {
25+
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
26+
Type: lsp.Warning,
27+
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
28+
" This is most likely bug, please report it.", docURI),
29+
})
30+
return fmt.Errorf("invalid URI: %s", docURI)
31+
}
32+
33+
dh := document.HandleFromURI(docURI)
1834

1935
err := svc.stateStore.DocumentStore.OpenDocument(dh, params.TextDocument.LanguageID,
2036
int(params.TextDocument.Version), []byte(params.TextDocument.Text))

internal/langserver/handlers/initialize.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (svc *service) Initialize(ctx context.Context, params lsp.InitializeParams)
3838
svc.server = jrpc2.ServerFromContext(ctx)
3939

4040
setupTelemetry(expClientCaps, svc, ctx, properties)
41+
defer svc.telemetry.SendEvent(ctx, "initialize", properties)
4142

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

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

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

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

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

263266
if len(params.WorkspaceFolders) > 0 {
264-
for _, folderPath := range params.WorkspaceFolders {
265-
modPath := document.DirHandleFromURI(folderPath.URI)
267+
for _, folder := range params.WorkspaceFolders {
268+
if !uri.IsURIValid(folder.URI) {
269+
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
270+
Type: lsp.Warning,
271+
Message: fmt.Sprintf("Ignoring workspace folder (unsupport or invalid URI) %s."+
272+
" This is most likely bug, please report it.", folder.URI),
273+
})
274+
continue
275+
}
276+
277+
modPath := document.DirHandleFromURI(folder.URI)
266278

267279
err := svc.stateStore.WalkerPaths.EnqueueDir(modPath)
268280
if err != nil {
269281
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
270282
Type: lsp.Warning,
271283
Message: fmt.Sprintf("Ignoring workspace folder %s: %s."+
272-
" This is most likely bug, please report it.", folderPath.URI, err),
284+
" This is most likely bug, please report it.", folder.URI, err),
273285
})
274286
continue
275287
}

0 commit comments

Comments
 (0)