Repository navigation
feat: add utils/json-pointer/base/assert/is-safe-token
#16320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 6 commits into
stdlib-js:develop
from
gururaj1512:json-pointer/base/assert/is-safe-token
Nov 10, 2026
+632
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
db1621f
feat: add utils/json-pointer/base/assert/is-safe-token
gururaj1512 3296f61
Merge branch 'develop' into json-pointer/base/assert/is-safe-token
kgryte b5f1dd5
chore: add missing folder
kgryte e12e782
refactor: use utility rather than hardcode conditional
kgryte 7a6a7b7
fix: invert test
kgryte 440be6f
test: add type tests
gururaj1512 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
lib/node_modules/@stdlib/utils/json-pointer/base/assert/is-safe-token/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # isSafeToken | ||
|
|
||
| > Test whether a JSON Pointer reference token is safe. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| This function tests whether a [JSON Pointer][json-pointer] reference token is safe to use for property traversal. Tokens matching `__proto__`, `constructor`, or `prototype` are considered unsafe as they are common prototype pollution vectors. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var isSafeToken = require( '@stdlib/utils/json-pointer/base/assert/is-safe-token' ); | ||
| ``` | ||
|
|
||
| #### isSafeToken( token ) | ||
|
|
||
| Tests whether a JSON Pointer reference token is safe. | ||
|
|
||
| ```javascript | ||
| var bool = isSafeToken( 'foo' ); | ||
| // returns true | ||
|
|
||
| bool = isSafeToken( '__proto__' ); | ||
| // returns false | ||
|
|
||
| bool = isSafeToken( 'constructor' ); | ||
| // returns false | ||
|
|
||
| bool = isSafeToken( 'prototype' ); | ||
| // returns false | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var isSafeToken = require( '@stdlib/utils/json-pointer/base/assert/is-safe-token' ); | ||
|
|
||
| var bool = isSafeToken( 'foo' ); | ||
| // returns true | ||
|
|
||
| bool = isSafeToken( '0' ); | ||
| // returns true | ||
|
|
||
| bool = isSafeToken( '-' ); | ||
| // returns true | ||
|
|
||
| bool = isSafeToken( '__proto__' ); | ||
| // returns false | ||
|
|
||
| bool = isSafeToken( 'constructor' ); | ||
| // returns false | ||
|
|
||
| bool = isSafeToken( 'prototype' ); | ||
| // returns false | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="references"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [json-pointer]: https://tools.ietf.org/html/rfc6901 | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
62 changes: 62 additions & 0 deletions
62
lib/node_modules/@stdlib/utils/json-pointer/base/assert/is-safe-token/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; | ||
| var pkg = require( './../package.json' ).name; | ||
| var isSafeToken = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var values; | ||
| var out; | ||
| var v; | ||
| var i; | ||
|
|
||
| values = [ | ||
| 'foo', | ||
| 'bar', | ||
| '__proto__', | ||
| 'constructor', | ||
| 'prototype', | ||
| '0', | ||
| '-', | ||
| 'baz' | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = values[ i % values.length ]; | ||
| out = isSafeToken( v ); | ||
| if ( typeof out !== 'boolean' ) { | ||
| b.fail( 'should return a boolean' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isBoolean( out ) ) { | ||
| b.fail( 'should return a boolean' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
lib/node_modules/@stdlib/utils/json-pointer/base/assert/is-safe-token/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
|
|
||
| {{alias}}( token ) | ||
| Tests whether a JSON Pointer reference token is safe. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| token: string | ||
| JSON Pointer reference token. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool: boolean | ||
| Boolean indicating whether a token is safe. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var bool = {{alias}}( 'foo' ) | ||
| true | ||
| > bool = {{alias}}( '0' ) | ||
| true | ||
| > bool = {{alias}}( '-' ) | ||
| true | ||
| > bool = {{alias}}( '__proto__' ) | ||
| false | ||
| > bool = {{alias}}( 'constructor' ) | ||
| false | ||
| > bool = {{alias}}( 'prototype' ) | ||
| false | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/utils/json-pointer/base/assert/is-safe-token/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /** | ||
| * Tests whether a JSON Pointer reference token is safe. | ||
| * | ||
| * @param token - reference token | ||
| * @returns boolean indicating whether a token is safe | ||
| * | ||
| * @example | ||
| * var bool = isSafeToken( 'foo' ); | ||
| * // returns true | ||
| * | ||
| * @example | ||
| * var bool = isSafeToken( '__proto__' ); | ||
| * // returns false | ||
| * | ||
| * @example | ||
| * var bool = isSafeToken( 'constructor' ); | ||
| * // returns false | ||
| * | ||
| * @example | ||
| * var bool = isSafeToken( 'prototype' ); | ||
| * // returns false | ||
| */ | ||
| declare function isSafeToken( token: string ): boolean; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = isSafeToken; |
46 changes: 46 additions & 0 deletions
46
lib/node_modules/@stdlib/utils/json-pointer/base/assert/is-safe-token/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import isSafeToken = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a boolean... | ||
| { | ||
| isSafeToken( 'foo' ); // $ExpectType boolean | ||
| isSafeToken( '__proto__' ); // $ExpectType boolean | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided a value other than a string... | ||
| { | ||
| isSafeToken( true ); // $ExpectError | ||
| isSafeToken( false ); // $ExpectError | ||
| isSafeToken( null ); // $ExpectError | ||
| isSafeToken( undefined ); // $ExpectError | ||
| isSafeToken( 5 ); // $ExpectError | ||
| isSafeToken( [] ); // $ExpectError | ||
| isSafeToken( {} ); // $ExpectError | ||
| isSafeToken( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided an unsupported number of arguments... | ||
| { | ||
| isSafeToken(); // $ExpectError | ||
| isSafeToken( 'foo', 123 ); // $ExpectError | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
lib/node_modules/@stdlib/utils/json-pointer/base/assert/is-safe-token/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var isSafeToken = require( './../lib' ); | ||
|
|
||
| var bool = isSafeToken( 'foo' ); | ||
| console.log( bool ); | ||
| // => true | ||
|
|
||
| bool = isSafeToken( '0' ); | ||
| console.log( bool ); | ||
| // => true | ||
|
|
||
| bool = isSafeToken( '-' ); | ||
| console.log( bool ); | ||
| // => true | ||
|
|
||
| bool = isSafeToken( '__proto__' ); | ||
| console.log( bool ); | ||
| // => false | ||
|
|
||
| bool = isSafeToken( 'constructor' ); | ||
| console.log( bool ); | ||
| // => false | ||
|
|
||
| bool = isSafeToken( 'prototype' ); | ||
| console.log( bool ); | ||
| // => false |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.