From 3d0a077ca08da8c0cd68139cecd7366e573bbbbe Mon Sep 17 00:00:00 2001 From: Xiaoyan Rao <270668624@qq.com> Date: Mon, 13 Jan 2025 16:01:18 +0800 Subject: [PATCH] fix(file-log): throw an error `No such file or directory` when the `path` has a whitespace at the beginning or end. --- .../fix_file_path_not_allowed_whitespace.yml | 3 ++ kong/plugins/file-log/schema.lua | 2 +- .../03-plugins/04-file-log/02-schema_spec.lua | 41 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/kong/fix_file_path_not_allowed_whitespace.yml diff --git a/changelog/unreleased/kong/fix_file_path_not_allowed_whitespace.yml b/changelog/unreleased/kong/fix_file_path_not_allowed_whitespace.yml new file mode 100644 index 00000000000..89740a42060 --- /dev/null +++ b/changelog/unreleased/kong/fix_file_path_not_allowed_whitespace.yml @@ -0,0 +1,3 @@ +message: "**file-log**: Fixed an issue where the path does not allow whitespace at the beginning or end." +type: bugfix +scope: Plugin \ No newline at end of file diff --git a/kong/plugins/file-log/schema.lua b/kong/plugins/file-log/schema.lua index 67a8590d021..e6b7213f696 100644 --- a/kong/plugins/file-log/schema.lua +++ b/kong/plugins/file-log/schema.lua @@ -10,7 +10,7 @@ return { fields = { { path = { description = "The file path of the output log file. The plugin creates the log file if it doesn't exist yet.", type = "string", required = true, - match = [[^[^*&%%\`]+$]], + match = [[^[^%s*&%%\\`]+$]], err = "not a valid filename", }, }, { reopen = { description = "Determines whether the log file is closed and reopened on every request.", type = "boolean", required = true, default = false }, }, diff --git a/spec/03-plugins/04-file-log/02-schema_spec.lua b/spec/03-plugins/04-file-log/02-schema_spec.lua index 17a815dad34..7c2de262dc2 100644 --- a/spec/03-plugins/04-file-log/02-schema_spec.lua +++ b/spec/03-plugins/04-file-log/02-schema_spec.lua @@ -30,6 +30,47 @@ describe("Plugin: file-log (schema)", function() } }, ---------------------------------------- + { + name = "beginning spaces are not allowed", + input = { + path = " /ovo", + reopen = true + }, + output = nil, + error = { + config = { + path = "not a valid filename" + } + } + }, + { + name = "beginning spaces are not allowed", + input = { + path = "/ov\\o", + reopen = true + }, + output = nil, + error = { + config = { + path = "not a valid filename" + } + } + }, + ---------------------------------------- + { + name = "trailing spaces are not allowed", + input = { + path = "/ovo ", + reopen = true + }, + output = nil, + error = { + config = { + path = "not a valid filename" + } + } + }, + -------------------------------------- { name = "accepts valid filename", input = {