Skip to content
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

Regex error when using WPAPI.discover #504

Open
githubuser441 opened this issue Apr 13, 2022 · 3 comments
Open

Regex error when using WPAPI.discover #504

githubuser441 opened this issue Apr 13, 2022 · 3 comments

Comments

@githubuser441
Copy link

SyntaxError: Invalid regular expression: /^[a-zA-Z0-9-_]+)$/: Unmatched ')' at new RegExp (<anonymous>) at reduceRouteComponents (C:\project\node_modules\wpapi\lib\route-tree.js:83:3) at Array.reduce (<anonymous>) at reduceRouteTree (C:\project\node_modules\wpapi\lib\route-tree.js:182:18) at C:\project\node_modules\wpapi\lib\util\object-reduce.js:25:20 at Array.reduce (<anonymous>) at module.exports (C:\project\node_modules\wpapi\lib\util\object-reduce.js:24:3) at buildRouteTree (C:\project\node_modules\wpapi\lib\route-tree.js:200:9) at WPAPI.bootstrap (C:\project\node_modules\wpapi\wpapi.js:349:23) at new WPAPI (C:\project\node_modules\wpapi\wpapi.js:88:4) Endpoint detected, proceeding despite error...

I'm using the default route, so I don't understand why it's giving me a error.

@joostdekeijzer
Copy link

joostdekeijzer commented Jun 13, 2022

After updating from WP5.9.3 to WP6.0 I experience a similar problem.

SyntaxError: unterminated character class

Error is in reduceRouteComponents (route-gree.js:83)

	// A level's validate method is called to check whether a value being set
	// on the request URL is of the proper type for the location in which it
	// is specified. If a group pattern was found, the validator checks whether
	// the input string exactly matches the group pattern.
	const groupPatternRE = groupPattern === '' ?
		// If groupPattern is an empty string, accept any input without validation
		/.*/ :
		// Otherwise, validate against the group pattern or the component string
		new RegExp( groupPattern ? '^' + groupPattern + '$' : component, 'i' ); # <= line 83

Related to #503 ?

@joostdekeijzer
Copy link

In my case, the code seems to break on the WP WP_REST_Global_Styles_Controller regex:

PHP code:

'/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations'

In above reduceRouteCompontens javascript the groupPattern is [\\/\\s%\\w\\.\\(\\
and the component is (?P<stylesheet>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+).

I guess the groupPattern is unterminated...

@braniubojni
Copy link

braniubojni commented Jun 29, 2022

Hi everyone, has same issue, using wpapi-node, solve it by patch, and try-catch for now, will wait for new updates,

	let groupPatternRE;
	try {
		groupPatternRE = groupPattern === '' ?
		// If groupPattern is an empty string, accept any input without validation
		/.*/ :
		// Otherwise, validate against the group pattern or the component string
		new RegExp( groupPattern ? '^' + groupPattern + '$' : component, 'i' );
	} catch (e) {
		if (groupPattern !== null && groupPattern.includes('+)?')) {
			groupPatternRE = groupPattern === '' ?
			// If groupPattern is an empty string, accept any input without validation
			/.*/ :
			// Otherwise, validate against the group pattern or the component string
			new RegExp( groupPattern ? '^' + groupPattern.replace('+)?', '+))?') + '$' : component, 'i' );
		}
	}

Not the best solution of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants