简体   繁体   English

使用JSON.parse reviver来混淆字段

[英]Using a JSON.parse reviver to obfuscate fields

I am attempting to abuse a reviver function with JSON.parse. 我试图用JSON.parse滥用reviver函数。

I basically want to make certain fields "null". 我基本上想让某些字段为“null”。

If I do this: 如果我这样做:

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

The entire json_data object ends up null. 整个json_data对象最终为null。 In fact, no matter what I make the else, that defines the value of the json_object. 事实上,无论我做了什么,都定义了json_object的值。

Interestingly, this works as expected: 有趣的是,这可以按预期工作:

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

The property "name" now has a value of "name". 属性“name”现在具有值“name”。

JSON in question: 有问题的JSON:

var j = '{"uuid":"62cfb2ec-9e43-11e1-abf2-70cd60fffe0e","count":1,"name":"Marvin","date":"2012-05-13T14:06:45+10:00"}';

Update 更新

I just realized that the inverse of what I want to do works as well so I can nullify the name field: 我刚刚意识到我想要做的事情的反面也是如此,所以我可以取消名称字段:

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

It has a rather interesting behavior that the entire object is included in the objects passed to the reviver. 它有一个相当有趣的行为,整个对象包含在传递给reviver的对象中。

When the entire object is passed, the key is null. 传递整个对象时,键为空。

http://jsfiddle.net/sGYGM/7/ http://jsfiddle.net/sGYGM/7/

var j = '{"uuid":"62cfb2ec-9e43-11e1-abf2-70cd60fffe0e","count":1,"name":"Marvin","date":"2012-05-13T14:06:45+10:00"}';

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

console.log(json_data);

As per https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/parse 根据https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/parse

The reviver is ultimately called with the empty string and the topmost value to permit transformation of the topmost value. 最终使用空字符串和最高值调用reviver,以允许转换最顶层的值。 Be certain to handle this case properly, usually by returning the provided value, or JSON.parse will return undefined. 一定要正确处理这种情况,通常是返回提供的值,否则JSON.parse将返回undefined。

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: 现在,由于""似乎确实是一个有效的JSON密钥,为了100%正确,最好使用类似的东西:

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 ;) 但那可能有点偏执;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM