From 69d881c96443b6cb9aa8d8f65cf615f4b1015b98 Mon Sep 17 00:00:00 2001 From: manunio Date: Sat, 14 Oct 2023 14:01:38 +0530 Subject: [PATCH] fix: Do not throw error on malformed URI escape in tag (#498) --- src/doc/directives.ts | 9 ++++++++- tests/doc/types.ts | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/doc/directives.ts b/src/doc/directives.ts index 4978c07f..add18fcd 100644 --- a/src/doc/directives.ts +++ b/src/doc/directives.ts @@ -146,7 +146,14 @@ 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(String(error)) + return null + } + } if (handle === '!') return source // local tag onError(`Could not resolve tag: ${source}`) diff --git a/tests/doc/types.ts b/tests/doc/types.ts index c03668be..28f2180b 100644 --- a/tests/doc/types.ts +++ b/tests/doc/types.ts @@ -106,6 +106,12 @@ describe('tags', () => { expect(doc.errors[0].code).toBe('MISSING_CHAR') }) } + + test('malformed URI (eemeli/yaml#498)', () => { + const doc = parseDocument('!!%ee 0') + expect(doc.errors).toHaveLength(1) + expect(doc.errors[0].message).toMatch('URIError') + }) }) test('eemeli/yaml#97', () => {