diff --git a/.changeset/shaggy-melons-destroy.md b/.changeset/shaggy-melons-destroy.md new file mode 100644 index 0000000000..d202cab6cd --- /dev/null +++ b/.changeset/shaggy-melons-destroy.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Skip adding entries to the `entities_search` table if their `key` exceeds a length limit. diff --git a/plugins/catalog-backend/src/database/search.test.ts b/plugins/catalog-backend/src/database/search.test.ts index c6793cbdf4..656da8b6f5 100644 --- a/plugins/catalog-backend/src/database/search.test.ts +++ b/plugins/catalog-backend/src/database/search.test.ts @@ -101,6 +101,12 @@ describe('search', () => { expect(output).toEqual([{ entity_id: 'eid', key: 'foo', value: 'bar' }]); }); + it('skips very large keys', () => { + const input = [{ key: 'a'.repeat(10000), value: 'foo' }]; + const output = mapToRows(input, 'eid'); + expect(output).toEqual([]); + }); + it('skips very large values', () => { const input = [{ key: 'foo', value: 'a'.repeat(10000) }]; const output = mapToRows(input, 'eid'); diff --git a/plugins/catalog-backend/src/database/search.ts b/plugins/catalog-backend/src/database/search.ts index 9e58d2468b..67b6d14b4f 100644 --- a/plugins/catalog-backend/src/database/search.ts +++ b/plugins/catalog-backend/src/database/search.ts @@ -31,6 +31,7 @@ const SPECIAL_KEYS = [ // The maximum length allowed for search values. These columns are indexed, and // database engines do not like to index on massive values. For example, // postgres will balk after 8191 byte line sizes. +const MAX_KEY_LENGTH = 200; const MAX_VALUE_LENGTH = 200; type Kv = { @@ -136,7 +137,7 @@ export function mapToRows( result.push({ entity_id: entityId, key, value: null }); } else { const value = String(rawValue).toLowerCase(); - if (value.length <= MAX_VALUE_LENGTH) { + if (key.length <= MAX_KEY_LENGTH && value.length <= MAX_VALUE_LENGTH) { result.push({ entity_id: entityId, key, value }); } } diff --git a/plugins/catalog-backend/src/next/search.test.ts b/plugins/catalog-backend/src/next/search.test.ts index 2e0b07b4a9..be896319b7 100644 --- a/plugins/catalog-backend/src/next/search.test.ts +++ b/plugins/catalog-backend/src/next/search.test.ts @@ -104,6 +104,12 @@ describe('search', () => { expect(output).toEqual([{ entity_id: 'eid', key: 'foo', value: 'bar' }]); }); + it('skips very large keys', () => { + const input = [{ key: 'a'.repeat(10000), value: 'foo' }]; + const output = mapToRows(input, 'eid'); + expect(output).toEqual([]); + }); + it('skips very large values', () => { const input = [{ key: 'foo', value: 'a'.repeat(10000) }]; const output = mapToRows(input, 'eid'); diff --git a/plugins/catalog-backend/src/next/search.ts b/plugins/catalog-backend/src/next/search.ts index 383b29fd70..0681bffa17 100644 --- a/plugins/catalog-backend/src/next/search.ts +++ b/plugins/catalog-backend/src/next/search.ts @@ -43,6 +43,7 @@ const SPECIAL_KEYS = [ // The maximum length allowed for search values. These columns are indexed, and // database engines do not like to index on massive values. For example, // postgres will balk after 8191 byte line sizes. +const MAX_KEY_LENGTH = 200; const MAX_VALUE_LENGTH = 200; type Kv = { @@ -145,7 +146,7 @@ export function mapToRows(input: Kv[], entityId: string): DbSearchRow[] { result.push({ entity_id: entityId, key, value: null }); } else { const value = String(rawValue).toLocaleLowerCase('en-US'); - if (value.length <= MAX_VALUE_LENGTH) { + if (key.length <= MAX_KEY_LENGTH && value.length <= MAX_VALUE_LENGTH) { result.push({ entity_id: entityId, key, value }); } }