Skip to content

Commit e09124d

Browse files
authored
Feedback: Better Spam Checks (#1546)
* Better Feedback Form Spam Checks * Set reply-to * Remove debugger
1 parent 954b124 commit e09124d

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

.rubocop.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ Metrics/AbcSize:
2424
Max: 100
2525

2626
Metrics/BlockLength:
27-
Max: 45
27+
Max: 45
28+
29+
Layout/LineLength:
30+
Max: 120

app/controllers/feedbacks_controller.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ def create
88

99
# spam? will tell us if the hidden field was filled in (it shouldn't be filled in)
1010
# valid? will tell us if the humanity test was answered correctly
11-
if @feedback.spam? or !@feedback.valid?
12-
flash[:alert] = _('Our apologies but you failed the spam check. You could try contacting us on Github instead.')
11+
if @feedback.spam? || !@feedback.valid?
12+
flash[:alert] =
13+
_('Our apologies but you failed the spam check. You could try contacting us on Github instead.')
1314
render :new, status: :unprocessable_entity
1415
else
1516
@feedback.request = request
1617
if @feedback.deliver
17-
redirect_to root_path, :notice => _('Feedback sent! We will get back to you as soon as possible.')
18+
redirect_to root_path,
19+
notice: _('Feedback sent! We will get back to you as soon as possible.')
1820
else
19-
flash[:alert] = _('Could not send feedback. Did you pass the Humanity Test? Valid email? Try again?')
21+
flash[:alert] =
22+
_('Could not send feedback. Did you pass the Humanity Test? Valid email? Try again?')
2023
render :new, status: :unprocessable_entity
2124
end
2225
end

app/models/feedback.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ class Feedback < MailForm::Base
33
append :remote_ip, :user_agent, :referrer
44

55
attribute :name, validate: true
6-
attribute :email, validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
6+
attribute :email, validate: /\A([\w.%+-]+)@([\w-]+\.)+(\w{2,})\z/i
77
attribute :message, validate: true
8-
attribute :control, validate: /\A95\z/
8+
attribute :control, validate: /\A97\z/
99
attributes :nickname, captcha: true
1010

11+
validates :message, format: { without: /\b(SEO|offer|ranking)\b+/i,
12+
message: 'spam detected' }
13+
1114
# Declare the e-mail headers. It accepts anything the mail method
1215
# in ActionMailer accepts.
1316
def headers
14-
headers = {
17+
{
1518
to: Settings.feedback.email,
1619
from: Settings.mail.mailer_sender,
17-
subject: Settings.brand.title + ' Feedback',
20+
subject: "#{Settings.brand.title} Feedback",
21+
reply_to: email,
1822
'X-PWPUSH-URL' => request.url
1923
}
20-
headers
2124
end
2225
end

app/views/feedbacks/new.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</div>
2727

2828
<div class="my-3">
29-
<%= f.label :name, _('What is one hundred minus 5?'), for: 'feedback_control', class: 'form-label' %>
29+
<%= f.label :name, _('What is one hundred minus 3?'), for: 'feedback_control', class: 'form-label' %>
3030
<%= f.text_field :control, { class: "form-control", required: true } %>
3131
</div>
3232

test/models/feedback_test.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
CONTROL = '97'
6+
7+
class FeedbackTest < ActiveSupport::TestCase
8+
test 'spam is caught and trashed' do
9+
feedback = Feedback.new(
10+
control: CONTROL,
11+
name: 'Joey',
12+
13+
message: <<~SPAMMSG
14+
Hello pwpush.com owner,
15+
16+
We can help you grow your online presence and attract more customers to your business with our Top SEO Services.
17+
18+
Our team of experts can improve your Google and YouTube Ranking, optimize your Google Maps listing, provide Professional Content for your website, and increase your Website Traffic.
19+
20+
Don't miss this opportunity to grow your business and stay ahead of the competition.
21+
22+
=>> https://some-spam-site.com
23+
24+
Best regards,
25+
Mullet
26+
SPAMMSG
27+
)
28+
assert_not feedback.valid?
29+
end
30+
31+
test 'valid emails are allowed through' do
32+
feedback = Feedback.new(
33+
control: CONTROL,
34+
name: 'Joey',
35+
36+
message: <<~MSG
37+
Hello! We love Password Pusher! It's the best!
38+
MSG
39+
)
40+
assert feedback.valid?
41+
end
42+
43+
test 'bad control is blocked' do
44+
feedback = Feedback.new(
45+
control: 1,
46+
name: 'Joey',
47+
48+
message: <<~MSG
49+
Hello! We love Password Pusher! It's the best!
50+
MSG
51+
)
52+
assert_not feedback.valid?
53+
end
54+
end

0 commit comments

Comments
 (0)