From ff49e21ecc13dead120b64e97887239b92d4f312 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Wed, 22 Mar 2017 08:09:09 -0600 Subject: [PATCH] Make sure is not encrypted before performing self destruct --- handlers.go | 36 ++++++++++++++++++++++++------------ page.go | 27 --------------------------- templates/index.html | 36 ++++++++++++++++++++++++++---------- utils.go | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 82 insertions(+), 51 deletions(-) diff --git a/handlers.go b/handlers.go index 75b1e6e..3fc7b3e 100755 --- a/handlers.go +++ b/handlers.go @@ -35,7 +35,7 @@ func handlePageRequest(c *gin.Context) { command := c.Param("command") version := c.DefaultQuery("version", "ajksldfjl") p := Open(page) - if p.IsPrimedForSelfDestruct && !p.IsLocked { + if p.IsPrimedForSelfDestruct && !p.IsLocked && !p.IsEncrypted { p.Update("*This page has now self-destructed.*\n\n" + p.Text.GetCurrent()) p.Erase() } @@ -52,7 +52,7 @@ func handlePageRequest(c *gin.Context) { versionText, err := p.Text.GetPreviousByTimestamp(int64(versionInt)) if err == nil { rawText = versionText - rawHTML = MarkdownToHtml(rawText) + rawHTML = GithubMarkdownToHTML(rawText) } } c.HTML(http.StatusOK, "index.html", gin.H{ @@ -105,9 +105,16 @@ func handlePrime(c *gin.Context) { } log.Trace("Update: %v", json) p := Open(json.Page) + if p.IsLocked { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"}) + return + } else if p.IsEncrypted { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"}) + return + } p.IsPrimedForSelfDestruct = true p.Save() - c.JSON(http.StatusOK, gin.H{"success": true}) + c.JSON(http.StatusOK, gin.H{"success": true, "message": "Primed"}) } func handleLock(c *gin.Context) { @@ -152,6 +159,7 @@ func handleEncrypt(c *gin.Context) { return } p := Open(json.Page) + q := Open(json.Page) var message string if p.IsEncrypted { decrypted, err2 := DecryptString(p.Text.GetCurrent(), json.Passphrase) @@ -159,20 +167,24 @@ func handleEncrypt(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"}) return } - p.Erase() - p = Open(json.Page) - p.Update(decrypted) - p.IsEncrypted = false + q.Erase() + q = Open(json.Page) + q.Update(decrypted) + q.IsEncrypted = false + q.IsLocked = p.IsLocked + q.IsPrimedForSelfDestruct = p.IsPrimedForSelfDestruct message = "Decrypted" } else { currentText := p.Text.GetCurrent() - p.Erase() - p = Open(json.Page) - p.IsEncrypted = true encrypted, _ := EncryptString(currentText, json.Passphrase) - p.Update(encrypted) + q.Erase() + q = Open(json.Page) + q.Update(encrypted) + q.IsEncrypted = true + q.IsLocked = p.IsLocked + q.IsPrimedForSelfDestruct = p.IsPrimedForSelfDestruct message = "Encrypted" } - p.Save() + q.Save() c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) } diff --git a/page.go b/page.go index 5b03655..37f16ce 100755 --- a/page.go +++ b/page.go @@ -1,14 +1,11 @@ package main import ( - "encoding/base32" "encoding/json" "io/ioutil" "os" "path" - "github.com/microcosm-cc/bluemonday" - "github.com/russross/blackfriday" "github.com/schollz/versionedtext" ) @@ -52,20 +49,6 @@ func (p *Page) Render() { p.RenderedPage = MarkdownToHtml(p.Text.GetCurrent()) } -func MarkdownToHtml(s string) string { - unsafe := blackfriday.MarkdownCommon([]byte(s)) - pClean := bluemonday.UGCPolicy() - pClean.AllowElements("img") - pClean.AllowAttrs("alt").OnElements("img") - pClean.AllowAttrs("src").OnElements("img") - pClean.AllowAttrs("class").OnElements("a") - pClean.AllowAttrs("href").OnElements("a") - pClean.AllowAttrs("id").OnElements("a") - pClean.AllowDataURIImages() - html := pClean.SanitizeBytes(unsafe) - return string(html) -} - func (p *Page) Save() error { bJSON, err := json.MarshalIndent(p, "", " ") if err != nil { @@ -77,13 +60,3 @@ func (p *Page) Save() error { func (p *Page) Erase() error { return os.Remove(path.Join(pathToData, encodeToBase32(p.Name)+".json")) } - -func encodeToBase32(s string) string { - return base32.StdEncoding.EncodeToString([]byte(s)) -} - -func decodeFromBase32(s string) (s2 string, err error) { - bString, err := base32.StdEncoding.DecodeString(s) - s2 = string(bString) - return -} diff --git a/templates/index.html b/templates/index.html index 2ea44a3..fa41112 100755 --- a/templates/index.html +++ b/templates/index.html @@ -9,6 +9,9 @@ + + +