shitou's blog 

  • Home
  • About
  • Feed
  • 十人族
  • MIX BOY
  • 塌客

Tags "rails"

28
Jul

file_column上传中文文件名的问题

rails的文件上传插件file_column上传的文件名是中文时将会失败,在网上baidu了好长时间,按照里面的做法修改了好几次
都还是没有成功,后来自己花了点时间研究了下,终于搞定了,修改如下:

修改file_column.rb文件中最后一个方法为

def self.sanitize_filename(filename)
    filename = File.basename(filename.gsub("\\", "/")) # work-around for IE #将windows系统路径的\替换成/
    #filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_") #将文件名中的除数字大小写字母.-+_之外的符号替换成_
    #filename = "_#{filename}" if filename =~ /^\.+$/ #如果文件名以.(本地路径)开头则在文件名前加上_
    #filename = "unnamed" if filename.size == 0 #如果文件名的长度=0,则赋予默认的名字unnamed
    time = Time.now.to_i
    filename = time.to_s + '.' + File.basename(filename).split('.').last
    filename
  end

通过上面我的注释可以看到不能上传中文文件名的原因应该在于:

    #filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_") #将文件名中的除数字大小写字母.-+_之外的符号替换成_

因为中文的编码问题,所以这里是出错的原因,在一般情况下可以把这行注释掉就可以了,因为后面我们把文件名给完全替换成数字了
所以可以把紧接后面文件名操作的两行也注释掉,
现在就行了

Tags: rails,file column

2008-07-28 17:52:45, 1047 reviews, comment

send to mailbox

Your email:

27
Jul

MIX BOY--自己写的音乐搜索程序

因为要学习ruby里的hpricot gem和URI gem的使用,所以就称这个机会自己做了一个音乐的搜索程序,取了个名字:Mixboy

功能:
a,音乐主要来源是yahoo的mp3搜索,所以不能保证所有的地址都有效,取到它的第一页数据,就是30条搜索结果,显示歌名和歌手
b,直接在线flash播放,可以下载
c,所有的交互都是基于ajax的

roadmap:
a,加入歌词显示的功能
b,加入dig的功能,保存网友dig音乐的地址


地址:Mixboy

Tags: mixboy,rails

2008-07-27 15:25:45, 1320 reviews, comment

send to mailbox

Your email:

23
Jul

用rails生成RSS

在rails中生成RSS,不需要使用其他插件,rails自动调用的ruby已内置XML框架,直接用就行了


Making RSS feeds for your Rails applications is one of those things that isn’t documented in very many places, but turns out to be fairly easy to do. Adding RSS feeds to your site is one of the best things you can do to retain a steady visitor base. It gives your visitors an easy way to be notified when you update your site with new content. Here’s the three parts you need to code in order to add an RSS feed.

    

The Controller Action

    

In the controller for your site you need to add an action for the feed generation, let’s call ours “feed”.

  
def feed
  @posts = Post.find(:all, :limit => 10, :order => "created_on DESC")
end
    

This will get the most recent ten posts out of our database and make them available for us to use in the builder template.

    

Also, if you are using a layout for your controller you need to not have it render for your feed or else it won’t work correctly. Something like this will fix it:

  
layout "my_layout", :except => [:feed]
    

The Builder Template

    

Builder templates are different from RHTML templates in that they represent a structured XML template instead of an HTML template.  They also end in .rxml instead of .rhtml.  Here’s the template we want to use that will follow the RSS 2.0 specification.  Here’s what our feed.rxml will look like.

  
xml.instruct! :xml, :version=>"1.0" 
xml.rss(:version=>"2.0"){
  xml.channel{
    xml.title("Your Blog's Title")
    xml.link("http://www.yoursite.tld/")
    xml.description("What your site is all about.")
    xml.language('en-us')
      for post in @posts
        xml.item do
          xml.title(post.title)
          xml.description(post.html_content)      
          xml.author("Your Name Here")              
          xml.pubDate(post.created_on.strftime("%a, %d %b %Y %H:%M:%S %z"))
          xml.link(post.link)
          xml.guid(post.link)
        end
      end
  }
}
    

