与Golang的net / http标头一起使用

时间:2014-07-31 06:37:32

标签: curl jenkins go csrf csrf-protection

我正在添加crumb CSRF保护支持,作为我为与Jenkins RESTful API(https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API)进行交互而编写的Golang代码的一部分

结构:

type Crumb struct {
  Crumb               string      `json:"crumb"`
  CrumbRequestField   string      `json:"crumbRequestField"`
}

  ...
  crb := Crumb{}
  // did some work to jsonify the crumb to Golang struct
  // https://jenkins.mydomain.com/crumbIssuer/api/json
  ...


  if (crb.Crumb != "" && crb.CrumbRequestField != "" ) {
    req.Header.Set(crb.CrumbRequestField, crb.Crumb)
  }

  req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)

  // i think the issue is related to DefaultClient, but not sure
  return http.DefaultClient.Do(req) 

但是上面的Golang实现继续获得403,但同样的事情可以正常使用cURL。

&{403 No valid crumb was included in the request 403 HTTP/1.1 1 1 map[Server:[nginx] Date:[Thu, 31 Jul 2014 05:58:52 GMT] Content-Type:[text/html;charset=ISO-8859-1] Connection:[keep-alive] Cache-Control:[must-revalidate,no-cache,no-store]] 0xc20800ff80 -1 [chunked] false map[] 0xc2082ba270 0xc208005da0}

cURL日志:

> POST /computer/node1/toggleOffline HTTP/1.1
> Authorization: Basic <key>
> User-Agent: curl/7.30.0
> Host: jenkins.mydomain.com
> Accept: */*
> .crumb: 0d6401898751f250ff1f95b5bf9589db

1 个答案:

答案 0 :(得分:1)

我在这里回答了我自己的问题。我发现了我的问题,它与Golang的net / http lib没有任何关系。 Jenkins crumb GET请求要求您提供基本身份验证以获得正确的碎屑。