douhuangjian9627 2016-10-19 19:32
浏览 107

如何在golang中清理输入数据?

I am trying to sanitize input shortly before marshaling the submitted data into a specified struct.

Here is the model that I am using.

type Post struct {
    Id               int       `json:"Id"`
    CreatedAt        time.Time `json:"CreatedAt"`
    UpdatedAt        time.Time `json:"UpdatedAt"`
    CreatorId        int       `json:"CreatorId"`
    Creator          *User
    Editors          []int  `json:"Editors"`
    Status           Status `json:"Status"`
    Title            string `json:"Title"`
    ShortDescription string `json:"ShortDescription"`
    Description      string `json:"Description"`
    Content          string `json:"Content"`
    Url              string `json:"Url"`
    Media            *Media
    Categories       []Category `json:"Categories"`
    Tags             []Tag      `json:"Tags"`
    MediaId          int        `json:"MediaId"`
    Keywords         string     `json:"Keywords"`
    Data             []string   `json:"Data"`
}

Here is an example of a possible submitted JSON data

{"Id":1,"CreatedAt":"2016-10-11T21:29:46.134+02:00","UpdatedAt":"0001-01-01T00:00:00Z","CreatorId":1,"Editors":null,"Status":1,"Title":"This is the title of the first post, to be changed.<script>alert()</script>","ShortDescription":"this is the short description of this post","Description":"","Content":"Contrary to popular belief Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC making it over 2000 years old. Richard McClintock","Url":"lorem-ipsum-first"}

How would I most effectively sanitize the above JSON form data during the ReadJSON request and before data insertion during this process, thus removing any malicious code as seen with <script>alert()</script>.? If there is any additional information that could be of use please ask and I will be glad to add it. Thanks

  • 写回答

1条回答 默认 最新

  • dpcnm2132 2016-10-20 06:41
    关注

    For HTML Sanitizing you could try github.com/microcosm-cc/bluemonday.

    For validating JSON input data as per rules you set.

    This article is a good read on the topic.

    An example from the article.

    type User struct {
         Name string    `json:"name"    validate:"nonzero"`
         Age uint       `json:"age"     validate:"min=1"`
         Address string `json:"address" validate:"nonzero"`
    }
    

    The package used for validation is gopkg.in/validator.v2

    Usage :

    user := &models.User{}
    if err = c.ReadJSON(user); err != nil {
        // Handle Error
    }
    
    p := bluemonday.UGCPolicy()
    user.Name, user.Address = p.Sanitize(user.Name),p.Sanitize(user.Address)
    
    if err = validator.Validate(user); err != nil {
       // Handle Error
    }
    
    err = db.Create(&user)
    if err != nil {
        // Handle Error
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式