Skip to content

Commit

Permalink
fix(parsing): allow invalid decodeUriComponent parsing to occur #296
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed Mar 6, 2024
1 parent 4c7b681 commit f9e83c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/pointer.ts
Expand Up @@ -8,6 +8,14 @@ const tildes = /~/g;
const escapedSlash = /~1/g;
const escapedTilde = /~0/g;

const safeDecodeURIComponent = (encodedURIComponent: string): string => {
try {
return decodeURIComponent(encodedURIComponent);
} catch {
return encodedURIComponent;
}
};

/**
* This class represents a single JSON pointer and its resolved value.
*
Expand Down Expand Up @@ -181,7 +189,7 @@ class Pointer {

// Decode each part, according to RFC 6901
for (let i = 0; i < pointer.length; i++) {
pointer[i] = decodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
pointer[i] = safeDecodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
}

if (pointer[0] !== "") {
Expand Down
8 changes: 8 additions & 0 deletions test/specs/pointer/null.spec.ts
@@ -0,0 +1,8 @@
import { describe, it } from "vitest";
import Pointer from "../../../lib/pointer";

describe("Pointers", () => {
it("should parse successfully", async () => {
Pointer.parse("#/c%d");
});
});

0 comments on commit f9e83c0

Please sign in to comment.