Skip to content

Commit ac6eff5

Browse files
authored
Merge pull request #4 from writeas/patch-filter
Prevent certain posts from being made public
2 parents d924964 + 7893f07 commit ac6eff5

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ DB_USER=dbuser DB_PASSWORD=pass DB_DB=htmlhouse PRIVATE_KEY=keys/dev PUBLIC_KEY=
5353
| `STATIC_DIR` | Relative dir where static files are stored | `static` |
5454
| `AUTO_APPROVE` | Automatically approves public posts | false |
5555
| `PREVIEWS_HOST` | Fully-qualified URL (without trailing slash) of screenshot server | None. |
56+
| `ADMIN_PASS` | Password to perform admin functions via API | `uhoh` |
57+
| `BROWSE_ITEMS` | Number of items to show on Browse page | 10 |
58+
| `BLACKLIST_TERMS` | Comma-separated list of terms to prevent a post from being made public | None. |
5659
| `TWITTER_KEY` | Twitter consumer key | `notreal` |
5760
| `TWITTER_SECRET` | Twitter consumer secret | `notreal` |
5861
| `TWITTER_TOKEN` | Twitter access token of the posting Twitter account | `notreal` |

config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package htmlhouse
22

33
import (
44
"github.com/danryan/env"
5+
"regexp"
6+
"strings"
57
)
68

79
type config struct {
@@ -23,6 +25,9 @@ type config struct {
2325
AdminPass string `env:"key=ADMIN_PASS default=uhoh"`
2426
BrowseItems int `env:"key=BROWSE_ITEMS default=10"`
2527

28+
BlacklistTerms string `env:"key=BLACKLIST_TERMS"`
29+
BlacklistReg *regexp.Regexp
30+
2631
// Twitter configuration
2732
TwitterConsumerKey string `env:"key=TWITTER_KEY default=notreal"`
2833
TwitterConsumerSecret string `env:"key=TWITTER_SECRET default=notreal"`
@@ -36,5 +41,11 @@ func newConfig() (*config, error) {
3641
return cfg, err
3742
}
3843

44+
// Process anything
45+
termsReg := `(?i)\b` + cfg.BlacklistTerms + `\b`
46+
termsReg = strings.Replace(termsReg, ",", `\b|\b`, -1)
47+
cfg.BlacklistReg = regexp.MustCompile(termsReg)
48+
49+
// Return result
3950
return cfg, nil
4051
}

construction.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"net/http"
99
"net/url"
10+
"os"
1011
"regexp"
1112
"strconv"
1213
"strings"
@@ -38,7 +39,7 @@ func createHouse(app *app, w http.ResponseWriter, r *http.Request) error {
3839

3940
resUser := newSessionInfo(houseID)
4041

41-
if public {
42+
if public && passesPublicFilter(app, html) {
4243
go addPublicAccess(app, houseID, html)
4344
}
4445

@@ -90,6 +91,10 @@ func addPublicAccess(app *app, houseID, html string) error {
9091
data.Set("url", fmt.Sprintf("%s/%s.html", app.cfg.HostName, houseID))
9192

9293
u, err := url.ParseRequestURI(app.cfg.PreviewsHost)
94+
if err != nil {
95+
fmt.Fprintf(os.Stderr, "Error parsing request URI: %v\n", err)
96+
return err
97+
}
9398
u.Path = "/"
9499
urlStr := fmt.Sprintf("%v", u)
95100

filter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package htmlhouse
2+
3+
func passesPublicFilter(app *app, html string) bool {
4+
if app.cfg.BlacklistTerms == "" {
5+
return true
6+
}
7+
8+
spam := app.cfg.BlacklistReg.MatchString(html)
9+
return !spam
10+
}

session.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package htmlhouse
22

33
import (
4+
"crypto/rsa"
45
"fmt"
56
jwt "github.com/dgrijalva/jwt-go"
67
"github.com/juju/errgo"
@@ -30,22 +31,32 @@ func newSessionInfo(houseID string) *sessionInfo {
3031
func newSessionManager(cfg *config) (sessionManager, error) {
3132
mgr := &defaultSessionManager{}
3233

33-
var err error
34-
35-
mgr.signKey, err = ioutil.ReadFile(cfg.PrivateKey)
34+
// Read and parse private key
35+
signBytes, err := ioutil.ReadFile(cfg.PrivateKey)
36+
if err != nil {
37+
return mgr, errgo.Mask(err)
38+
}
39+
mgr.signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes)
3640
if err != nil {
3741
return mgr, errgo.Mask(err)
3842
}
3943

40-
mgr.verifyKey, err = ioutil.ReadFile(cfg.PublicKey)
44+
// Read and parse public key
45+
verifyBytes, err := ioutil.ReadFile(cfg.PublicKey)
4146
if err != nil {
4247
return mgr, errgo.Mask(err)
4348
}
49+
mgr.verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
50+
if err != nil {
51+
return mgr, errgo.Mask(err)
52+
}
53+
4454
return mgr, nil
4555
}
4656

4757
type defaultSessionManager struct {
48-
verifyKey, signKey []byte
58+
verifyKey *rsa.PublicKey
59+
signKey *rsa.PrivateKey
4960
}
5061

5162
func (m *defaultSessionManager) readToken(r *http.Request) (string, error) {

templates/editor.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ <h1>HTMLhouse</h1>
125125
data: {html: editor.getSession().getValue(), public: $publicCheck.checked ? "true" : ""},
126126
success: function(data, status, xhr) {
127127
publishing = false;
128-
{{if .ID}}if (data.meta.code == 200) { {{else}}if (data.meta.code == 201) {
128+
{{if .ID}}if (data.code == 200) { {{else}}if (data.code == 201) {
129129
var houses = JSON.parse(H.get('neighborhood', '[]'));
130130
houses.push({id: data.data.id, token: xhr.getResponseHeader('Authorization')});
131131
H.set('neighborhood', JSON.stringify(houses));{{end}}
132132
justPublished = true;
133133
{{if .ID}}{{else}}H.remove('constructionSite');{{end}}
134134
window.location = '/' + data.data.id + '.html';
135135
} else {
136-
alert(data.meta.error_msg);
136+
alert(data.error_msg);
137137
}
138138
},
139139
error: function(jqXHR, status, error) {

0 commit comments

Comments
 (0)