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

Validate URI query string encoding #183

Merged
merged 7 commits into from
Feb 3, 2025
Merged

Validate URI query string encoding #183

merged 7 commits into from
Feb 3, 2025

Conversation

smaye81
Copy link
Member

@smaye81 smaye81 commented Jan 31, 2025

This adds an additional check for validating that the query string of a URI is correctly encoded.

Copy link

github-actions bot commented Jan 31, 2025

The latest Buf updates on your PR. Results from workflow Buf / validate-protos (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedFeb 1, 2025, 2:28 AM

cel/library.go Outdated
return false
}
}
if _, err := url.ParseQuery(uri.RawQuery); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment here real quick why this is necessary for future contributors who might wonder why it's necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, done. Let me know if it needs more detail.

cel/library.go Outdated Show resolved Hide resolved
cel/library.go Outdated Show resolved Hide resolved
Copy link
Member

@jchadwick-buf jchadwick-buf left a comment

Choose a reason for hiding this comment

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

I think this is a worthwhile improvement in strictness overall, though I definitely am thinking more strongly by the day that we need to formally define the grammar allowed by each validation rule. Relying on Go's sensibilities is nice for protovalidate-go, but even not considering other languages it has some caveats, including this issue of parsing things just to validate them and throw away the parsed result. But, that's definitely out-of-scope for this PR.

}

// Parse the query string to validate it is formed and encoded properly
_, err = url.ParseQuery(uri.RawQuery)
Copy link
Member

Choose a reason for hiding this comment

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

I understand the intent here, but this does an awful lot of work just to check the validity of the encoding. It parses out the query components and constructs a map with them, which we don't need.

It's a bit less succinct, but we can get the exact same validation while doing a lot less work:

	// (Not sure if we want to disallow unescaped semicolons, but ParseQuery does.)
	if strings.Contains(uri.RawQuery, ";") {
		return false
	}
	_, err = url.QueryUnescape(uri.RawQuery)
	return err == nil

It'd be even better if we could just validate without parsing and decoding everything, but I argue we at least should avoid building the map if we can.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry @jchadwick-buf i merged before i saw your comments. @rodaine wdyt? I can open up another PR to incorporate John's suggestions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually in talking to @timostamm today, we may want to discuss this deeper once his validation is done for protovalidate-ts. We can then make sure all the implementations are behaving the same way since he's following the RFC. We could then add additional conformance checks and formally define the grammar for what we validate.

@smaye81 smaye81 merged commit 09938de into main Feb 3, 2025
8 checks passed
@smaye81 smaye81 deleted the sayers/uri_test_cases branch February 3, 2025 15:09
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

Successfully merging this pull request may close these issues.

3 participants