Skip to content

Commit d2fda73

Browse files
authored
Merge pull request #14339 from karalabe/faucet-block-banned-users
cmd/faucet: further user validations and bot protection
2 parents 5aa21d8 + af48a33 commit d2fda73

5 files changed

Lines changed: 109 additions & 29 deletions

File tree

‎cmd/faucet/faucet.go‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"io/ioutil"
3030
"math/big"
3131
"net/http"
32+
"net/url"
3233
"os"
3334
"path/filepath"
3435
"strings"
@@ -73,6 +74,9 @@ var (
7374
githubUser = flag.String("github.user", "", "GitHub user to authenticate with for Gist access")
7475
githubToken = flag.String("github.token", "", "GitHub personal token to access Gists with")
7576

77+
captchaToken = flag.String("captcha.token", "", "Recaptcha site key to authenticate client side")
78+
captchaSecret = flag.String("captcha.secret", "", "Recaptcha secret key to authenticate server side")
79+
7680
logFlag = flag.Int("loglevel", 3, "Log level to use for Ethereum and the faucet")
7781
)
7882

@@ -96,9 +100,10 @@ func main() {
96100
}
97101
website := new(bytes.Buffer)
98102
template.Must(template.New("").Parse(string(tmpl))).Execute(website, map[string]interface{}{
99-
"Network": *netnameFlag,
100-
"Amount": *payoutFlag,
101-
"Period": period,
103+
"Network": *netnameFlag,
104+
"Amount": *payoutFlag,
105+
"Period": period,
106+
"Recaptcha": *captchaToken,
102107
})
103108
// Load and parse the genesis block requested by the user
104109
blob, err := ioutil.ReadFile(*genesisFlag)
@@ -297,7 +302,8 @@ func (f *faucet) apiHandler(conn *websocket.Conn) {
297302
for {
298303
// Fetch the next funding request and validate against github
299304
var msg struct {
300-
URL string `json:"url"`
305+
URL string `json:"url"`
306+
Captcha string `json:"captcha"`
301307
}
302308
if err := websocket.JSON.Receive(conn, &msg); err != nil {
303309
return
@@ -308,6 +314,33 @@ func (f *faucet) apiHandler(conn *websocket.Conn) {
308314
}
309315
log.Info("Faucet funds requested", "gist", msg.URL)
310316

317+
// If captcha verifications are enabled, make sure we're not dealing with a robot
318+
if *captchaToken != "" {
319+
form := url.Values{}
320+
form.Add("secret", *captchaSecret)
321+
form.Add("response", msg.Captcha)
322+
323+
res, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", form)
324+
if err != nil {
325+
websocket.JSON.Send(conn, map[string]string{"error": err.Error()})
326+
continue
327+
}
328+
var result struct {
329+
Success bool `json:"success"`
330+
Errors json.RawMessage `json:"error-codes"`
331+
}
332+
err = json.NewDecoder(res.Body).Decode(&result)
333+
res.Body.Close()
334+
if err != nil {
335+
websocket.JSON.Send(conn, map[string]string{"error": err.Error()})
336+
continue
337+
}
338+
if !result.Success {
339+
log.Warn("Captcha verification failed", "err", string(result.Errors))
340+
websocket.JSON.Send(conn, map[string]string{"error": "Beep-boop, you're a robot!"})
341+
continue
342+
}
343+
}
311344
// Retrieve the gist from the GitHub Gist APIs
312345
parts := strings.Split(msg.URL, "/")
313346
req, _ := http.NewRequest("GET", "https://api.github.com/gists/"+parts[len(parts)-1], nil)
@@ -334,7 +367,7 @@ func (f *faucet) apiHandler(conn *websocket.Conn) {
334367
continue
335368
}
336369
if gist.Owner.Login == "" {
337-
websocket.JSON.Send(conn, map[string]string{"error": "Nice try ;)"})
370+
websocket.JSON.Send(conn, map[string]string{"error": "Anonymous Gists not allowed"})
338371
continue
339372
}
340373
// Iterate over all the files and look for Ethereum addresses
@@ -348,6 +381,17 @@ func (f *faucet) apiHandler(conn *websocket.Conn) {
348381
websocket.JSON.Send(conn, map[string]string{"error": "No Ethereum address found to fund"})
349382
continue
350383
}
384+
// Validate the user's existence since the API is unhelpful here
385+
if res, err = http.Head("https://github.com/" + gist.Owner.Login); err != nil {
386+
websocket.JSON.Send(conn, map[string]string{"error": err.Error()})
387+
continue
388+
}
389+
res.Body.Close()
390+
391+
if res.StatusCode != 200 {
392+
websocket.JSON.Send(conn, map[string]string{"error": "Invalid user... boom!"})
393+
continue
394+
}
351395
// Ensure the user didn't request funds too recently
352396
f.lock.Lock()
353397
var (

‎cmd/faucet/faucet.html‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ <h1 style="text-align: center;"><i class="fa fa-bath" aria-hidden="true"></i> {{
5151
<div class="input-group">
5252
<input id="gist" type="text" class="form-control" placeholder="GitHub Gist URL containing your Ethereum address...">
5353
<span class="input-group-btn">
54-
<button class="btn btn-default" type="button" onclick="submit()">Give me Ether!</button>
54+
<button class="btn btn-default" type="button" onclick="{{if .Recaptcha}}grecaptcha.execute(){{else}}submit(){{end}}">Give me Ether!</button>
5555
</span>
56-
</div>
56+
</div>{{if .Recaptcha}}
57+
<div class="g-recaptcha" data-sitekey="{{.Recaptcha}}" data-callback="submit" data-size="invisible"></div>{{end}}
5758
</div>
5859
</div>
5960
<div class="row" style="margin-top: 32px;">
@@ -76,7 +77,7 @@ <h1 style="text-align: center;"><i class="fa fa-bath" aria-hidden="true"></i> {{
7677
<div class="row" style="margin-top: 32px;">
7778
<div class="col-lg-12">
7879
<h3>How does this work?</h3>
79-
<p>This Ether faucet is running on the {{.Network}} network. To prevent malicious actors from exhausting all available funds or accumulating enough Ether to mount long running spam attacks, requests are tied to GitHub accounts. Anyone having a GitHub account may request funds within the permitted limit of <strong>{{.Amount}} Ether(s) / {{.Period}}</strong>.</p>
80+
<p>This Ether faucet is running on the {{.Network}} network. To prevent malicious actors from exhausting all available funds or accumulating enough Ether to mount long running spam attacks, requests are tied to GitHub accounts. Anyone having a GitHub account may request funds within the permitted limit of <strong>{{.Amount}} Ether(s) / {{.Period}}</strong>.{{if .Recaptcha}} The faucet is running invisible reCaptcha protection against bots.{{end}}</p>
8081
<p>To request funds, simply create a <a href="https://gist.github.com/" target="_about:blank">GitHub Gist</a> with your Ethereum address pasted into the contents (the file name doesn't matter), copy paste the gists URL into the above input box and fire away! You can track the current pending requests below the input field to see how much you have to wait until your turn comes.</p>
8182
</div>
8283
</div>
@@ -89,8 +90,9 @@ <h3>How does this work?</h3>
8990
var server;
9091

9192
// Define the function that submits a gist url to the server
92-
var submit = function() {
93-
server.send(JSON.stringify({url: $("#gist")[0].value}));
93+
var submit = function({{if .Recaptcha}}captcha{{end}}) {
94+
server.send(JSON.stringify({url: $("#gist")[0].value{{if .Recaptcha}}, captcha: captcha{{end}}}));{{if .Recaptcha}}
95+
grecaptcha.reset();{{end}}
9496
};
9597
// Define a method to reconnect upon server loss
9698
var reconnect = function() {
@@ -134,10 +136,10 @@ <h3>How does this work?</h3>
134136
}
135137
}
136138
server.onclose = function() { setTimeout(reconnect, 3000); };
137-
server.onerror = function() { setTimeout(reconnect, 3000); };
138139
}
139140
// Establish a websocket connection to the API server
140141
reconnect();
141-
</script>
142+
</script>{{if .Recaptcha}}
143+
<script src="https://www.google.com/recaptcha/api.js" async defer></script>{{end}}
142144
</body>
143145
</html>

0 commit comments

Comments
 (0)