diff --git a/kong/db/schema/entities/targets.lua b/kong/db/schema/entities/targets.lua index 54b6041416a3..89346233e0a4 100644 --- a/kong/db/schema/entities/targets.lua +++ b/kong/db/schema/entities/targets.lua @@ -5,7 +5,12 @@ local utils = require "kong.tools.utils" local function validate_target(target) local p = utils.normalize_ip(target) if not p then - return nil, "Invalid target; not a valid hostname or ip address" + local ok = utils.validate_utf8(target) + if not ok then + return nil, "Invalid target; not a valid hostname or ip address" + end + + return nil, "Invalid target ('" .. target .. "'); not a valid hostname or ip address" end return true end diff --git a/kong/db/schema/entities/upstreams.lua b/kong/db/schema/entities/upstreams.lua index 6c5c612b2558..f7fda2cebd87 100644 --- a/kong/db/schema/entities/upstreams.lua +++ b/kong/db/schema/entities/upstreams.lua @@ -4,16 +4,26 @@ local utils = require "kong.tools.utils" local null = ngx.null +local function get_name_for_error(name) + local ok = utils.validate_utf8(name) + if not ok then + return "Invalid name" + end + + return "Invalid name ('" .. name .. "')" +end + + local validate_name = function(name) local p = utils.normalize_ip(name) if not p then - return nil, "Invalid name; must be a valid hostname" + return nil, get_name_for_error(name) .. "; must be a valid hostname" end if p.type ~= "name" then - return nil, "Invalid name; no ip addresses allowed" + return nil, get_name_for_error(name) .. "; no ip addresses allowed" end if p.port then - return nil, "Invalid name; no port allowed" + return nil, get_name_for_error(name) .. "; no port allowed" end return true end diff --git a/spec/01-unit/01-db/01-schema/08-targets_spec.lua b/spec/01-unit/01-db/01-schema/08-targets_spec.lua index 36eec2dc23df..915ab26e335f 100644 --- a/spec/01-unit/01-db/01-schema/08-targets_spec.lua +++ b/spec/01-unit/01-db/01-schema/08-targets_spec.lua @@ -26,7 +26,7 @@ describe("targets", function() local ok, err = validate({ target = "\\\\bad\\\\////name////", upstream = upstream }) assert.falsy(ok) - assert.same({ target = "Invalid target; not a valid hostname or ip address"}, err) + assert.same({ target = "Invalid target ('\\\\bad\\\\////name////'); not a valid hostname or ip address"}, err) end) end) end) diff --git a/spec/01-unit/01-db/01-schema/09-upstreams_spec.lua b/spec/01-unit/01-db/01-schema/09-upstreams_spec.lua index e5c4092221d0..b745c74aff6d 100644 --- a/spec/01-unit/01-db/01-schema/09-upstreams_spec.lua +++ b/spec/01-unit/01-db/01-schema/09-upstreams_spec.lua @@ -199,15 +199,15 @@ describe("load upstreams", function() ok, err = Upstreams:validate({ name = "123.123.123.123" }) assert.falsy(ok) - assert.same({ name = "Invalid name; no ip addresses allowed" }, err) + assert.same({ name = "Invalid name ('123.123.123.123'); no ip addresses allowed" }, err) ok, err = Upstreams:validate({ name = "\\\\bad\\\\////name////" }) assert.falsy(ok) - assert.same({ name = "Invalid name; must be a valid hostname" }, err) + assert.same({ name = "Invalid name ('\\\\bad\\\\////name////'); must be a valid hostname" }, err) ok, err = Upstreams:validate({ name = "name:80" }) assert.falsy(ok) - assert.same({ name = "Invalid name; no port allowed" }, err) + assert.same({ name = "Invalid name ('name:80'); no port allowed" }, err) end) -- acceptance diff --git a/spec/02-integration/04-admin_api/07-upstreams_routes_spec.lua b/spec/02-integration/04-admin_api/07-upstreams_routes_spec.lua index c2481e02f4e3..0ed80c344a1e 100644 --- a/spec/02-integration/04-admin_api/07-upstreams_routes_spec.lua +++ b/spec/02-integration/04-admin_api/07-upstreams_routes_spec.lua @@ -212,7 +212,7 @@ describe("Admin API: #" .. strategy, function() body = assert.res_status(400, res) local json = cjson.decode(body) assert.equals("schema violation", json.name) - assert.same({ name = "Invalid name; must be a valid hostname" }, json.fields) + assert.same({ name = "Invalid name ('some invalid host name'); must be a valid hostname" }, json.fields) -- Invalid slots parameter res = assert(client:send { @@ -566,7 +566,7 @@ describe("Admin API: #" .. strategy, function() body = assert.response(res).has.status(400) local json = cjson.decode(body) - assert.same("Invalid name; no ip addresses allowed", json.message) + assert.same("Invalid name ('1.2.3.4'); no ip addresses allowed", json.message) end end) end)