GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Node crashes when a big sparse array is given to console.log (might cause DoS) · Issue #4905 · nodejs/node

Node crashes when a big sparse array is given to console.log (might cause DoS) #4905

New issue
Copy link
New issue
Copy link
Closed
Closed
Node crashes when a big sparse array is given to console.log (might cause DoS)#4905
Copy link
Labels
confirmed-bugIssues and PRs for confirmed bugs.consoleIssues and PRs related to the console subsystem.utilIssues and PRs related to the built-in util module.

Description

@mik01aj
mik01aj
opened on Jan 27, 2016
Issue body actions
var a=[] 
a[1000000000]=1 
console.log(a) 

This ends up with a FATAL ERROR: process out of memory. Wouldn't expect this...

Note that many applications use console.log for logging their stuff, and this can lead to a DoS attack: for example, when an user-specified JSON {"1000000000":"a"} is merged with some pre-existing array and then printed on console. Having an upper bound on printed Array items in console.log seems like an easy fix for this.

I originally reported this to [email protected], but I got this response:

I don't think we consider this a security issue (it's known and documented) but it's arguably a quality-of-implementation issue. If you'd like to pursue this further, can you file an issue (...)?

So I'm opening an issue 😃

Btw, I can't see this documented anywhere in the console docs, but maybe I'm missing something?

Activity

Next
  1. vkurchatkin
    added
    utilIssues and PRs related to the built-in util module.
    on Jan 27, 2016
  2. ChALkeR commented on Jan 27, 2016

    @ChALkeR
    ChALkeR
    on Jan 27, 2016
    Member
    More actions

    @mik01aj Am I correct that when speaking about DoS, you have console.log(util._extend([],{"1000000000":"a"})) or console.log(Object.assign([],{"1000000000":"a"})) in mind, and this in fact requires someone to use Object.assign or util._extend on an Array?

    If so, Object.assign([],{"length":1000000000}) (or util._extend([],{"length":1000000000})) will chew all your memory even without console.log, so you can't blame Node.js here. Edit: I was mistaken here, see below.

  3. mik01aj commented on Jan 27, 2016

    @mik01aj
    mik01aj
    on Jan 27, 2016
    Author
    More actions

    Yes. I think it can be pretty common in PATCH endpoints, or in deserialization libraries, or Redux reducers. I didn't check for real-world examples, but I believe that console.log should never ever crash the process, regardless of what was passed in.

    Note that Object.assign([],{"length":1000000000}) or similar examples create just objects, and this doesn't make Node crash - it's console.log that does (note that Node uses console.log internally in REPL too).

  4. vkurchatkin commented on Jan 27, 2016

    @vkurchatkin
    vkurchatkin
    on Jan 27, 2016
    Contributor
    More actions

    If so, Object.assign([],{"length":1000000000}) (or util._extend([],{"length":1000000000})) will chew all your memory

    How so?

  5. ChALkeR commented on Jan 27, 2016

    @ChALkeR
    ChALkeR
    on Jan 27, 2016
    Member
    More actions

    @vkurchatkin My bad, it seems that I was incorrect, sorry.

    I was testing this in repl, and it consumed the memory not on that operation, but when trying to print the result.

    I will edit my comments above, thanks!

  6. ChALkeR commented on Jan 27, 2016

    @ChALkeR
    ChALkeR
    on Jan 27, 2016
    Member
    More actions

    As a possible fix, we could limit the maximum length of an array being printed in util.inspect and replace it with an [Object] (or [Array]), if it's too long — pretty much the same way like it is done when nesting level is too deep.

  7. mik01aj commented on Jan 27, 2016

    @mik01aj
    mik01aj
    on Jan 27, 2016
    Author
    More actions

    Yes, that's what I meant. 👍

    As for DoS risk, I don't have time to think about a good full blown code example, sorry. Maybe later... Or maybe this could convince you that it's certainly possible to make such an array accidentally: lodash/lodash#1685.

  8. vkurchatkin commented on Jan 27, 2016

    @vkurchatkin
    vkurchatkin
    on Jan 27, 2016
    Contributor
    More actions

    As a possible fix, we could limit the maximum length of an array being printed in util.inspect

    +1.

  9. mscdex
    added
    consoleIssues and PRs related to the console subsystem.
    and removed
    utilIssues and PRs related to the built-in util module.
    on Jan 27, 2016
  10. ChALkeR commented on Jan 27, 2016

    @ChALkeR
    ChALkeR
    on Jan 27, 2016
    Member
    More actions

    @mscdex This is related to util.format/util.inspect, not to console.

  11. bnoordhuis commented on Jan 27, 2016

    @bnoordhuis
    bnoordhuis
    on Jan 27, 2016
    Member
    More actions

    console.log() and friends call util.format() and util.inspect(), though.

  12. ChALkeR commented on Jan 27, 2016

    @ChALkeR
    ChALkeR
    on Jan 27, 2016
    Member
    More actions

    @bnoordhuis This is what I meant, and is exactly why this issue belongs to util, not to console =).

  13. mscdex
    added
    utilIssues and PRs related to the built-in util module.
    on Jan 27, 2016
  14. rvagg commented on Jan 28, 2016

    @rvagg
    rvagg
    on Jan 28, 2016
    Member
    More actions

    Seems like the same issue as I was pinged about on Twitter recently

    screen shot 2016-01-28 at 10 22 40 pm

    It'd be nice to not have unexpected and non-obvious behaviour like this if we can help it. Fair enough if you're actually using up memory but when you know you're not but it still dies, that's not so great.

    /cc @trevnorris

  15. 8 remaining items

  16. dcposch
    added a commit that references this issue on Feb 4, 2016

    util: compact inspect() for sparse arrays

    This issue will close once commit d257683 is merged into the 'master' branch.
    d257683
  17. zeusdeux commented on Feb 5, 2016

    @zeusdeux
    zeusdeux
    on Feb 5, 2016
    Contributor
    More actions

    @trevnorris If you mean how the streaming model would work for objects with circular dependencies, then it would work as is since the already seen value will be in ctx.seen like it is in the current implementation. The stream would end up pushing [Circular] formatted string to it's consumer.

  18. dcposch commented on Feb 5, 2016

    @dcposch
    dcposch
    on Feb 5, 2016
    Contributor
    More actions

    @zeusdeux the reason var a=[]; a[1000000000]=1; console.log(a) works in browser not because it streams out jiggabytes of text, but because it prints sparse arrays compactly.

    util.inspect isn't for machine-to-machine communication -- it's not JSON.stringify. instead it's a debugging aid, meant for human viewing. so i prefer the INSPECT_MAX_BYTES approach (ie, always return a reasonable-size string) to the streaming approach (ie, efficiently output unlimited amounts of text)

  19. tunniclm commented on Mar 21, 2016

    @tunniclm
    tunniclm
    on Mar 21, 2016
    More actions

    How about something that operates like this (hacked together this morning):

    Options to restrict the output to a maximum number of elements and to elide series of unset values, with the settings available at module and per-invocation scope. Setting either to 0 disables that limit.

    > u=require('util'); u.MAX_ARRAY_OUTPUT_ELEMENTS=15; 
       u.MAX_UNSET_ELEMENTS_BEFORE_ELIDE=9;
      a=[]; a[9]=1; for (var i=100; i<10000; ++i) { a[i]=1}
    1
    > a
    [ , , , , , , , , , 1, <90 unset>, 1, 1, 1, <9897 more> ]
    > u.inspect(a).split(',').length
    15
    > console.log(u.inspect(a, {arrayMaxUnsetElementsBeforeElide: 1, 
                                arrayMaxOutputElements: 20}))
    [ <9 unset>,
      1,
      <90 unset>,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      <9884 more> ]
    undefined
    > console.log(a)
    [ , , , , , , , , , 1, <90 unset>, 1, 1, 1, <9897 more> ]
    undefined
    
  20. mik01aj commented on Mar 22, 2016

    @mik01aj
    mik01aj
    on Mar 22, 2016
    Author
    More actions

    @tunniclm I like your suggestion, but making it the default would need a new semver-major version (as stated by @rvagg in #5070 (comment)). I would say, the main reason is that you change the output format even for small arrays (I mean small for machine, so 10000 is small, it all prints in a split second).

    I would suggest to change the output format only for arrays big enough to cause the crash, and make this the default, and push this fix to any relevant versions. I consider making the output format nicer and more human-readable a separate issue (:+1: for it, but not in a minor version)

  21. tunniclm commented on Mar 22, 2016

    @tunniclm
    tunniclm
    on Mar 22, 2016
    More actions

    @mik01aj I'm not sure why this would necessarily be semver-major, since it could even be completely disabled by default, if necessary. (I was thinking setting the module options to be disabled by default, and to change the REPL to use the per-invocation values arrayMaxUnsetElementsBeforeElide: 0 and arrayMaxOutputElements: A_SUFFICIENTLY_LARGE_NUMBER by default).

    I can see that it doesn't help with selecting the right value for a maximum number of array output elements though -- would a flat value be sufficient? would we want a number calculated at startup depending on the machine? would we want a value calculated at run time based on the amount of free memory?

  22. mik01aj commented on Mar 22, 2016

    @mik01aj
    mik01aj
    on Mar 22, 2016
    Author
    More actions

    @tunniclm I can see your point too, but if it's disabled by default, then it doesn't fix this issue.

    About the SUFFICIENTLY_LARGE_NUMBER - I did some tests. I ran this command...

    /usr/bin/time -l node -e 'var a=[]; a[NUMBER]=1; console.log(a)'
    

    ...with different numbers. Here are the results:

     NUMBER     time (real)       memory (maximum resident set size)
     10000      0.21 s            22.6 MB
     100000     0.84 s            32.4 MB
    *1000000    6.76 s            91.9 MB
     10000000   66.26 s           732.7 MB -- with command as above
     10000000   8.79 s            732.6 MB -- with output redirected to /dev/null
     100000000  32.24 s to crash  1.9943 GB
    

    Based on this data, I would say that the reasonable number would be 1000000 (marked with *). If anyone actually prints arrays of this size using console.log and then uses the output for something, it's really really wrong and, well, he should be using JSON.stringify for it anyway.

  23. tunniclm commented on Mar 23, 2016

    @tunniclm
    tunniclm
    on Mar 23, 2016
    More actions

    @mik01aj I was having two separate thoughts about this with regard to semver-major:

    1. If it is not completely disabled (in the module options) by default, can you select a value for maximum output elements that gets very close to changing the behaviour from "this crashes" to "this provides truncated output in some new format". Seems difficult to do this well -- ideally, we'd be able to predict whether the was enough space at the time we are constructing the result array, and truncate if not.
    2. If you disable completely (in the module options), but alter the REPL behaviour so that it truncates output, would that be different from changing the behaviour of inspect for everyone in terms of impact -- whilst I can see that people might rely on the format provided by inspect generally in their programs, are people relying on the output of the REPL's statement evaluation? Maybe.

    EDIT:
    I was thinking about (2) when I made the comment #4905 (comment) . Considering the original bug was with console.log(), I realise I was thinking a bit off track for this issue.

    An alternative approach to this might be to re-architect inspect to stream out the result instead of building it in memory in its entirety.

  24. mik01aj commented on Mar 23, 2016

    @mik01aj
    mik01aj
    on Mar 23, 2016
    Author
    More actions

    Yup, so I'm for your point (1) 😄 And I think we shouldn't strive for perfectionism here. If someone really parses the util.inspect output for lists longer than million elements, then they have a lot of other trouble anyway.

    My idea is:

    1. First, change the current default behaviour in a minor version: truncate the output for lists longer than 1M elements and retain the same output format for shorter lists. Additional config options in util don't hurt, they can be added too. I propose the defaults to be:

      u.MAX_ARRAY_OUTPUT_ELEMENTS=1000000;
      u.MAX_UNSET_ELEMENTS_BEFORE_ELIDE=1000000;
      
    2. Then, in the next major version, change the default parameter values so that the printed output is what you'd expect for human reading, e.g.

      u.MAX_ARRAY_OUTPUT_ELEMENTS=100;
      u.MAX_UNSET_ELEMENTS_BEFORE_ELIDE=9;
      

    @tunniclm, what do you think?

  25. bnoordhuis commented on Mar 23, 2016

    @bnoordhuis
    bnoordhuis
    on Mar 23, 2016
    Member
    More actions

    If I understand the proposal correctly, it won't be a complete fix. There is no currently no way to get non-enumerable properties in a streaming fashion (there is for enumerable properties: Reflect.enumerate()), so if you call Object.getOwnPropertyNames() on an object with 10 million non-enumerable properties, you still stand a good chance of hitting a fatal out-of-memory exception.

    EDIT: Apropos Reflect.enumerate(), it was removed from the ES6 draft and will probably be removed in V8 5.0. Better not use it.

  26. mik01aj commented on Mar 23, 2016

    @mik01aj
    mik01aj
    on Mar 23, 2016
    Author
    More actions

    @bnoordhuis I didn't check that @tunniclm's solution actually prevents the crash. So you might be right that it doesn't fix it.

    On the other hand Object.getOwnPropertyNames() returns just 2 keys ('1000000', 'length') for a sparse array, so the problem is somewhere else, maybe even just in building the output string.

  27. tunniclm commented on Mar 23, 2016

    @tunniclm
    tunniclm
    on Mar 23, 2016
    More actions

    @bnoordhuis The code change I have only really addresses the memory consumed by the output array, which is built from formatting the key-value pairs. This is pretty good for a sparse array with few keys that would otherwise balloon in size by formatting out many unset slots. However, it would only help a little for dense arrays, which would still call Object.keys() / Object.getOwnPropertyNames() and presumably therefore generate large objects from those calls alone.

    With limits on the output array enabled, a console.log() of a sparse 1,000,000,000 element array works, but it crashes when the limits are disabled:

    > u=require('util'); 
       u.MAX_UNSET_ELEMENTS_BEFORE_ELIDE=0; 
       u.MAX_ARRAY_OUTPUT_ELEMENTS=10; 
       a=[]; 
       a[1000000000]=1;
    1
    > a
    [ , , , , , , , , , <999999992 more> ]
    > u.MAX_ARRAY_OUTPUT_ELEMENTS=0;
    0
    > a
    <--- Last few GCs --->
    
      232414 ms: Mark-sweep 755.5 (796.8) -> 574.6 (615.8) MB, 144.3 / 0 ms [allocation failure] [GC in old space requested].
      232547 ms: Mark-sweep 574.6 (615.8) -> 574.6 (614.8) MB, 133.3 / 0 ms [allocation failure] [GC in old space requested].
      232682 ms: Mark-sweep 574.6 (614.8) -> 574.6 (613.8) MB, 135.2 / 0 ms [last resort gc].
      232815 ms: Mark-sweep 574.6 (613.8) -> 574.6 (612.8) MB, 132.7 / 0 ms [last resort gc].
    
    
    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    Security context: 0x1a3a7d5c1ec1 <JS Object>
        2: pushOutputElement(aka pushOutputElement) [util.js:~548] [pc=0x152b8acf460f] (this=0x1a3a7d504189 <undefined>,element=0x1a3a7d5042e1 <String[0]: >,increment=1,remaining=925317035)
        3: formatArray(aka formatArray) [util.js:~541] [pc=0x152b8acf7ffa] (this=0x1a3a7d504189 <undefined>,ctx=0x14960ad14319 <an Object with map 0x378af902aab9>,value=0x14960ad14341 <JS Array[1000000001]>,recurseT...
    
    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
    Aborted
    
  28. jasnell
    added
    confirmed-bugIssues and PRs for confirmed bugs.
    on Apr 22, 2016
  29. jasnell commented on May 3, 2016

    @jasnell
    jasnell
    on May 3, 2016
    Member
    More actions

    #6334 landed. Closing.

  30. jasnell
    closed this as completedon May 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.consoleIssues and PRs related to the console subsystem.utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.