2016-10-19 5 views
1

送信されたデータを指定された構造体にマーシャリングする直前に入力をサニタイズしようとしています。golangの入力データのサニタイズ方法は?

ここに私が使用しているモデルがあります。ここで

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"` 
} 

どのように私は最も効果的にこのようにいずれかを削除し、このプロセスの間にReadJSON要求時に、データ挿入前に上記のJSON形式のデータをサニタイズう

{"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"} 

JSONデータを提出した可能性の一例です<script>alert()</script>.で見られるような悪質なコード? 使用可能な追加情報がある場合は質問してください。追加することができれば幸いです。 ありがとう

+4

あなたはおそらくこれを読んでください.florinpatan.ro/2016/10/why-you-should-not-use-iris-for-your-go.html)。私はあなたが切り替える必要があると言っているわけではありませんが、あなたは少なくとも、コミュニティの多くがアイリスについて持っている懸念を知っておくべきです。 – joshlf

+2

実際にGoでフレームワークを使用する理由はありません。それは私が最も気に入っているものです。 –

+0

少なくとも、私は今より後ではなく知っていました。 –

答えて

3

HTMLサニタイズの場合は、github.com/microcosm-cc/bluemondayを試してみてください。

設定したルールに従ってJSON入力データの検証を行います。

これはarticleです。

記事の例。

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

検証に使用されるパッケージはgopkg.in/validator.v2

使い方です:[なぜあなたは行くのプロジェクトのための虹彩を使うべきではありません](HTTP:// WWW

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 
} 
+1

答えをありがとうございます!私は実際には危険な入力がデータベースに挿入されないようにしたいと思っていたので、挿入されたパラメータを検証プロセス以上にきれいにする必要がありました。答えにもう一度感謝します! –

関連する問題