gopls/internal/template: fix panic in completion when cursor is between opening braces#636
gopls/internal/template: fix panic in completion when cursor is between opening braces#636YLChen-007 wants to merge 1 commit intogolang:masterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
…en opening braces
Add a bounds check in completer.complete() to prevent a slice bounds
out of range panic when the cursor is positioned between the two '{'
characters of a template action delimiter '{{'.
When an LSP client requests code completion at such a position,
c.offset (computed as tk.Start + len("{{")) can exceed the cursor
position (start), causing c.p.buf[c.offset:start] to panic with
'slice bounds out of range [8:7]'. This crashes the entire gopls
process, resulting in a Denial of Service for the IDE user.
The fix returns an empty completion list when c.offset > start,
which is the correct behavior since there is no meaningful text
to complete at a position inside the delimiter characters.
Includes a regression test that directly constructs the problematic
state and verifies complete() returns gracefully without panicking.
8948a89 to
bc699b3
Compare
|
This PR (HEAD: bc699b3) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/765760. Important tips:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/765760. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/765760. |
Summary
Fix a Denial of Service (DoS) vulnerability in
goplswhere the language server crashes withpanic: slice bounds out of range [8:7]when code completion is requested at a cursor position between the two{characters of a template action delimiter{{.Root Cause
In
completer.complete(), the expressionc.p.buf[c.offset:start]panics whenc.offset > start. This occurs when:.tmplfile contains a template action likehello {{ }}{and second{(e.g., at byte offset 7)c.offsetis computed astk.Start + len("{{")= 8start(cursor position) = 7buf[8:7]triggerspanic: runtime error: slice bounds out of range [8:7]Since there is no recovery mechanism, this crashes the entire
goplsprocess, degrading the user's IDE workspace.Fix
Added a bounds check before the slice operation in
completer.complete():This returns an empty completion list when the cursor is inside the delimiter characters, which is the correct behavior — there is no meaningful text to complete at that position.
Testing
TestCompleteBoundsCheckregression test that directly constructs the problematic state (offset=8, cursor=7) and verifiescomplete()returns gracefully without panickingTestParsedsubtests +TestSymbols+TestWordAt+TestQuotes)Reproduction
goplsand configuretemplateExtensions: ["tmpl"]hi.tmplwith content:hello {{ }}{characters (byte offset 7)goplscrashesImpact
Denial of Service. An attacker can craft a malicious Go project containing a
.tmplfile that crashesgoplswhen the victim opens it and the cursor enters the trigger position.