If you compare this against the RSS 2.0 Specification you’ll see how it mirrors an RSS 2.0 XML file. The important points to note are that the strftime conversion for the pubDate is necessary, and that I’m using the link for each post as the guid as well since it is fairly safe to assume each post will have it’s own unique link. Your Post object may look slightly different, so make sure you make appropriate changes as necessary to this template. I also make the post description the actual content of the post since I like full-text feeds.

    

The Auto-Discovery Tag

    

The last bit to finish off your brand new spankin’ RSS feed is the auto-discovery tag.

  
<%= auto_discovery_link_tag(:rss, :action => 'feed') %>
    

Place it within the element of your site’s layout (if you use one), that way your visitors using a browser such as Safari or Firefox will be able to easily see that your site has an RSS feed.

    

Final Notes

    

So that’s it for generating an RSS feed in Rails. Fairly easy, huh? The only other thing to really note is that you are free to define how you want to get objects out of your database to add to the feed. Posts only relating to food, just pictures, or whatever. Just expand that :conditions symbol for the find method as needed.

Tags: rails,rss,xml

2008-07-23 20:15:24, 1330 reviews, comment

send to mailbox

Your email:

14
Jul

【转载】CSRF攻击:不容忽视

CSRF这种攻击方式虽然提出来很久(在2006年的时候就有了)了,但是这个沉睡的攻击巨人直到前不久才逐渐走入我们的视线,到底CSRF是啥,危害到底有多大?常见的利用方式是如何的,今天作为“安全相关 | Security”分类的第一篇文章,我按照自己的理解告诉你,不要低估了CSRF危害性和攻击能力。

一、什么是CSRF

先看看CSRF的原文说明,如下:
Cross Site Reference Forgery works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.
二、CSRF案例说明和分析
自 然,这里拿Rails程序来举例子说明这些问题,大家知道Rails2之前是把session放在服务器端(文件或者DB或者缓存中),客户但在 cookie中保存sessionid;而到了Rails2后,还有一种方式是把session放在基于cookie的客户端中。当然这两样都各有道理, 各有优劣,不在我们这次说的范围之内。我们继续说,当我们向一个域名发送一个请求的时候,如果本店存在这个域名的cookie,浏览器会自动的把 cookie附带上。这样本来没有啥问题,也是我们为了解决http无状态记录的解决方案,但是有个问题出现了,如果出现一个到其他域名的请求,浏览区在 加载的时候,也把cookie给带上了,会有什么问题?我们举个简单的也很常见的例子来说明这个问题。
-----------------------------案例------------------------------
1、Bob在自己的电脑上刚刚查看完自己的银行A账户余额,然后比较无聊就跑到一个公开的BBS上灌水,当他看到一篇“银行A的内部照片”的帖子,很有兴趣的打开这个帖子想看看自己信任的银行A的内部图片是啥样子的,殊不知,这其实是一个attacker精心设计的骗局。
2、在这个帖子中确实有几个图片,看上去真的像是银行A的照片,但是其中有个图片没显示出来,Bob以为是自己网速太慢,导致这个图片没有加载进来,也没在意。只是对这些并不是十分满意的照片摇摇头,就关了这个帖子。
3、几天后,Bob猛然发现自己在银行A的账户上少了1000元,到底是怎么了?

分析:

