From 867da5ba694c55920da41f260fb59425d9cd7e42 Mon Sep 17 00:00:00 2001 From: Vazha Omanashvili Date: Mon, 17 May 2021 17:06:14 +0300 Subject: [PATCH] fix: local refs should not contain any whitespace characters (#89) * fix: local refs should not contain any whitespace characters * chore: # edge case and tests * fix: make regex less strict --- src/__tests__/isLocalRef.spec.ts | 34 +++++++++++++++++++++++++++++--- src/isLocalRef.ts | 2 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/__tests__/isLocalRef.spec.ts b/src/__tests__/isLocalRef.spec.ts index f09a168..ed7187c 100644 --- a/src/__tests__/isLocalRef.spec.ts +++ b/src/__tests__/isLocalRef.spec.ts @@ -1,11 +1,39 @@ import { isLocalRef } from '../isLocalRef'; describe('isLocalRef', () => { - it.each(['#', '#/', '#/foo', '#/0/1', '#/~1'])('should treat %s as local reference', ref => { + it.each([ + '#', + '#/foo', + '#/foo/0', + '#/', + '#/a~1b', + '#/c%25d', + '#/e%5Ef', + '#/g%7Ch', + '#/i%5Cj', + '#/k%22l', + '#/%20', + '#/m~0n', + '#./d~8z', + ])('should treat %s as local reference', ref => { expect(isLocalRef(ref)).toEqual(true); }); - it.each(['', '../#', 'http://a#', '/foo/#', '../test.yaml#/'])('should not treat %s as local reference', ref => { - expect(isLocalRef(ref)).toEqual(false); + it.each([ + '', + '../#', + 'http://a#', + '/foo/#', + '../test.yaml#/', + '#/foo\n/bar', + '#/foo\t/bar', + '#/foo\u2006​/bar', + ' #/foo/bar', + '#/foo /bar', + '#/foo/bar ', + '# Hello, world!', + 'foo', + ])('should not treat %s as local reference', ref => { + expect(isLocalRef(ref as string)).toEqual(false); }); }); diff --git a/src/isLocalRef.ts b/src/isLocalRef.ts index d646166..ba73488 100644 --- a/src/isLocalRef.ts +++ b/src/isLocalRef.ts @@ -1 +1 @@ -export const isLocalRef = (pointer: string) => pointer.length > 0 && pointer[0] === '#'; +export const isLocalRef = (pointer: string) => pointer.length > 0 && (pointer === '#' || /^#\S*$/.test(pointer));