{"id":4096,"date":"2010-03-23T00:36:39","date_gmt":"2010-03-22T16:36:39","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=4096"},"modified":"2015-12-01T16:27:22","modified_gmt":"2015-12-01T08:27:22","slug":"ruby-on-rails-security-best-practices","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/4096-ruby-on-rails-security-best-practices","title":{"rendered":"Ruby on Rails Security \u6700\u4f73\u5be6\u52d9"},"content":{"rendered":"<p>\u9019\u662f\u6211 2\/26 \u5728\u4e2d\u7814\u9662 OSSF \u5de5\u4f5c\u574a\u548c 3\/23  \u5728 Ruby Tuesday \u6240\u6f14\u8b1b\u7684\u984c\u76ee\u4e4b\u4e00\u3002<\/p>\n<p>Ruby on Rails \u8981\u8ac7\u7684 Security \u7db2\u7ad9\u5b89\u5168\uff0c\u4e3b\u8981\u5728 Web application layer \u7684\u7bc4\u570d(\u9019\u4e5f\u662f\u6700\u5bb9\u6613\u88ab\u653b\u64ca\u7684\u90e8\u5206)\uff0c\u50cf\u662f XSS\u3001CSRF\u3001Session Hijacking \u8ddf Fixation\u3001SQL Injection \u7b49\u7b49\u90fd\u662f\u6240\u6709 Web \u61c9\u7528\u7a0b\u5f0f\u5fc5\u9808\u8655\u7406\u7684\u554f\u984c\u3002\u4e5f\u6709\u4e00\u4e9b\u662f Rails \u70ba\u4e86\u7a0b\u5f0f\u65b9\u4fbf\u6027\u800c\u88fd\u9020\u51fa\u4f86\u7684\u554f\u984c\uff0c\u50cf\u662f Mass assignment\u3001Unscoped finds\u3001Controller Exposing methods \u7b49\u7b49\u3002<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/www.slideshare.net\/slideshow\/embed_code\/key\/zWjDyVEYNk7eL7\" width=\"595\" height=\"485\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" style=\"border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;\" allowfullscreen> <\/iframe><\/p>\n<div style=\"margin-bottom:5px\"> <strong> <a href=\"\/\/www.slideshare.net\/ihower\/rails-security-3299368\" title=\"Rails Security\" target=\"_blank\">Rails Security<\/a> <\/strong> from <strong><a href=\"\/\/www.slideshare.net\/ihower\" target=\"_blank\">Wen-Tien Chang<\/a><\/strong> <\/div>\n<p>\u6703\u8b1b\u7684\u5167\u5bb9\u770b\u6295\u5f71\u7247\u5c31\u53ef\u4ee5\u4e86\uff0c\u9019\u88e1\u6211\u60f3\u7279\u5225\u63d0\u4e00\u4e0b\u6211\u7684\u958b\u5834\u8ddf\u4e00\u500b\u89c0\u5ff5:<\/p>\n<p>\u6211\u7684\u958b\u5834\u5f15\u7528\u4e86 <a href=\"http:\/\/phpsec.org\/projects\/guide\/1.html#1.1\">PHP Security Guide: Overview<\/a> \u4ecb\u7d39\u4ec0\u9ebc\u662f Security\uff0c\u6211\u5f88\u559c\u6b61\uff1a<\/p>\n<ol>\n<li>\u5b89\u5168\u6027\u662f\u76f8\u5c0d\u7684\uff0c\u4e0d\u662f\u4e00\u500b\u529f\u80fd\u3002\u78b0\u904e\u592a\u591a\u5ba2\u6236\u8981\u6c42 &#8220;\u7db2\u7ad9\u8981\u7d55\u5c0d\u5b89\u5168\uff0c\u4e0d\u80fd\u88ab HACK&#8221;\uff0c\u9019\u5be6\u5728\u592a\u5f37\u4eba\u6240\u96e3\u4e86\u3002\u5b89\u5168\u6027\u5c31\u8ddf\u6eab\u5ea6\u4e00\u6a23\uff0c\u76f8\u5c0d\u71b1\u3001\u76f8\u5c0d\u51b7\u3002\u5b89\u5168\u6027\u4e5f\u662f\u76f8\u5c0d\u5b89\u5168\u3001\u76f8\u5c0d\u4e0d\u5b89\u5168\u7684\uff0c\u800c\u4e0d\u662f\u50cf\u529f\u80fd\u8aaa\u6709\u6216\u6c92\u6709\u3002<\/li>\n<li>\u627f\u4e0a\uff0c\u8981\u8d8a\u5b89\u5168\uff0c\u5c31\u7d66\u82b1\u8d8a\u591a\u6210\u672c\u53bb\u505a\u3002\u4e0d\u904e\u5462\uff0c\u4e0d\u9700\u8981\u591a\u82b1\u4ec0\u9ebc\u9322\uff0c\u5c31\u53ef\u4ee5\u5f88\u7c21\u55ae\u505a\u5230\u8db3\u5920\u5730\u5b89\u5168\u3002\u5982\u679c\u8981\u975e\u5e38\u975e\u5e38\u5b89\u5168\uff0c\u5c31\u6703\u975e\u5e38\u975e\u5e38\u8cb4\u4e86\u3002\u6240\u4ee5\u8acb\u8003\u91cf\u4f60\u7684\u9810\u7b97\u3002<\/li>\n<li>\u5fc5\u9808\u8981\u8207\u4f7f\u7528\u6027(usability)\u505a\u5e73\u8861\u3002\u5f88\u591a\u6642\u5019\uff0c\u5b89\u5168\u6027\u8ddf\u4f7f\u7528\u6027\u6703\u662f\u885d\u7a81\u7684\uff0c\u60f3\u8981\u8d8a\u591a\u5b89\u5168\u6027\uff0c\u5c31\u7d66\u72a7\u7272\u6389\u4e00\u4e9b\u4f7f\u7528\u4e0a\u7684\u65b9\u4fbf\u6027\u3002<\/li>\n<li>\u5b89\u5168\u6027\u5fc5\u9808\u662f\u7db2\u7ad9\u7a0b\u5f0f\u8a2d\u8a08\u904e\u7a0b\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6700\u5f8c\u624d\u52a0\u4e0a\u53bb\u3002<\/li>\n<\/ol>\n<p>\u800c\u60f3\u63d0\u7684\u5beb\u7a0b\u5f0f\u89c0\u5ff5\u662f\u6211\u770b <a href=\"http:\/\/www.pragprog.com\/titles\/fr_secure\/security-on-rails\">Security on Rails<\/a> \u4e00\u66f8\u88e1\u9762\u4ecb\u7d39\u7684 Fail Close\uff0c\u5e95\u4e0b\u7b2c\u4e00\u6bb5\u662f\u7528 Fail open \u89c0\u5ff5\u5beb\u7684\u7a0b\u5f0f\uff0c\u7b2c\u4e8c\u6bb5\u662f\u7528 Fail close\u3002<\/p>\n<pre>\n<code>\n# fail open way, it\u2019s bad\ndef show\n  @invoice = Invoice.find(params[:id])\n  unless @user.validate_code( @invoice.code )\n    redirect_to :action => 'not_authorized'\n  end\nend\n<\/code>\n<\/pre>\n<pre>\n<code>\n# fail close way\ndef show\n  @invoice = Invoice.find(params[:id])\n  if @user.validate_code( @invoice.code )\n    redirect_to :action => 'authorized\n  else\n    redirect_to :action => 'not_authorized'\n   end\nend\n<\/code>\n<\/pre>\n<p>\u5176\u4e2d\u7684\u7aef\u502a\u5c31\u662f\uff0c\u5728\u64b0\u5beb\u9a57\u8b49\u5b89\u5168\u6027\u7a0b\u5f0f\u78bc\u7684\u6642\u5019\uff0c\u8acb\u7528 Fail close \u7684\u89c0\u5ff5\uff1a&#8221;\u5982\u679c\u689d\u4ef6\u6210\u529f\uff0c\u624d\u5141\u8a31\u9032\u884c\uff0c\u4e0d\u7136\u5c31\u4e0d\u5141\u8a31&#8221;\u3002\u800c\u4e0d\u662f Fail open\uff1a&#8221;\u5982\u679c\u689d\u4ef6\u4e0d\u6210\u529f\uff0c\u624d\u4e0d\u5141\u8a31\uff0c\u4e0d\u7136\u5c31\u5141\u8a31\u3002&#8221;\u3002\u9019\u500b\u7c21\u55ae\u7684\u64b0\u78bc\u89c0\u5ff5\uff0c\u53ef\u4ee5\u6e1b\u5c11\u6211\u5011\u5beb\u51fa\u6f0f\u6d1e\u7a0b\u5f0f\u78bc\u7684\u6a5f\u6703\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9019\u662f\u6211 2\/26 \u5728\u4e2d\u7814\u9662 OSSF \u5de5\u4f5c\u574a\u548c 3\/23 \u5728 Ruby Tuesday \u6240\u6f14\u8b1b\u7684\u984c\u76ee\u4e4b\u4e00\u3002 R &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/4096-ruby-on-rails-security-best-practices\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Ruby on Rails Security \u6700\u4f73\u5be6\u52d9\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53,40],"tags":[],"class_list":["post-4096","post","type-post","status-publish","format-standard","hentry","category-rails","category-security","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-144","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=4096"}],"version-history":[{"count":18,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4096\/revisions"}],"predecessor-version":[{"id":8405,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4096\/revisions\/8405"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=4096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=4096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=4096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}