element-hq / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://element-hq.github.io/synapse
GNU Affero General Public License v3.0
963 stars 109 forks source link

Tidy up integer parsing #17339

Closed dkasak closed 1 week ago

dkasak commented 1 week ago

During https://github.com/element-hq/synapse/pull/16920, we seem to have inadvertently disallowed negative integers in parse_integer by default. This was contrary to the parse_integer documentation.

Surprisingly, nothing really broke, which indicates that things mostly do expect positive integers. Running an ast-grep query confirms it:

❯ ast-grep run --pattern 'parse_integer($A, $PARAM, $$$)' --json=pretty | jq -r '.[].metaVariables.single.PARAM.text' | sort -u
"before_ts"
"from"
"from_token"
"from_ts"
"height"
"limit"
"size_gt"
"timeout"
"timeout_ms"
"ts"
"upto_token"
"width"

As can be seen, these boil down to timestamps, limits, timeouts, sizes, etc, none of which really make sense to be negative. So it seems we've inadvertently stumbled into

This PR:

One remaining questionable thing is that due to the previous change in default, some code which used to return M.BAD_JSON now returns M.INVALID_PARAM instead, due to the validation being done within parse_integer instead of directly in the caller. One such example is the aforementioned C2S /hierarchy endpoint. Is this a problem? The spec doesn't mention which error code should be returned in this case and there were obviously no checks for this. Is there a general principle on whether M.BAD_JSON or M.INVALID_PARAM should be returned on a disallowed (but well formed) value within a JSON body?

Pull Request Checklist

erikjohnston commented 1 week ago

One remaining questionable thing is that due to the previous change in default, some code which used to return M.BAD_JSON now returns M.INVALID_PARAM instead, due to the validation being done within parse_integer instead of directly in the caller. One such example is the aforementioned C2S /hierarchy endpoint. Is this a problem? The spec doesn't mention which error code should be returned in this case and there were obviously no checks for this. Is there a general principle on whether M.BAD_JSON or M.INVALID_PARAM should be returned on a disallowed (but well formed) value within a JSON body?

None of these are JSON are they? They all look like query parameters?

dkasak commented 1 week ago

Uhh, you're right. The previous error code was wrong then, which caused my thinko. Even better, then this fixes the error codes too.