aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/language.lua
diff options
context:
space:
mode:
authorChristian Clason <c.clason@uni-graz.at>2023-01-26 09:42:23 +0100
committerChristian Clason <c.clason@uni-graz.at>2023-01-28 11:28:52 +0100
commitc032e83b22994332dd8769ef34cb817906a63cac (patch)
tree5c2d8c7131e07c1a6597c496d018e60e706e4cda /runtime/lua/vim/treesitter/language.lua
parentb4c4c232ba6fe3df5c6f12faff4405a16e4d40df (diff)
downloadrneovim-c032e83b22994332dd8769ef34cb817906a63cac.tar.gz
rneovim-c032e83b22994332dd8769ef34cb817906a63cac.tar.bz2
rneovim-c032e83b22994332dd8769ef34cb817906a63cac.zip
fix(treesitter): validate language name
Problem: Some injections (like markdown) allow specifying arbitrary language names for code blocks, which may be lead to errors when looking for a corresponding parser in runtime path. Solution: Validate that the language name only contains alphanumeric characters and `_` (e.g., for `c_sharp`) and error otherwise.
Diffstat (limited to 'runtime/lua/vim/treesitter/language.lua')
-rw-r--r--runtime/lua/vim/treesitter/language.lua12
1 files changed, 9 insertions, 3 deletions
diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua
index c92d63b8c4..8634e53b7b 100644
--- a/runtime/lua/vim/treesitter/language.lua
+++ b/runtime/lua/vim/treesitter/language.lua
@@ -6,7 +6,7 @@ local M = {}
---
--- Parsers are searched in the `parser` runtime directory, or the provided {path}
---
----@param lang string Language the parser should parse
+---@param lang string Language the parser should parse (alphanumerical and `_` only)
---@param path (string|nil) Optional path the parser is located at
---@param silent (boolean|nil) Don't throw an error if language not found
---@param symbol_name (string|nil) Internal symbol name for the language to load
@@ -16,13 +16,19 @@ function M.require_language(lang, path, silent, symbol_name)
return true
end
if path == nil then
- local fname = 'parser/' .. vim.fn.fnameescape(lang) .. '.*'
+ if not (lang and lang:match('[%w_]+') == lang) then
+ if silent then
+ return false
+ end
+ error("'" .. lang .. "' is not a valid language name")
+ end
+
+ local fname = 'parser/' .. lang .. '.*'
local paths = a.nvim_get_runtime_file(fname, false)
if #paths == 0 then
if silent then
return false
end
-
error("no parser for '" .. lang .. "' language, see :help treesitter-parsers")
end
path = paths[1]