为什么钱少了呢?我们得分析一下上面这个案例,好记得当时Bob说有个图片没显示么,是的,我们来看看这个图片的地址,惊奇的发现是:<img .src="http://www.banka.com/transfer?account=myself&amount=1000&destination=attacker">,这是一个什么地址?聪明的您一定很快就能明白,这个地址是邪恶的,看上去,他的意思是打开这个地址的人,给attacker转了1000元。
这怎么可能?你肯定急了,我怎么能随便给一个人转1000元呢,而且我都不知道呀!但是,注意了,这其实是完全有可能的。还记得当时Bob刚刚查看完整及的帐号信息,基于银行A的cookie并不过期,当出现如上链接出现在src的时候(
note that .src is meant to be src),浏览器尝试着按照本地的cookie去加载上面这个URL,而银行A验证了来源请求的cookie是可以的,所以就这样事情就悄悄的发生了。
-------------------------------案例结束---------------------------

ok, 看明白了么,这就是CSRF,一句话给他下个定义就是:借你的cookie在你不知道的时候悄悄的做了一些你不愿意做 的事情。恶日期这里有个更要命的是,这个包含上述URL的图片或者链接,并不需要一定是放在银行A的服务器上,相反可以在任一地方,比如blog,公开的 BBS,或者一些群发的Mail中等等,如此多的场合下,这些都有可能存在陷阱。
再看一副图片吧,其说明的正是CSRF的攻击原理。


三、CSRF的预防
看上去很恐怖吧,是的,确实恐怖,意识到恐怖是个好事情,这样会促使你接着往下看如何改进和防止类似的漏洞出现。
总体来说,预防CSRF主要从2个方面入手,分别是:
1、正确使用GET,POST和Cookie;
2、在non-GET请求中使用Security token;

一般上,大家知道的浏览器发送请求的方式有GET或者POST,但是还有一种比较常用的是Cookie,至于其他的HTTP协议请求方式,你可以google,一般按照W3C的规范:
1、GET常用在查看,列举,展示的时候;
2、POST常用在下达订单,改变一个资源的属性或者做其他一些事情
;

ok,我们这里拿Rails按照前面列举的2种预防手段做说明,首先,我们可以在Rails的控制权中(controller)将一些方法(action)限定(verify)为只能使用POST或者GET,例如:
Ruby代码
  1. verify :method => :post, :only => [ :transfer ], :redirect_to => { :action => :list }   
恩,很好,这样做下限制以后,前面案例中的方法就失效了,因为这里我们限定了transfer必须使用POST来提交请求,当GET请求来的时候并不会被响应。
万事大吉了?NO!因为POST的请求也是可以被构造出来后自动发送的,如何实现,看下面吧,你肯定会吃惊的。
XML/HTML代码
  1. <a .href="http://www.1sters.com/" onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = 'http://www.example.com/account/destroy'; f.submit();return false;">点我试试</a>  
是的,这就是一个活生生的例子(.href is meant to be href),使用link的href或者img的src都可以的,再想想一个Attacher放了一个图片,然后写了一个
onmouseover方法,执行上述的那段JS,如下,或者使用AJAX。
XML/HTML代码
  1. <img .src="http://www.harmless.com/img" width="400" height="400" onmouseover="…" />  
所以,限定为POST后还不是非常的保险,怎么办?不急,我们还有第二步,给non-GET的请求设置security token,如何实现,在Rails2以后非常简单(也是默认的),我们只需要在environment.rb中添加如下代码:
Ruby代码
  1. config.action_controller.session = {  
  2.   :session_key => '_csrf_session',  
  3.   :secret      => 'ae4b43dda38ff78bb50898b2935da76d1e224061ab72a9399d34cea4c6178eee6dae815fff920a20642f27abda83b793da4e9b6cf20c4838805e80abf53e318a'  
  4. }  
然后
在application controller中包含如下security token设置:
Ruby代码
  1. protect_from_forgery  :secret => '053cef294a333f72c3584311799c69d2' 
ok,基本上安全了,如果这时POST请求过去,但是
security token和session计算出来的secret和服务端的secret匹配不上的话,就会返回一个ActionController::InvalidAuthenticityToken错误,防止该类缺陷的出现。
安全了,也许你要说,那我如果能破解出
protect_from_forgery,不进OK了么,按照理论上是,但是实际是破解是基本上不可能的,因为有人曾计算过,暴力破解该串大概需要2的11次方时间,后续我将再写一篇文章详细的介绍,这里不再赘述。

