Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -->
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();
});
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
--------

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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @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
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing the tests for the argument.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You require the parameter to be a string. Test that the compiler throws an error if provided anything other than a string.

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
isSafeToken(); // $ExpectError
isSafeToken( 'foo', 123 ); // $ExpectError
}
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
Loading
Loading