Skip to content

Commit

Permalink
Add support for custom upload html for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelreyna committed Apr 15, 2021
1 parent 94e87bc commit 8057395
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ oneshot [flags]... [file|dir|url]
Setting this flag will override the -u, --upload flag.
See also: -c, --cgi ; -s, --shell-command ; -S, --shell ; -R, --replace-headers ; -H, --header ; -E, --env ; --cgi-stderr
--custom-csrf-token string Use a custom CSRF token for uploads.
This flag does nothing if both the -u, --upload and --upload-input flags are not set.
This flag does nothing if none of the -u, --upload, --upload-input or --upload-file flags are set.
See also: -u, --upload; --upload-input; --no-csrf-token
-d, --dir string Working directory for the executable or when saving files.
Defaults to where oneshot was called.
Expand Down Expand Up @@ -210,7 +210,7 @@ oneshot [flags]... [file|dir|url]
depending on if a file was given.
See also: -u, --upload ; --upload-input ; --upload-file
--no-csrf-token Do not use a CSRF token for uploads.
This flag does nothing if both the -u, --upload and --upload-input flags are not set.
This flag does nothing if none of the -u, --upload, --upload-input or --upload-file flags are set.
See also: -u, --upload; --upload-input
-D, --no-download Don't trigger browser download client side.
If set, the "Content-Disposition" header used to trigger downloads in the clients browser won't be sent.
Expand Down Expand Up @@ -280,6 +280,12 @@ oneshot [flags]... [file|dir|url]
Setting both this flag and --upload-input is equivalent to setting the -u, --upload flag.
For more information see the -u, --upload flag documentation.
See also: --upload-input; -u, --upload
--upload-html string Path to html file to present to clients attempting to upload.
The file may be a Go HTML temlate. Two boolean values '.FileSection' and '.InputSection' and a string value ' .CSRFToken ' are made available to the template.
The boolean values ' .FileSection ' and ' .InputSection ' reflect the usage of the -u, --upload; --upload-input and --upload-file flags.
The string value ' .CSRFToken ' reflects the usage of the --no-csrf-token and --custom-csrf-token flags.
This flag does nothing if none of the -u, --upload, --upload-input or --upload-file flags are set.
See also: -u, --upload; --upload-input
--upload-input Receive text from a browser.
Setting both this flag and --upload-file is equivalent to setting the -u, --upload flag.
For more information see the -u, --upload flag documentation.
Expand Down
2 changes: 2 additions & 0 deletions cmd/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Conf struct {
NoCSRFToken bool
CustomCSRFToken string

UploadHTML string

cmdFlagSet *pflag.FlagSet

sstlsLoc string
Expand Down
12 changes: 10 additions & 2 deletions cmd/conf/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,20 @@ See also: -r, --redirect`,
)

flags.BoolVar(&c.NoCSRFToken, "no-csrf-token", false, `Do not use a CSRF token for uploads.
This flag does nothing if both the -u, --upload and --upload-input flags are not set.
This flag does nothing if none of the -u, --upload, --upload-input or --upload-file flags are set.
See also: -u, --upload; --upload-input`,
)

flags.StringVar(&c.CustomCSRFToken, "custom-csrf-token", "", `Use a custom CSRF token for uploads.
This flag does nothing if both the -u, --upload and --upload-input flags are not set.
This flag does nothing if none of the -u, --upload, --upload-input or --upload-file flags are set.
See also: -u, --upload; --upload-input; --no-csrf-token`,
)

flags.StringVar(&c.UploadHTML, "upload-html", "", `Path to html file to present to clients attempting to upload.
The file may be a Go HTML temlate. Two boolean values '.FileSection' and '.InputSection' and a string value ' .CSRFToken ' are made available to the template.
The boolean values ' .FileSection ' and ' .InputSection ' reflect the usage of the -u, --upload; --upload-input and --upload-file flags.
The string value ' .CSRFToken ' reflects the usage of the --no-csrf-token and --custom-csrf-token flags.
This flag does nothing if none of the -u, --upload, --upload-input or --upload-file flags are set.
See also: -u, --upload; --upload-input`,
)
}
37 changes: 24 additions & 13 deletions cmd/conf/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (c *Conf) setupUploadRoute(args []string, srvr *server.Server) (*server.Rou
<link rel="icon" type="image/png" href="/assets/icon.png">
</head>
<body>
poop
{{ if .FileSection }}
{{ template "file-section" .CSRFToken }}
{{ end }}
Expand All @@ -103,22 +102,34 @@ poop
inputSection := `{{ define "input-section" }}<form action="/" method="post">
<input type="hidden" name="csrf-token" value="{{ . }}">
<h5>Enter text to send: </h5>
<textarea name="oneshotTextUpload"></textarea>
<textarea name="text"></textarea>
<br><br>
<input type="submit" value="Upload">
</form>{{ end }}`

tmpl, err := template.New("file-section").Parse(fileSection)
if err != nil {
return nil, err
}
tmpl, err = tmpl.Parse(inputSection)
if err != nil {
return nil, err
}
tmpl, err = tmpl.Parse(base)
if err != nil {
return nil, err
var (
err error
tmpl *template.Template
)

if c.UploadHTML != "" {
tmpl, err = template.ParseFiles(c.UploadHTML)
if err != nil {
return nil, err
}
} else {
tmpl, err = template.New("file-section").Parse(fileSection)
if err != nil {
return nil, err
}
tmpl, err = tmpl.Parse(inputSection)
if err != nil {
return nil, err
}
tmpl, err = tmpl.Parse(base)
if err != nil {
return nil, err
}
}

sections := struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func HandleUpload(file *file.FileWriter, unixEOLNormalization bool, csrfToken st
return err
}

src = strings.NewReader(r.PostForm.Get("oneshotTextUpload"))
src = strings.NewReader(r.PostForm.Get("text"))
if unixEOLNormalization {
src = iohelper.NewBytesReplacingReader(src, crlf, lf)
}
Expand Down
13 changes: 11 additions & 2 deletions oneshot.1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ See also: \-c, \-\-cgi ; \-s, \-\-shell\-command ; \-S, \-\-shell ; \-R, \-\-rep
.PP
\fB\-\-custom\-csrf\-token\fP=""
Use a custom CSRF token for uploads.
This flag does nothing if both the \-u, \-\-upload and \-\-upload\-input flags are not set.
This flag does nothing if none of the \-u, \-\-upload, \-\-upload\-input or \-\-upload\-file flags are set.
See also: \-u, \-\-upload; \-\-upload\-input; \-\-no\-csrf\-token

.PP
Expand Down Expand Up @@ -128,7 +128,7 @@ See also: \-u, \-\-upload ; \-\-upload\-input ; \-\-upload\-file
.PP
\fB\-\-no\-csrf\-token\fP[=false]
Do not use a CSRF token for uploads.
This flag does nothing if both the \-u, \-\-upload and \-\-upload\-input flags are not set.
This flag does nothing if none of the \-u, \-\-upload, \-\-upload\-input or \-\-upload\-file flags are set.
See also: \-u, \-\-upload; \-\-upload\-input

.PP
Expand Down Expand Up @@ -255,6 +255,15 @@ Setting both this flag and \-\-upload\-input is equivalent to setting the \-u, \
For more information see the \-u, \-\-upload flag documentation.
See also: \-\-upload\-input; \-u, \-\-upload

.PP
\fB\-\-upload\-html\fP=""
Path to html file to present to clients attempting to upload.
The file may be a Go HTML temlate. Two boolean values '.FileSection' and '.InputSection' and a string value ' .CSRFToken ' are made available to the template.
The boolean values ' .FileSection ' and ' .InputSection ' reflect the usage of the \-u, \-\-upload; \-\-upload\-input and \-\-upload\-file flags.
The string value ' .CSRFToken ' reflects the usage of the \-\-no\-csrf\-token and \-\-custom\-csrf\-token flags.
This flag does nothing if none of the \-u, \-\-upload, \-\-upload\-input or \-\-upload\-file flags are set.
See also: \-u, \-\-upload; \-\-upload\-input

.PP
\fB\-\-upload\-input\fP[=false]
Receive text from a browser.
Expand Down

0 comments on commit 8057395

Please sign in to comment.