index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. const SEA = require('./sea')
  2. const Gun = SEA.Gun;
  3. // After we have a GUN extension to make user registration/login easy, we then need to handle everything else.
  4. // We do this with a GUN adapter, we first listen to when a gun instance is created (and when its options change)
  5. Gun.on('opt', function(at){
  6. if(!at.sea){ // only add SEA once per instance, on the "at" context.
  7. at.sea = {own: {}};
  8. at.on('in', security, at); // now listen to all input data, acting as a firewall.
  9. at.on('out', signature, at); // and output listeners, to encrypt outgoing data.
  10. at.on('node', each, at);
  11. }
  12. this.to.next(at); // make sure to call the "next" middleware adapter.
  13. });
  14. // Alright, this next adapter gets run at the per node level in the graph database.
  15. // This will let us verify that every property on a node has a value signed by a public key we trust.
  16. // If the signature does not match, the data is just `undefined` so it doesn't get passed on.
  17. // If it does match, then we transform the in-memory "view" of the data into its plain value (without the signature).
  18. // Now NOTE! Some data is "system" data, not user data. Example: List of public keys, aliases, etc.
  19. // This data is self-enforced (the value can only match its ID), but that is handled in the `security` function.
  20. // From the self-enforced data, we can see all the edges in the graph that belong to a public key.
  21. // Example: ~ASDF is the ID of a node with ASDF as its public key, signed alias and salt, and
  22. // its encrypted private key, but it might also have other signed values on it like `profile = <ID>` edge.
  23. // Using that directed edge's ID, we can then track (in memory) which IDs belong to which keys.
  24. // Here is a problem: Multiple public keys can "claim" any node's ID, so this is dangerous!
  25. // This means we should ONLY trust our "friends" (our key ring) public keys, not any ones.
  26. // I have not yet added that to SEA yet in this alpha release. That is coming soon, but beware in the meanwhile!
  27. function each(msg){ // TODO: Warning: Need to switch to `gun.on('node')`! Do not use `Gun.on('node'` in your apps!
  28. // NOTE: THE SECURITY FUNCTION HAS ALREADY VERIFIED THE DATA!!!
  29. // WE DO NOT NEED TO RE-VERIFY AGAIN, JUST TRANSFORM IT TO PLAINTEXT.
  30. var to = this.to, vertex = (msg.$._).put, c = 0, d;
  31. Gun.node.is(msg.put, function(val, key, node){ c++; // for each property on the node
  32. // TODO: consider async/await use here...
  33. SEA.verify(val, false, function(data){ c--; // false just extracts the plain data.
  34. var tmp = data;
  35. data = SEA.opt.unpack(data, key, node);
  36. node[key] = val = data; // transform to plain value.
  37. if(d && !c && (c = -1)){ to.next(msg) }
  38. });
  39. });
  40. d = true;
  41. if(d && !c){ to.next(msg) }
  42. return;
  43. }
  44. // signature handles data output, it is a proxy to the security function.
  45. function signature(msg){
  46. if(msg.user){
  47. return this.to.next(msg);
  48. }
  49. var ctx = this.as;
  50. msg.user = ctx.user;
  51. security.call(this, msg);
  52. }
  53. // okay! The security function handles all the heavy lifting.
  54. // It needs to deal read and write of input and output of system data, account/public key data, and regular data.
  55. // This is broken down into some pretty clear edge cases, let's go over them:
  56. function security(msg){
  57. var at = this.as, sea = at.sea, to = this.to;
  58. if(msg.get){
  59. // if there is a request to read data from us, then...
  60. var soul = msg.get['#'];
  61. if(soul){ // for now, only allow direct IDs to be read.
  62. if(typeof soul !== 'string'){ return to.next(msg) } // do not handle lexical cursors.
  63. if('alias' === soul){ // Allow reading the list of usernames/aliases in the system?
  64. return to.next(msg); // yes.
  65. } else
  66. if('~@' === soul.slice(0,2)){ // Allow reading the list of public keys associated with an alias?
  67. return to.next(msg); // yes.
  68. } else { // Allow reading everything?
  69. return to.next(msg); // yes // TODO: No! Make this a callback/event that people can filter on.
  70. }
  71. }
  72. }
  73. if(msg.put){
  74. // potentially parallel async operations!!!
  75. var check = {}, each = {}, u;
  76. each.node = function(node, soul){
  77. if(Gun.obj.empty(node, '_')){ return check['node'+soul] = 0 } // ignore empty updates, don't reject them.
  78. Gun.obj.map(node, each.way, {soul: soul, node: node});
  79. };
  80. each.way = function(val, key){
  81. var soul = this.soul, node = this.node, tmp;
  82. if('_' === key){ return } // ignore meta data
  83. if('~@' === soul){ // special case for shared system data, the list of aliases.
  84. each.alias(val, key, node, soul); return;
  85. }
  86. if('~@' === soul.slice(0,2)){ // special case for shared system data, the list of public keys for an alias.
  87. each.pubs(val, key, node, soul); return;
  88. }
  89. if('~' === soul.slice(0,1) && 2 === (tmp = soul.slice(1)).split('.').length){ // special case, account data for a public key.
  90. each.pub(val, key, node, soul, tmp, msg.user); return;
  91. }
  92. each.any(val, key, node, soul, msg.user); return;
  93. return each.end({err: "No other data allowed!"});
  94. };
  95. each.alias = function(val, key, node, soul){ // Example: {_:#~@, ~@alice: {#~@alice}}
  96. if(!val){ return each.end({err: "Data must exist!"}) } // data MUST exist
  97. if('~@'+key === Gun.val.link.is(val)){ return check['alias'+key] = 0 } // in fact, it must be EXACTLY equal to itself
  98. each.end({err: "Mismatching alias."}); // if it isn't, reject.
  99. };
  100. each.pubs = function(val, key, node, soul){ // Example: {_:#~@alice, ~asdf: {#~asdf}}
  101. if(!val){ return each.end({err: "Alias must exist!"}) } // data MUST exist
  102. if(key === Gun.val.link.is(val)){ return check['pubs'+soul+key] = 0 } // and the ID must be EXACTLY equal to its property
  103. each.end({err: "Alias must match!"}); // that way nobody can tamper with the list of public keys.
  104. };
  105. each.pub = function(val, key, node, soul, pub, user){ // Example: {_:#~asdf, hello:SEA{'world',fdsa}}
  106. if('pub' === key){
  107. if(val === pub){ return (check['pub'+soul+key] = 0) } // the account MUST match `pub` property that equals the ID of the public key.
  108. return each.end({err: "Account must match!"});
  109. }
  110. check['user'+soul+key] = 1;
  111. if(user && user.is && pub === user.is.pub){
  112. //var id = Gun.text.random(3);
  113. SEA.sign([soul, key, val, Gun.state.is(node, key)], (user._).sea, function(data){ var rel;
  114. if(u === data){ return each.end({err: SEA.err || 'Pub signature fail.'}) }
  115. if(rel = Gun.val.link.is(val)){
  116. (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
  117. }
  118. node[key] = data;
  119. check['user'+soul+key] = 0;
  120. each.end({ok: 1});
  121. });
  122. // TODO: Handle error!!!!
  123. return;
  124. }
  125. SEA.verify(val, pub, function(data){ var rel, tmp;
  126. data = SEA.opt.unpack(data, key, node);
  127. if(u === data){ // make sure the signature matches the account it claims to be on.
  128. return each.end({err: "Unverified data."}); // reject any updates that are signed with a mismatched account.
  129. }
  130. if((rel = Gun.val.link.is(data)) && pub === relpub(rel)){
  131. (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
  132. }
  133. check['user'+soul+key] = 0;
  134. each.end({ok: 1});
  135. });
  136. };
  137. function relpub(s){
  138. if(!s){ return }
  139. s = s.split('~');
  140. if(!s || !(s = s[1])){ return }
  141. s = s.split('.');
  142. if(!s || 2 > s.length){ return }
  143. s = s.slice(0,2).join('.');
  144. return s;
  145. }
  146. each.any = function(val, key, node, soul, user){ var tmp, pub;
  147. if(!user || !user.is){
  148. if(tmp = relpub(soul)){
  149. check['any'+soul+key] = 1;
  150. SEA.verify(val, pub = tmp, function(data){ var rel;
  151. data = SEA.opt.unpack(data, key, node);
  152. if(u === data){ return each.end({err: "Mismatched owner on '" + key + "'."}) } // thanks @rogowski !
  153. if((rel = Gun.val.link.is(data)) && pub === relpub(rel)){
  154. (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
  155. }
  156. check['any'+soul+key] = 0;
  157. each.end({ok: 1});
  158. });
  159. return;
  160. }
  161. check['any'+soul+key] = 1;
  162. at.on('secure', function(msg){ this.off();
  163. check['any'+soul+key] = 0;
  164. if(at.opt.secure){ msg = null }
  165. each.end(msg || {err: "Data cannot be modified."});
  166. }).on.on('secure', msg);
  167. //each.end({err: "Data cannot be modified."});
  168. return;
  169. }
  170. if(!(tmp = relpub(soul))){
  171. if(at.opt.secure){
  172. each.end({err: "Soul is missing public key at '" + key + "'."});
  173. return;
  174. }
  175. if(val && val.slice && 'SEA{' === (val).slice(0,4)){
  176. check['any'+soul+key] = 0;
  177. each.end({ok: 1});
  178. return;
  179. }
  180. //check['any'+soul+key] = 1;
  181. //SEA.sign(val, user, function(data){
  182. // if(u === data){ return each.end({err: 'Any signature failed.'}) }
  183. // node[key] = data;
  184. check['any'+soul+key] = 0;
  185. each.end({ok: 1});
  186. //});
  187. return;
  188. }
  189. if(!msg.I || (pub = tmp) !== (user.is||noop).pub){
  190. each.any(val, key, node, soul);
  191. return;
  192. }
  193. /*var other = Gun.obj.map(at.sea.own[soul], function(v, p){
  194. if((user.is||{}).pub !== p){ return p }
  195. });
  196. if(other){
  197. each.any(val, key, node, soul);
  198. return;
  199. }*/
  200. check['any'+soul+key] = 1;
  201. SEA.sign([soul, key, val, Gun.state.is(node, key)], (user._).sea, function(data){
  202. if(u === data){ return each.end({err: 'My signature fail.'}) }
  203. node[key] = data;
  204. check['any'+soul+key] = 0;
  205. each.end({ok: 1});
  206. });
  207. }
  208. each.end = function(ctx){ // TODO: Can't you just switch this to each.end = cb?
  209. if(each.err){ return }
  210. if((each.err = ctx.err) || ctx.no){
  211. console.log('NO!', each.err, msg.put); // 451 mistmached data FOR MARTTI
  212. return;
  213. }
  214. if(!each.end.ed){ return }
  215. if(Gun.obj.map(check, function(no){
  216. if(no){ return true }
  217. })){ return }
  218. to.next(msg);
  219. };
  220. Gun.obj.map(msg.put, each.node);
  221. each.end({end: each.end.ed = true});
  222. return; // need to manually call next after async.
  223. }
  224. to.next(msg); // pass forward any data we do not know how to handle or process (this allows custom security protocols).
  225. }
  226. SEA.opt.unpack = function(data, key, node){
  227. if(u === data){ return }
  228. var tmp = data, soul = Gun.node.soul(node), s = Gun.state.is(node, key);
  229. if(tmp && 4 === tmp.length && soul === tmp[0] && key === tmp[1] && s === tmp[3]){
  230. return tmp[2];
  231. }
  232. if(s < SEA.opt.shuffle_attack){
  233. return data;
  234. }
  235. }
  236. SEA.opt.shuffle_attack = 1546329600000; // Jan 1, 2019
  237. var noop = {}, u;