Skip to content

Commit

Permalink
Filename is random by default
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrafrancyz committed May 9, 2022
1 parent c21450c commit aa9e861
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 217 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ maxexpiry = 86400
| ```xframeoptions = "..." ``` | X-Frame-Options header (default is "SAMEORIGIN")
| ```remoteuploads = true``` | (optionally) enable remote uploads (/upload?url=https://...)
| ```nologs = true``` | (optionally) disable request logs in stdout
| ```force-random-filename = true``` | (optionally) force the use of random filenames
| ```custompagespath = custom_pages/``` | (optionally) specify path to directory containing markdown pages (must end in .md) that will be added to the site navigation (this can be useful for providing contact/support information and so on). For example, custom_pages/My_Page.md will become My Page in the site navigation


Expand Down
1 change: 0 additions & 1 deletion display.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request, fileNam
"expiry": expiryHuman,
"expirylist": listExpirationTimes(),
"extra": extra,
"forcerandom": Config.forceRandomFilename,
"lines": lines,
"files": metadata.ArchiveFiles,
"siteurl": strings.TrimSuffix(getSiteURL(r), "/"),
Expand Down
1 change: 0 additions & 1 deletion linx-server.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ maxexpiry = 86400
allowhotlink = true
remoteuploads = true
nologs = true
force-random-filename = false
cleanup-every-minutes = 5
18 changes: 7 additions & 11 deletions pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ const (

func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err := renderTemplate(Templates["index.html"], pongo2.Context{
"maxsize": Config.maxSize,
"expirylist": listExpirationTimes(),
"forcerandom": Config.forceRandomFilename,
"maxsize": Config.maxSize,
"expirylist": listExpirationTimes(),
}, r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -32,8 +31,7 @@ func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {

func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err := renderTemplate(Templates["paste.html"], pongo2.Context{
"expirylist": listExpirationTimes(),
"forcerandom": Config.forceRandomFilename,
"expirylist": listExpirationTimes(),
}, r, w)
if err != nil {
oopsHandler(c, w, r, RespHTML, "")
Expand All @@ -43,7 +41,6 @@ func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err := renderTemplate(Templates["API.html"], pongo2.Context{
"siteurl": getSiteURL(r),
"forcerandom": Config.forceRandomFilename,
"keyless_delete": Config.anyoneCanDelete,
}, r, w)
if err != nil {
Expand All @@ -54,11 +51,10 @@ func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) {
func makeCustomPageHandler(fileName string) func(c web.C, w http.ResponseWriter, r *http.Request) {
return func(c web.C, w http.ResponseWriter, r *http.Request) {
err := renderTemplate(Templates["custom_page.html"], pongo2.Context{
"siteurl": getSiteURL(r),
"forcerandom": Config.forceRandomFilename,
"contents": customPages[fileName],
"filename": fileName,
"pagename": customPagesNames[fileName],
"siteurl": getSiteURL(r),
"contents": customPages[fileName],
"filename": fileName,
"pagename": customPagesNames[fileName],
}, r, w)
if err != nil {
oopsHandler(c, w, r, RespHTML, "")
Expand Down
3 changes: 0 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ var Config struct {
s3Region string
s3Bucket string
s3ForcePathStyle bool
forceRandomFilename bool
anyoneCanDelete bool
accessKeyCookieExpiry uint64
customPagesDir string
Expand Down Expand Up @@ -312,8 +311,6 @@ func main() {
"S3 bucket to use for files and metadata")
flag.BoolVar(&Config.s3ForcePathStyle, "s3-force-path-style", false,
"Force path-style addressing for S3 (e.g. https://s3.amazonaws.com/linx/example.txt)")
flag.BoolVar(&Config.forceRandomFilename, "force-random-filename", false,
"Force all uploads to use a random filename")
flag.BoolVar(&Config.anyoneCanDelete, "anyone-can-delete", false,
"Anyone has delete button on the file page")
flag.Uint64Var(&Config.accessKeyCookieExpiry, "access-cookie-expiry", 0, "Expiration time for access key cookies in seconds (set 0 to use session cookies)")
Expand Down
132 changes: 0 additions & 132 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,58 +575,6 @@ func TestPostExpiresJSONUpload(t *testing.T) {
}
}

func TestPostRandomizeJSONUpload(t *testing.T) {
mux := setup()
w := httptest.NewRecorder()

filename := generateBarename() + ".txt"

var b bytes.Buffer
mw := multipart.NewWriter(&b)
fw, err := mw.CreateFormFile("file", filename)
if err != nil {
t.Fatal(err)
}
fw.Write([]byte("File content"))

rnd, err := mw.CreateFormField("randomize")
if err != nil {
t.Fatal(err)
}
rnd.Write([]byte("true"))

mw.Close()

req, err := http.NewRequest("POST", "/upload/", &b)
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("Accept", "application/json")
req.Header.Set("Referer", Config.siteURL)
if err != nil {
t.Fatal(err)
}

mux.ServeHTTP(w, req)

if w.Code != 200 {
t.Log(w.Body.String())
t.Fatalf("Status code is not 200, but %d", w.Code)
}

var myjson RespOkJSON
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
if err != nil {
t.Fatal(err)
}

if myjson.Original_Name == filename {
t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Original_Name)
}

if myjson.Size != "12" {
t.Fatalf("File size was not 12 but %s", myjson.Size)
}
}

func TestPostEmptyUpload(t *testing.T) {
mux := setup()
w := httptest.NewRecorder()
Expand Down Expand Up @@ -753,52 +701,6 @@ func TestPutUpload(t *testing.T) {
}
}

func TestPutRandomizedUpload(t *testing.T) {
mux := setup()
w := httptest.NewRecorder()

filename := generateBarename() + ".file"

req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
if err != nil {
t.Fatal(err)
}

req.Header.Set("Linx-Randomize", "yes")

mux.ServeHTTP(w, req)

if w.Body.String() == Config.siteURL+filename {
t.Fatal("Filename was not random")
}
}

func TestPutForceRandomUpload(t *testing.T) {
mux := setup()
w := httptest.NewRecorder()

oldFRF := Config.forceRandomFilename
Config.forceRandomFilename = true
filename := "randomizeme.file"

req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
if err != nil {
t.Fatal(err)
}

// while this should also work without this header, let's try to force
// the randomized filename off to be sure
req.Header.Set("Linx-Randomize", "no")

mux.ServeHTTP(w, req)

if w.Body.String() == Config.siteURL+filename {
t.Fatal("Filename was not random")
}

Config.forceRandomFilename = oldFRF
}

func TestPutNoExtensionUpload(t *testing.T) {
mux := setup()
w := httptest.NewRecorder()
Expand All @@ -810,8 +712,6 @@ func TestPutNoExtensionUpload(t *testing.T) {
t.Fatal(err)
}

req.Header.Set("Linx-Randomize", "yes")

mux.ServeHTTP(w, req)

if w.Body.String() == Config.siteURL+filename {
Expand All @@ -830,8 +730,6 @@ func TestPutEmptyUpload(t *testing.T) {
t.Fatal(err)
}

req.Header.Set("Linx-Randomize", "yes")

mux.ServeHTTP(w, req)

if w.Code != 400 {
Expand All @@ -853,8 +751,6 @@ func TestPutTooLargeUpload(t *testing.T) {
t.Fatal(err)
}

req.Header.Set("Linx-Randomize", "yes")

mux.ServeHTTP(w, req)

if w.Code != 500 {
Expand Down Expand Up @@ -896,34 +792,6 @@ func TestPutJSONUpload(t *testing.T) {
}
}

func TestPutRandomizedJSONUpload(t *testing.T) {
var myjson RespOkJSON

mux := setup()
w := httptest.NewRecorder()

filename := generateBarename() + ".file"

req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
if err != nil {
t.Fatal(err)
}

req.Header.Set("Accept", "application/json")
req.Header.Set("Linx-Randomize", "yes")

mux.ServeHTTP(w, req)

err = json.Unmarshal([]byte(w.Body.String()), &myjson)
if err != nil {
t.Fatal(err)
}

if myjson.Original_Name == filename {
t.Fatal("Filename was not random ")
}
}

func TestPutExpireJSONUpload(t *testing.T) {
var myjson RespOkJSON

Expand Down
6 changes: 0 additions & 6 deletions static/css/linx.css
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,7 @@ body {
padding-top: 1px;
}

#randomize {
vertical-align: bottom;
margin: 0;
}

#access_key {
min-width: 100%;
line-height: 1.3em;
}

Expand Down
4 changes: 0 additions & 4 deletions static/js/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ Dropzone.options.dropzone = {
file.uploadElement.setAttribute("style", 'background-image: -webkit-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -moz-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -ms-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -o-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%)');
},
sending: function (file, xhr, formData) {
var randomize = document.getElementById("randomize");
if (randomize != null) {
formData.append("randomize", randomize.checked);
}
formData.append("expires", document.getElementById("expires").value);
},
success: function (file, resp) {
Expand Down
44 changes: 20 additions & 24 deletions templates/API.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ <h3>Uploading a file</h3>

<p><strong>Optional headers with the request</strong></p>

{% if not forcerandom %}
<p>Randomize the filename<br />
<code>Linx-Randomize: yes</code></p>
{% endif %}

{% if !keyless_delete %}
<p>Specify a custom deletion key<br />
<code>Linx-Delete-Key: mysecret</code></p>
Expand Down Expand Up @@ -71,33 +66,33 @@ <h3>Uploading a file</h3>
<p>Uploading myphoto.jpg</p>

{% if auth != "none" %}
<pre><code>$ curl -H &#34;Linx-Api-Key: mysecretkey&#34; -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}{% if not forcerandom %}myphoto.jpg{% else %}7z4h4ut.jpg{% endif %}</code></pre>
<pre><code>$ curl -H "Linx-Api-Key: mysecretkey" -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}7z4h4ut01.jpg</code></pre>
{% else %}
<pre><code>$ curl -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}{% if not forcerandom %}myphoto.jpg{% else %}wtq7pan.jpg{% endif %}</code></pre>
<pre><code>$ curl -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}wtq7pan4o.jpg</code></pre>
{% endif %}

<p>Uploading myphoto.jpg with an expiry of 20 minutes</p>

{% if auth != "none" %}
<pre><code>$ curl -H &#34;Linx-Api-Key: mysecretkey&#34; -H &#34;Linx-Expiry: 1200&#34; -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}{% if not forcerandom %}myphoto.jpg{% else %}jm295snf.jpg{% endif %}</code></pre>
<pre><code>$ curl -H "Linx-Api-Key: mysecretkey" -H "Linx-Expiry: 1200" -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}jm295snf4h.jpg</code></pre>
{% else %}
<pre><code>$ curl -H &#34;Linx-Expiry: 1200&#34; -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}{% if not forcerandom %}myphoto.jpg{% else %}1doym9u2.jpg{% endif %}</code></pre>
<pre><code>$ curl -H "Linx-Expiry: 1200" -T myphoto.jpg {{ siteurl }}upload/
{{ siteurl }}1d7oym9u2o.jpg</code></pre>
{% endif %}

<p>Uploading myphoto.jpg with a random filename and getting a json response:</p>
<p>Uploading myphoto.jpg and getting a json response:</p>

{% if auth != "none" %}
<pre><code>$ curl -H &#34;Linx-Api-Key: mysecretkey&#34; -H &#34;Accept: application/json&#34;{% if not forcerandom %} -H &#34;Linx-Randomize: yes&#34;{% endif %} -T myphoto.jpg {{ siteurl }}upload/
{&#34;delete_key&#34;:&#34;...&#34;,&#34;expiry&#34;:&#34;0&#34;,&#34;filename&#34;:&#34;f34h4iu.jpg&#34;,&#34;mimetype&#34;:&#34;image/jpeg&#34;,
&#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;,&#34;url&#34;:&#34;{{ siteurl }}f34h4iu.jpg&#34;}</code></pre>
<pre><code>$ curl -H "Linx-Api-Key: mysecretkey" -H "Accept: application/json" -T myphoto.jpg {{ siteurl }}upload/
{"delete_key":"...","expiry":"0","filename":"f34h4iuj7.jpg","mimetype":"image/jpeg",
"sha256sum":"...","size":"...","url":"{{ siteurl }}f34h4iuj7.jpg","original_name":"myphoto.jpg"}</code></pre>
{% else %}
<pre><code>$ curl -H &#34;Accept: application/json&#34;{% if not forcerandom %} -H &#34;Linx-Randomize: yes&#34;{% endif %} -T myphoto.jpg {{ siteurl }}upload/
{&#34;delete_key&#34;:&#34;...&#34;,&#34;expiry&#34;:&#34;0&#34;,&#34;filename&#34;:&#34;f34h4iu.jpg&#34;,&#34;mimetype&#34;:&#34;image/jpeg&#34;,
&#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;,&#34;url&#34;:&#34;{{ siteurl }}f34h4iu.jpg&#34;}</code></pre>
<pre><code>$ curl -H "Accept: application/json" -T myphoto.jpg {{ siteurl }}upload/
{"delete_key":"...","expiry":"0","filename":"f34h4iuj7.jpg","mimetype":"image/jpeg",
"sha256sum":"...","size":"...","url":"{{ siteurl }}f34h4iuj7.jpg","original_name":"myphoto.jpg"}</code></pre>
{% endif %}

<h3>Deleting a file</h3>
Expand All @@ -107,9 +102,9 @@ <h3>Deleting a file</h3>

<p><strong>Example</strong></p>

<p>To delete myphoto.jpg</p>
<p>To delete f34h4iuj7.jpg</p>

<pre><code>$ curl{% if auth != "none" %} -H&#34;Linx-Api-Key: mysecretkey&#34;{% endif %}{% if !keyless_delete %} -H &#34;Linx-Delete-Key: mysecret&#34;{% endif %} -X DELETE {{ siteurl }}myphoto.jpg
<pre><code>$ curl{% if auth != "none" %} -H"Linx-Api-Key: mysecretkey"{% endif %}{% if !keyless_delete %} -H "Linx-Delete-Key: mysecret"{% endif %} -X DELETE {{ siteurl }}f34h4iuj7.jpg
DELETED</code></pre>

<h3>Information about a file</h3>
Expand All @@ -130,8 +125,9 @@ <h3>Information about a file</h3>

<p><strong>Example</strong></p>

<pre><code>$ curl -H &#34;Accept: application/json&#34; {{ siteurl }}myphoto.jpg
{&#34;expiry&#34;:&#34;0&#34;,&#34;filename&#34;:&#34;myphoto.jpg&#34;,&#34;mimetype&#34;:&#34;image/jpeg&#34;,&#34;sha256sum&#34;:&#34;...&#34;,&#34;size&#34;:&#34;...&#34;}</code></pre>
<pre><code>$ curl -H "Accept: application/json" {{ siteurl }}f34h4iuj7.jpg
{"expiry":"0","filename":"f34h4iuj7.jpg","mimetype":"image/jpeg","sha256sum":"...","size":"...",
"original_name":"myphoto.jpg"}</code></pre>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>{% block title %}{{ sitename }}{% endblock %}</title>
<meta charset='utf-8' content='text/html' http-equiv='content-type'>
<meta name='viewport' content='width=device-width, initial-scale=0.8'>
<link href='{{ sitepath }}static/css/linx.css?v=2' media='screen, projection' rel='stylesheet' type='text/css'>
<link href='{{ sitepath }}static/css/linx.css?v=3' media='screen, projection' rel='stylesheet' type='text/css'>
<link href='{{ sitepath }}static/css/hint.css' rel='stylesheet' type='text/css'>
<link href='{{ sitepath }}static/images/favicon.gif' rel='icon' type='image/gif'>
{% block head %}{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion templates/display/bin.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<form id="reply" action='{{ sitepath }}upload' method='post'>
<div class="info-flex">
<div>
{% if not forcerandom %}<input class="codebox" name='filename' id="filename" type='text' value="" placeholder="filename">{% endif %}.<input id="extension" class="codebox" name='extension' type='text' value="{{ extra.extension }}" placeholder="txt">
<input class="codebox" name='filename' id="filename" type='text' value="" placeholder="filename">.<input id="extension" class="codebox" name='extension' type='text' value="{{ extra.extension }}" placeholder="txt">
</div>
<div class="info-actions">
<select id="expiry" name="expires">
Expand Down
2 changes: 1 addition & 1 deletion templates/display/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<form id="reply" action='{{ sitepath }}upload' method='post'>
<div class="info-flex">
<div>
{% if not forcerandom %}<input class="codebox" name='filename' id="filename" type='text' value="" placeholder="filename">{% endif %}.<input id="extension" class="codebox" name='extension' type='text' value="story" placeholder="txt">
<input class="codebox" name='filename' id="filename" type='text' value="" placeholder="filename">.<input id="extension" class="codebox" name='extension' type='text' value="story" placeholder="txt">
</div>
<div class="info-actions">
<select id="expiry" name="expires">
Expand Down
Loading

0 comments on commit aa9e861

Please sign in to comment.