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

1.6.0: querystring.stringify does not work with number literals #1208

Closed
dpatti opened this issue Mar 19, 2015 · 13 comments
Closed

1.6.0: querystring.stringify does not work with number literals #1208

dpatti opened this issue Mar 19, 2015 · 13 comments
Labels
confirmed-bug Issues with confirmed bugs. querystring Issues and PRs related to the built-in querystring module.

Comments

@dpatti
Copy link

dpatti commented Mar 19, 2015

1.5.1:

> querystring.stringify({ foo: 1 })
'foo=1'

1.6.0:

> querystring.stringify({ foo: 1 })
'foo='
@thedufer
Copy link

85a92a3 appears to be at fault - QueryString.escape no longer works properly on numbers, but the calling code expects it to.

@mikeal
Copy link
Contributor

mikeal commented Mar 19, 2015

this is bad.

@mikeal
Copy link
Contributor

mikeal commented Mar 19, 2015

@mscdex @trevnorris pinging you since this is because of 85a92a3

@cjihrig
Copy link
Contributor

cjihrig commented Mar 19, 2015

This seems like something that there should have been an existing test for.

@kenany
Copy link
Contributor

kenany commented Mar 19, 2015

Would this patch suffice?

diff --git a/lib/querystring.js b/lib/querystring.js
index af320cf..601ed16 100644
--- a/lib/querystring.js
+++ b/lib/querystring.js
@@ -145,7 +145,7 @@ QueryString.escape = function(str) {

 var stringifyPrimitive = function(v) {
   if (typeof v === 'string' || (typeof v === 'number' && isFinite(v)))
-    return v;
+    return String(v);
   if (typeof v === 'boolean')
     return v ? 'true' : 'false';
   return '';
diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js
index e2591d7..6ebb85e 100644
--- a/test/parallel/test-querystring.js
+++ b/test/parallel/test-querystring.js
@@ -14,6 +14,7 @@ var qsTestCases = [
   ['foo=bar', 'foo=bar', {'foo': 'bar'}],
   ['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
   ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
+  ['foo=1&bar=2', 'foo=1&bar=2', {'foo': 1, 'bar': 2}],
   ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
    'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
    {'my weird field': 'q1!2"\'w$5&7/z8)?' }],

@thedufer
Copy link

If we're so worried about optimizing this, it seems wrong to run strings through String.

@kenany
Copy link
Contributor

kenany commented Mar 19, 2015

@thedufer Alright,

diff --git a/lib/querystring.js b/lib/querystring.js
index af320cf..0a5e2d1 100644
--- a/lib/querystring.js
+++ b/lib/querystring.js
@@ -144,8 +144,10 @@ QueryString.escape = function(str) {
 };

 var stringifyPrimitive = function(v) {
-  if (typeof v === 'string' || (typeof v === 'number' && isFinite(v)))
+  if (typeof v === 'string')
     return v;
+  if (typeof v === 'number' && isFinite(v))
+    return String(v);
   if (typeof v === 'boolean')
     return v ? 'true' : 'false';
   return '';

@mscdex
Copy link
Contributor

mscdex commented Mar 19, 2015

Looks like we should use '' + v instead of the String constructor.

@thedufer
Copy link

Seems reasonable.

However, this is dependent on the expectations of QueryString.escape. The docs indicate that it is exported solely for the purpose of allowing it to be overridden. So do we care that someone out there is probably using it (maybe on numbers) and this will still break their code? And second, we now pass strings to escape, even if a number was passed in to stringify - it's possible for someone to have overridden escape it in such a way that it operates on numbers differently from strings. Again, do we care that we've potentially broken their code?

@Fishrock123
Copy link
Contributor

Looks like all the current querystring test cases are strings: https://github.com/iojs/io.js/blob/v1.x/test/parallel/test-querystring.js

@jbergstroem
Copy link
Member

@Fishrock123 There doesn't seem to be a case where it passes a number. Try suggested test by @kenany above.

@brendanashworth brendanashworth added confirmed-bug Issues with confirmed bugs. querystring Issues and PRs related to the built-in querystring module. labels Mar 19, 2015
@rvagg
Copy link
Member

rvagg commented Mar 19, 2015

Can someone get a PR in for this and we'll push a 1.6.1 today, @mscdex are you in a position to deal with this one or should someone else step up?

Fishrock123 added a commit to Fishrock123/node that referenced this issue Mar 20, 2015
Fixes a number parsing regression introduced in 85a92a3

Fixes: nodejs#1208
PR-URL: nodejs#1213
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Brian White <mscdex@mscdex.net>
Fishrock123 added a commit to Fishrock123/node that referenced this issue Mar 20, 2015
stringifyPrimitive has always failed to stringify numbers since its
introduction in 422d3c9. This went uncaught due to encodeURIComponent's
string coercion.

Fixes: nodejs#1208
PR-URL: nodejs#1213
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Brian White <mscdex@mscdex.net>
@Fishrock123
Copy link
Contributor

Fixed in a89f5c2 and c9aec2b

rvagg added a commit that referenced this issue Mar 20, 2015
Notable Changes:

* path: New type-checking on path.resolve()
  <#1153> uncovered some edge-cases
  being relied upon in the wild, most notably path.dirname(undefined).
  Type-checking has been loosened for path.dirname(), path.basename(),
  and path.extname(), (Colin Ihrig)
  <#1216>.
* querystring: Internal optimizations in querystring.parse() and
  querystring.stringify() <#847>
  prevented Number literals from being properly converted via
  querystring.escape() <#1208>,
  exposing a blind-spot in the test suite. The bug and the tests have
  now been fixed (Jeremiah Senkpiel)
  <#1213>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. querystring Issues and PRs related to the built-in querystring module.
Projects
None yet
Development

No branches or pull requests

10 participants