json.test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. 'use strict';
  2. const chai = require('chai');
  3. const expect = chai.expect;
  4. const Support = require('../support');
  5. const { DataTypes, Op } = require('@sequelize/core');
  6. const current = Support.sequelize;
  7. const dialect = current.dialect;
  8. describe(Support.getTestDialectTeaser('Model'), () => {
  9. describe('JSON', () => {
  10. if (!dialect.supports.dataTypes.JSON) {
  11. return;
  12. }
  13. beforeEach(async function () {
  14. this.Event = this.sequelize.define('Event', {
  15. data: {
  16. // TODO: JSON & JSONB tests should be split
  17. type: dialect.name === 'postgres' ? DataTypes.JSONB : DataTypes.JSON,
  18. field: 'event_data',
  19. // This is only available on JSONB
  20. index: dialect.name === 'postgres',
  21. },
  22. json: DataTypes.JSON,
  23. });
  24. await this.Event.sync({ force: true });
  25. });
  26. if (current.dialect.supports.lock) {
  27. it('findOrCreate supports transactions, json and locks', async function () {
  28. const transaction = await current.startUnmanagedTransaction();
  29. await this.Event.findOrCreate({
  30. where: {
  31. json: { 'some.input:unquote': 'Hello' },
  32. },
  33. defaults: {
  34. json: { some: { input: 'Hello' }, input: [1, 2, 3] },
  35. data: { some: { input: 'There' }, input: [4, 5, 6] },
  36. },
  37. transaction,
  38. lock: transaction.LOCK.UPDATE,
  39. logging: sql => {
  40. if (sql.includes('SELECT') && !sql.includes('CREATE')) {
  41. expect(sql.includes('FOR UPDATE')).to.be.true;
  42. }
  43. },
  44. });
  45. const count = await this.Event.count();
  46. expect(count).to.equal(0);
  47. await transaction.commit();
  48. const count0 = await this.Event.count();
  49. expect(count0).to.equal(1);
  50. });
  51. }
  52. describe('create', () => {
  53. it('should create an instance with JSON data', async function () {
  54. await this.Event.create({
  55. data: {
  56. name: {
  57. first: 'Homer',
  58. last: 'Simpson',
  59. },
  60. employment: 'Nuclear Safety Inspector',
  61. },
  62. });
  63. const events = await this.Event.findAll();
  64. const event = events[0];
  65. expect(event.get('data')).to.eql({
  66. name: {
  67. first: 'Homer',
  68. last: 'Simpson',
  69. },
  70. employment: 'Nuclear Safety Inspector',
  71. });
  72. });
  73. });
  74. describe('find', () => {
  75. if (!dialect.supports.jsonOperations || !dialect.supports.jsonExtraction.quoted) {
  76. return;
  77. }
  78. it('should be possible to query multiple nested values', async function () {
  79. await this.Event.create({
  80. data: {
  81. name: {
  82. first: 'Homer',
  83. last: 'Simpson',
  84. },
  85. employment: 'Nuclear Safety Inspector',
  86. },
  87. });
  88. await Promise.all([
  89. this.Event.create({
  90. data: {
  91. name: {
  92. first: 'Marge',
  93. last: 'Simpson',
  94. },
  95. employment: 'Housewife',
  96. },
  97. }),
  98. this.Event.create({
  99. data: {
  100. name: {
  101. first: 'Bart',
  102. last: 'Simpson',
  103. },
  104. employment: 'None',
  105. },
  106. }),
  107. ]);
  108. const events = await this.Event.findAll({
  109. where: {
  110. data: {
  111. name: {
  112. last: 'Simpson',
  113. },
  114. employment: {
  115. [Op.ne]: 'None',
  116. },
  117. },
  118. },
  119. order: [['id', 'ASC']],
  120. });
  121. expect(events).to.have.length(2);
  122. expect(events[0].get('data')).to.eql({
  123. name: {
  124. first: 'Homer',
  125. last: 'Simpson',
  126. },
  127. employment: 'Nuclear Safety Inspector',
  128. });
  129. expect(events[1].get('data')).to.eql({
  130. name: {
  131. first: 'Marge',
  132. last: 'Simpson',
  133. },
  134. employment: 'Housewife',
  135. });
  136. });
  137. it('should be possible to query a nested value and order results', async function () {
  138. await this.Event.create({
  139. data: {
  140. name: {
  141. first: 'Homer',
  142. last: 'Simpson',
  143. },
  144. employment: 'Nuclear Safety Inspector',
  145. },
  146. });
  147. await Promise.all([
  148. this.Event.create({
  149. data: {
  150. name: {
  151. first: 'Marge',
  152. last: 'Simpson',
  153. },
  154. employment: 'Housewife',
  155. },
  156. }),
  157. this.Event.create({
  158. data: {
  159. name: {
  160. first: 'Bart',
  161. last: 'Simpson',
  162. },
  163. employment: 'None',
  164. },
  165. }),
  166. ]);
  167. const events = await this.Event.findAll({
  168. where: {
  169. data: {
  170. name: {
  171. last: 'Simpson',
  172. },
  173. },
  174. },
  175. order: [['data.name.first']],
  176. });
  177. expect(events.length).to.equal(3);
  178. expect(events[0].get('data')).to.eql({
  179. name: {
  180. first: 'Bart',
  181. last: 'Simpson',
  182. },
  183. employment: 'None',
  184. });
  185. expect(events[1].get('data')).to.eql({
  186. name: {
  187. first: 'Homer',
  188. last: 'Simpson',
  189. },
  190. employment: 'Nuclear Safety Inspector',
  191. });
  192. expect(events[2].get('data')).to.eql({
  193. name: {
  194. first: 'Marge',
  195. last: 'Simpson',
  196. },
  197. employment: 'Housewife',
  198. });
  199. });
  200. });
  201. describe('destroy', () => {
  202. if (!dialect.supports.jsonOperations || !dialect.supports.jsonExtraction.quoted) {
  203. return;
  204. }
  205. it('should be possible to destroy with where', async function () {
  206. const conditionSearch = {
  207. where: {
  208. data: {
  209. employment: 'Hacker',
  210. },
  211. },
  212. };
  213. await Promise.all([
  214. this.Event.create({
  215. data: {
  216. name: {
  217. first: 'Elliot',
  218. last: 'Alderson',
  219. },
  220. employment: 'Hacker',
  221. },
  222. }),
  223. this.Event.create({
  224. data: {
  225. name: {
  226. first: 'Christian',
  227. last: 'Slater',
  228. },
  229. employment: 'Hacker',
  230. },
  231. }),
  232. this.Event.create({
  233. data: {
  234. name: {
  235. first: ' Tyrell',
  236. last: 'Wellick',
  237. },
  238. employment: 'CTO',
  239. },
  240. }),
  241. ]);
  242. await expect(this.Event.findAll(conditionSearch)).to.eventually.have.length(2);
  243. await this.Event.destroy(conditionSearch);
  244. await expect(this.Event.findAll(conditionSearch)).to.eventually.have.length(0);
  245. });
  246. });
  247. describe('sql injection attacks', () => {
  248. beforeEach(async function () {
  249. this.Model = this.sequelize.define('Model', {
  250. data: DataTypes.JSON,
  251. });
  252. await this.sequelize.sync({ force: true });
  253. });
  254. if (dialect.supports.jsonOperations && dialect.supports.jsonExtraction.quoted) {
  255. it('should query an instance with JSONB data and order while trying to inject', async function () {
  256. await this.Event.create({
  257. data: {
  258. name: {
  259. first: 'Homer',
  260. last: 'Simpson',
  261. },
  262. employment: 'Nuclear Safety Inspector',
  263. },
  264. });
  265. await Promise.all([
  266. this.Event.create({
  267. data: {
  268. name: {
  269. first: 'Marge',
  270. last: 'Simpson',
  271. },
  272. employment: 'Housewife',
  273. },
  274. }),
  275. this.Event.create({
  276. data: {
  277. name: {
  278. first: 'Bart',
  279. last: 'Simpson',
  280. },
  281. employment: 'None',
  282. },
  283. }),
  284. ]);
  285. const events = await this.Event.findAll({
  286. where: {
  287. data: {
  288. name: {
  289. last: 'Simpson',
  290. },
  291. },
  292. },
  293. order: [["data.name.first}'); INSERT INJECTION HERE! SELECT ('"]],
  294. });
  295. expect(events).to.be.ok;
  296. expect(events[0].get('data')).to.eql({
  297. name: {
  298. first: 'Homer',
  299. last: 'Simpson',
  300. },
  301. employment: 'Nuclear Safety Inspector',
  302. });
  303. });
  304. }
  305. });
  306. });
  307. });