2016-10-19 9 views
1

제출 된 데이터를 지정된 struct에 마샬링하기 직전에 입력을 위생적으로 처리하려고합니다.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"` 
} 

는 가능한 전송 JSON 데이터의 예이다 I 가장 효과적으로 따라서 임의의 제거,이 과정에서 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"} 

<script>alert()</script>.과 같은 악의적 인 코드? 사용할 수있는 추가 정보가있는 경우 요청하십시오. 추가 할 수있게되어 기쁩니다. 감사합니다.

+4

당신은 아마이 읽어야 : 당신이 당신의 이동 프로젝트를 위해 조리개를 사용하지 말아야하는 이유 (HTTP :

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

사용입니다 // www가 .florinpatan.ro/2016/10/why-you-should-not-use-iris-for-your-go.html). 나는 당신이 전환 할 필요가 없다는 말은 아니지만 적어도 지역 사회의 많은 사람들이 아이리스에 관해 갖고있는 우려 사항을 알고 있어야합니다. – joshlf

+2

실제로 Go에서 프레임 워크를 사용할 이유가 없습니다. Go에 관해서 가장 좋아하는 것 같습니다 Go –

+0

적어도 적어도 나중에는 알았습니다. –

답변

3

HTML 살균을 위해 github.com/microcosm-cc/bluemonday을 시도해 볼 수 있습니다.

설정 한 규칙에 따라 JSON 입력 데이터의 유효성을 검사합니다.

article은 주제에 대해 잘 설명되어 있습니다.

기사의 예.

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

답변 해 주셔서 감사합니다! 그리고 위대한 유효성 검사 문서, 나는 실제로 위험한 입력을 데이터베이스에 삽입 할 수 있도록 본질적으로 html 삽입을 처리하는 것처럼 보이는 유효성 검사 프로세스 이상으로 삽입 된 매개 변수를 제거 (위생)해야합니다. 답변을 주셔서 다시 한 번 감사드립니다! –

관련 문제