From 5206398bed063b1923ad0cc9d00952c7555e915a Mon Sep 17 00:00:00 2001 From: manunio Date: Tue, 10 Oct 2023 22:04:27 +0530 Subject: [PATCH] fix: Malformed uri --- src/doc/directives.ts | 8 +++++++- tests/doc/parse.ts | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/doc/directives.ts b/src/doc/directives.ts index 4978c07f..06fc5925 100644 --- a/src/doc/directives.ts +++ b/src/doc/directives.ts @@ -146,7 +146,13 @@ export class Directives { const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/) as string[] if (!suffix) onError(`The ${source} tag has no suffix`) const prefix = this.tags[handle] - if (prefix) return prefix + decodeURIComponent(suffix) + if (prefix) { + try { + return prefix + decodeURIComponent(suffix) + } catch (error) { + onError('Failed to decode suffix') + } + } if (handle === '!') return source // local tag onError(`Could not resolve tag: ${source}`) diff --git a/tests/doc/parse.ts b/tests/doc/parse.ts index a40ac414..31dc0b6a 100644 --- a/tests/doc/parse.ts +++ b/tests/doc/parse.ts @@ -819,3 +819,10 @@ describe('CRLF line endings', () => { expect(res).toBe('foo bar') }) }) + +describe('Failed to decode suffix', () => { + test('rise in parse for malformed uri', () => { + const data = `[!!eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeJJJJJJJeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeebooleaeeeeeeeeeee-eeeeeeeee%eeeeeeeeeeeee[` + expect(() => YAML.parse(data)).toThrow(/Failed to decode suffix/) + }) +})