四、总结
总的来说,CSRF的兴起刚刚开始,网络上肯定会有一股热潮,Railser们一定要注意自己的程序的安全性,CSRF比你我能想到的更有威胁,千万别小瞧了它。

Tags: rails,csrf,安全

2008-07-14 18:19:55, 730 reviews, comment

send to mailbox

Your email:

29
May

插件auto_complete的使用

因为项目需要,找个下rails上的ajax自动填充表单的插件,在agilewebdevelopment上找到了auto_complete
上面说这个插件是官方的插件,应该不错
先用ruby script/plugin install auto_complete安装
然后就是使用了

  1.首先在一Controller中加入
    auto_complete_for :report, :title
    :report是对应的model名, title是字段名
  2.在控制器中定义一action
     def auto_complete_for_report_title
      search = params[:report][:title]
      @articles = Report.find(:all, :conditions => ["title like ?", "%#{search}%"], :limit => 10, :order => "id DESC") unless search.blank?
      render :partial => "live/search"
     end

   action的命名要以auto_complete_for_开始,然后是对应上面定义的auto_complete_for :report, :title
  3.然后在view中调用就行了,记得先调用rails的default js文件
    <% form_tag '/welcome/auto_complete_for_report_title' do %>
      <%= text_field_with_auto_complete :report, :title, { :size => 35, :value => '' } %>
    <% end %>

    其中value是指默认文本框的value值为空,因为我在测试时发现每次代开页面时文本框的value值会自动与当前页面模型的title进行绑定显示,这样显然不行的,所以就加了这条属性,让它每次都自动为空
   4.生成的模板文件会自动创建,在views/live/_search.rhtml中,根据自己的需要定制就行
   5.然后打开页面测试时报错,说是表单认证错误,后来google了下才知道,rails2.0后为了防止外部调用表单,进行数据库的操作,加了此项安全措施,只用在application.rb中加入
   protect_from_forgery :only => [:create, :update, :destroy]
   就行了
   6.样式可以自定义,打开页面后,查看下源代码,就知道定义那些属性了,可以覆盖的

Tags: rails,plugin,auto complete

2008-05-29 01:08:45, 1211 reviews, comment

send to mailbox

Your email:

Tags

json Impactjs Canvas fun gen_server superfly PS3 Webgame HTML5 jquery SSH tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • HTML5[3]
  • iPhone[17]
  • Erlang[11]
  • google[13]
  • 生活[45]
  • 音乐[13]
  • 电影[11]
  • linux[29]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[9]
  • ruby[20]
  • ruby on rails[28]
  • 开源[4]

Episode

  • MongoDB
  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache


Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Ruby遍历MemCached的key
  • 服务器监控小脚本
  • MySQL Innodb备份
  • [转载]5个有用的ruby gems

Recommended Posts

  • Tri-survive - HTML5 Game
  • Cut the rope - HTML5版
  • json_formatter
  • 在gen_server中spawn新的进程
  • Superfly - Wildflowers
  • 继续凸墙 for Mac OS
  • MongoDB Beijing 2011
  • MongoDB删除map_reduce生成的tmp collection
  • Mai Kuraki - Future Kiss
  • Erlang OOP
  • 用SSH tunnel凸墙
  • Google的语法高亮工具包
  • 大量数据的批量操作
  • 再次被和谐-_-
  • 十人族: 上线了

Friends' blogs

  • levy
  • sphance
  • andreas
  • yangkunlun
  • {:dev=>:wxianfeng}
  • bheye
  • joeydarko

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 316253
  • 今天访问: 48
  • 日志: 213
  • 评论: 4967
  • 音乐: 9
  • 用户: 1758


少女,不点下广告吗!

 

all by shitou

blog comments powered by Disqus

close