Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js のセキュリティの話

Node.js のセキュリティの話

Production (セキュリティ編)ということで疎かになりがちなセキュリティの話

Yosuke Furukawa

June 14, 2014
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. ։࠵༧ఆΠϕϯτ: 6/23 : ౦ژNodeֶԂ13࣌ݶ໨ 7/3 : socket.io meetup 7/11 :

    ఱԼҰΫϥΠΞϯταΠυJSϑϨʔϜϫʔΫ෢ಆձ ʊਓਓਓਓਓਓਓʊ ʼɹશ෦౦ژʂʂɹʻ ʉ:?:?:?:?:?:?:ʉ
  2. csrfରࡦ (tokenํࣜ) // express var express = require(‘express'); var csrf

    = require(‘csurf'); ! var app = express(); app.use(csrf()); ! ! // html ͷ form ΍ xhr Ͱ ϦΫΤετͷதʹ tokenΛೖΕͯૹΔ <input type=“hidden” name=“_csrf” value=“{{csrftoken}}”>
  3. csrfରࡦ (xhr͔Ͳ͏͔Λ֬ೝ͢Δ) // express var express = require(‘express'); ! var

    app = express(); app.use(function(req, res){ if (req.xhr) { // => X-Requested-With ͕͍ͭͯΔͱreq.xhr͸true // xhr͸same origin policy͕ద༻͞ΕΔͨΊɺผυϝΠϯ͔Β // ϦΫΤετ͸ൃߦͰ͖ͳ͍ } // … }); !
  4. x-powered-byΛফ͢ // express var express = require(‘express'); var app =

    express(); app.disable(‘x-powered-by’); ! // ͜͏͢Δ͜ͱͰ // ߈ܸऀʹόοΫΤϯυͷαʔόʔ͕Կ͔఻͑ͳ͍ɻ
  5. httpsͰexpress var express = require(‘express’); var https = require('https'); var

    fs = require('fs'); ! var options = { key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') }; ! express.createServer(options); ! // ·͊Ͱ΋ϦόʔεϓϩΩγͱ͔ͰhttpsΛhttpʹม׵͢Δ͜ͱͷํ͕ଟ͍͔΋
  6. JSON.parse/JSON.stringify // JSON.parse/JSON.stringify͸ಉظతͳॲཧ // ௒ڊେͳJSON͕དྷΔͱͦͷparse/stringifyͷλΠϛϯάͰॲཧ͕ࢭ·Δɻ // ͳͷͰɺϦΫΤετͷίϯςϯταΠζΛݟͯͪΌΜͱ஄͘Α͏ʹͨ͠΄͏͕ྑ ͍ɻ ! var

    length = +req[‘content-length’]; if (length > 100000) { throw new Error(“Max content size is exceeded”); } ! // ͪͳΈʹexpress/body-parser͸ 100kb ·ͰͰσϑΥϧτ੍ݶ͞ΕͯΔɻ // ࠓ JSON.parse/stringifyʹasync͕෇͔͘Ͳ͏͔ݕ౼͞ΕͯΔ // https://github.com/joyent/node/issues/7543