From 0ba51891272b84ebbc40b106ee2f123fd06c4e66 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 15 Oct 2021 19:21:01 -0400 Subject: [PATCH] log an invalid type for SO (#115175) (#115275) Co-authored-by: Mikhail Shustov --- .../serialization/serializer.test.ts | 46 +++++++++++++++++++ .../saved_objects/serialization/serializer.ts | 6 ++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/core/server/saved_objects/serialization/serializer.test.ts b/src/core/server/saved_objects/serialization/serializer.test.ts index 3fdeb4aa088e1..24eded55615c4 100644 --- a/src/core/server/saved_objects/serialization/serializer.test.ts +++ b/src/core/server/saved_objects/serialization/serializer.test.ts @@ -491,6 +491,52 @@ describe('#rawToSavedObject', () => { expect(actual).toHaveProperty('namespaces', ['baz']); }); }); + + describe('throws if provided invalid type', () => { + expect(() => + singleNamespaceSerializer.rawToSavedObject({ + _id: 'foo:bar', + _source: { + // @ts-expect-error expects a string + // eslint-disable-next-line + type: new String('foo'), + }, + }) + ).toThrowErrorMatchingInlineSnapshot( + `"Expected saved object type to be a string but given [String] with [foo] value."` + ); + + expect(() => + singleNamespaceSerializer.rawToSavedObject({ + _id: 'foo:bar', + _source: { + // @ts-expect-error expects astring + type: { + toString() { + return 'foo'; + }, + }, + }, + }) + ).toThrowErrorMatchingInlineSnapshot( + `"Expected saved object type to be a string but given [Object] with [foo] value."` + ); + }); + + describe('throws if provided invalid id', () => { + expect(() => + singleNamespaceSerializer.rawToSavedObject({ + // @ts-expect-error expects a string + // eslint-disable-next-line + _id: new String('foo:bar'), + _source: { + type: 'foo', + }, + }) + ).toThrowErrorMatchingInlineSnapshot( + `"Expected document id to be a string but given [String] with [foo:bar] value."` + ); + }); }); describe('#savedObjectToRaw', () => { diff --git a/src/core/server/saved_objects/serialization/serializer.ts b/src/core/server/saved_objects/serialization/serializer.ts index 5e27b3de24409..9d9d65e735866 100644 --- a/src/core/server/saved_objects/serialization/serializer.ts +++ b/src/core/server/saved_objects/serialization/serializer.ts @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import typeDetect from 'type-detect'; import { LEGACY_URL_ALIAS_TYPE } from '../object_types'; import { decodeVersion, encodeVersion } from '../version'; import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry'; @@ -236,6 +236,8 @@ function checkIdMatchesPrefix(id: string, prefix: string) { function assertNonEmptyString(value: string, name: string) { if (!value || typeof value !== 'string') { - throw new TypeError(`Expected "${value}" to be a ${name}`); + throw new TypeError( + `Expected ${name} to be a string but given [${typeDetect(value)}] with [${value}] value.` + ); } }