Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit cccc964

Browse files
committed
Fix get policy failure
1 parent 716989a commit cccc964

3 files changed

Lines changed: 22 additions & 46 deletions

File tree

cmd/github-s3-auto/go.work

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
go 1.21.0
1+
go 1.23.0
22

33
use (
44
.

cmd/github-s3-auto/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"github.com/j178/kooky"
55

6+
_ "github.com/j178/kooky/browser/chrome"
7+
68
githubs3 "github.com/j178/github-s3"
79
)
810

github.go

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/url"
99
"os"
1010
"path/filepath"
11-
"regexp"
1211
"strconv"
1312

1413
"github.com/go-resty/resty/v2"
@@ -50,30 +49,13 @@ func New(userSession string, repo string) *GitHub {
5049
c.SetDebug(os.Getenv("DEBUG") == "1")
5150
c.SetRedirectPolicy(resty.NoRedirectPolicy())
5251
c.SetContentLength(true)
53-
c.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36")
52+
c.SetHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
53+
c.SetHeader("X-Requested-With", "XMLHttpRequest")
5454
g.c = c
5555

5656
return g
5757
}
5858

59-
var tokenPattern = regexp.MustCompile(`<file-attachment class="js-upload-markdown-image.*?<input type="hidden" value="([^{"]+?)" data-csrf="true"`)
60-
61-
func (g *GitHub) fetchAuthenticityToken() (string, error) {
62-
resp, err := g.c.R().Get(fmt.Sprintf("https://github.com/%s/issues/new", g.repo))
63-
if err != nil {
64-
return "", err
65-
}
66-
if !resp.IsSuccess() {
67-
return "", fmt.Errorf("failed to fetch authenticity token: %s", resp.Status())
68-
}
69-
body := resp.String()
70-
matches := tokenPattern.FindStringSubmatch(body)
71-
if len(matches) != 2 {
72-
return "", fmt.Errorf("authenticity token not found")
73-
}
74-
return matches[1], nil
75-
}
76-
7759
func (g *GitHub) getRepoId() (int, error) {
7860
var result struct {
7961
ID int `json:"id"`
@@ -88,7 +70,7 @@ func (g *GitHub) getRepoId() (int, error) {
8870
return result.ID, nil
8971
}
9072

91-
type preUploadResult struct {
73+
type uploadPolicy struct {
9274
UploadUrl string `json:"upload_url"`
9375
UploadAuthenticityToken string `json:"upload_authenticity_token"`
9476
AssetUploadUrl string `json:"asset_upload_url"`
@@ -106,14 +88,7 @@ type preUploadResult struct {
10688
SameOrigin bool `json:"same_origin"`
10789
}
10890

109-
func (g *GitHub) preUpload(name string, size int, contentType string) (*preUploadResult, error) {
110-
if g.authenticityToken == "" {
111-
token, err := g.fetchAuthenticityToken()
112-
if err != nil {
113-
return nil, err
114-
}
115-
g.authenticityToken = token
116-
}
91+
func (g *GitHub) getPolicy(name string, size int, contentType string) (*uploadPolicy, error) {
11792
if g.repoId == 0 {
11893
repoId, err := g.getRepoId()
11994
if err != nil {
@@ -122,36 +97,36 @@ func (g *GitHub) preUpload(name string, size int, contentType string) (*preUploa
12297
g.repoId = repoId
12398
}
12499

125-
var result preUploadResult
100+
var result uploadPolicy
126101
resp, err := g.c.R().
127102
SetMultipartFormData(map[string]string{
128-
"authenticity_token": g.authenticityToken,
129-
"repository_id": strconv.Itoa(g.repoId),
130-
"name": name,
131-
"size": strconv.Itoa(size),
132-
"content_type": contentType,
103+
"repository_id": strconv.Itoa(g.repoId),
104+
"name": name,
105+
"size": strconv.Itoa(size),
106+
"content_type": contentType,
133107
}).
108+
SetHeader("Github-Verified-Fetch", "true").
109+
SetHeader("Origin", "https://github.com").
134110
SetResult(&result).
135111
Post("https://github.com/upload/policies/assets")
136112
if err != nil {
137113
return nil, err
138114
}
139115
if !resp.IsSuccess() {
140-
return nil, fmt.Errorf("failed to pre-upload: %s\n%s", resp.Status(), resp.String())
116+
return nil, fmt.Errorf("failed to get policy: %s\n%s", resp.Status(), resp.String())
141117
}
142118
return &result, nil
143119
}
144120

145-
func (g *GitHub) postUpload(result *preUploadResult) error {
121+
func (g *GitHub) markUploadComplete(result *uploadPolicy) error {
146122
resp, err := g.c.R().
147-
SetHeader("X-Requested-With", "XMLHttpRequest").
148123
SetMultipartFormData(map[string]string{"authenticity_token": result.AssetUploadAuthenticityToken}).
149124
Put("https://github.com" + result.AssetUploadUrl)
150125
if err != nil {
151126
return err
152127
}
153128
if !resp.IsSuccess() {
154-
return fmt.Errorf("failed to post upload: %s", resp.Status())
129+
return fmt.Errorf("failed to mark upload complete: %s", resp.Status())
155130
}
156131
return nil
157132
}
@@ -172,16 +147,15 @@ func (g *GitHub) Upload(name string, size int, r io.Reader) (UploadResult, error
172147
} else {
173148
contentType = mime.TypeByExtension(ext)
174149
}
175-
result, err := g.preUpload(name, size, contentType)
150+
policy, err := g.getPolicy(name, size, contentType)
176151
if err != nil {
177152
return UploadResult{}, err
178153
}
179154

180155
resp, err := g.c.R().
181-
SetHeader("X-Requested-With", "XMLHttpRequest").
182-
SetFormData(result.Form).
156+
SetFormData(policy.Form).
183157
SetFileReader("file", name, r).
184-
Post(result.UploadUrl)
158+
Post(policy.UploadUrl)
185159
if err != nil {
186160
return UploadResult{}, err
187161
}
@@ -190,13 +164,13 @@ func (g *GitHub) Upload(name string, size int, r io.Reader) (UploadResult, error
190164
}
191165
loc := resp.Header().Get("Location")
192166

193-
err = g.postUpload(result)
167+
err = g.markUploadComplete(policy)
194168
if err != nil {
195169
return UploadResult{}, err
196170
}
197171

198172
return UploadResult{
199-
GithubLink: result.Asset.Href,
173+
GithubLink: policy.Asset.Href,
200174
AwsLink: loc,
201175
}, nil
202176
}

0 commit comments

Comments
 (0)