Skip to content

Conversation

scottinet
Copy link
Contributor

@scottinet scottinet commented Jul 23, 2019

Description

HTTP querystrings do not define how boolean values should be encoded, how they are handled depend on the API parsing the querystring.

Kuzzle's convention is that, for booleans, option values are irrelevant. Instead, a boolean option is truthy if it' set in the querystring, and falsey otherwise.

So this: url?foo=0 or url?foo=false makes Kuzzle interpret the foo option as a non-empty string, and thus as "true" when converted to a boolean.

This PR fixes how the SDK passes boolean options to the querystring, only setting one if it's truthy, and skipping it if it's not.

How to test it

Use Kuzzle's CLI createFirstAdmin command: even if you tell it to not reset anonymous rights, it resets it because of that bug. If you apply these changes, the CLI acts as expected.

Other changes

Add a missing unit test about how arrays are encoded in the querystring.

// option "foo=false" in it will make Kuzzle consider it as truthy.
if (value === true) {
queryString.push(key);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpicking: do we really need the comment block and the nested if?

else if (typeof value === 'boolean' && value) {
  // foo=false => foo is truthy
  queryString.push(key);
}

Copy link
Contributor Author

@scottinet scottinet Jul 24, 2019

Choose a reason for hiding this comment

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

We need the nested if because, otherwise, a falsey boolean value will be processed by the final else condition just below, and added to the querystring anyway

@Aschen Aschen changed the title [fix] properly handle boolean flags in HTTP querystrings Properly handle boolean flags in HTTP querystrings Jul 24, 2019
@scottinet scottinet merged commit 470a513 into 6-dev Jul 26, 2019
@scottinet scottinet deleted the fix-http-boolean-options branch July 26, 2019 08:30
@scottinet scottinet mentioned this pull request Jul 31, 2019
scottinet added a commit that referenced this pull request Jul 31, 2019
# [6.2.0](https://github.com/kuzzleio/sdk-javascript/releases/tag/6.2.0) (2019-07-31)


#### Bug fixes

- [ [#428](#428) ] Properly handle boolean flags in HTTP querystrings   ([scottinet](https://github.com/scottinet))
- [ [#427](#427) ] Solve promise+event+memory leaks when the network fails   ([scottinet](https://github.com/scottinet))
- [ [#424](#424) ] Prevent pending request leak when disconnect the SDK   ([Aschen](https://github.com/Aschen))
- [ [#422](#422) ] Fix bug when decoding JWT in browser   ([Aschen](https://github.com/Aschen))
- [ [#420](#420) ] Fix http protocol unresolved promise on connection error   ([Aschen](https://github.com/Aschen))

#### New features

- [ [#419](#419) ] Add bulk:write and bulk:mWrite   ([Aschen](https://github.com/Aschen))

#### Enhancements

- [ [#421](#421) ] Get api routes from server:publicApi   ([Aschen](https://github.com/Aschen))
- [ [#423](#423) ] Emit queryError event on malformed request   ([Aschen](https://github.com/Aschen))
- [ [#417](#417) ] Security controller documentation   ([benoitvidis](https://github.com/benoitvidis))
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants