Using a JSON.parse reviver to obfuscate fields

后端 未结 2 1092
梦毁少年i
梦毁少年i 2021-02-06 15:06

I am attempting to abuse a reviver function with JSON.parse.

I basically want to make certain fields \"null\".

If I do this:

var json_data = JSON         


        
2条回答
  •  Happy的楠姐
    2021-02-06 15:41

    Through some experimentation, it looks like a final call is made to the function where the key is an empty string and the value is the top-level object:

    > JSON.parse('{"hello": "world"}', function(k, v) { console.log(arguments); return v; })
    ["hello", "world"]
    ["", Object]
    

    So you could use:

    var json_data = JSON.parse(j, function(key, value) {
      if (key == "name" || key === "") {        
        return value;
      } else {
        return null;    
      }    
    });
    

    Now, since "" does appear to be a valid JSON key, to be 100% correct it might be better to use something like:

    var json_data;
    JSON.parse(j, function(key, value) {
      if (key == "name") {        
        return value;
      } else if (key === "") {
        json_data = value;
        return null;
      } else {
        return null;    
      }    
    });
    

    But that might be a little bit paranoid ;)

提交回复
热议问题