Skip to content

Commit 7d479d9

Browse files
committed
feat(payments): add strong parameters filtering
- Replace raw params access with params.permit - Add nested data parameter filtering for Stripe tokens - Create test coverage for parameter filtering - Add minimal view template for create action Security improvement: prevents mass assignment of unpermitted fields
1 parent bfd2efc commit 7d479d9

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

app/controllers/payments_controller.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ class PaymentsController < ApplicationController
44
def new; end
55

66
def create
7-
@amount = params[:amount]
7+
payment_params = params.permit(:amount, :name, data: [:email, :id])
8+
9+
@amount = payment_params[:amount]
810

911
customer = Stripe::Customer.create(
10-
email: params[:data][:email],
11-
description: params[:name],
12-
source: params[:data][:id]
12+
email: payment_params[:data][:email],
13+
description: payment_params[:name],
14+
source: payment_params[:data][:id]
1315
)
1416

1517
charge_customer(customer, @amount)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Payment processed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
RSpec.describe PaymentsController do
2+
let(:member) { Fabricate(:member) }
3+
4+
before do
5+
login(member)
6+
allow(Stripe::Customer).to receive(:create).and_return(double(id: 'cus_123'))
7+
allow(Stripe::Charge).to receive(:create).and_return(true)
8+
end
9+
10+
describe 'POST #create' do
11+
context 'with valid parameters' do
12+
it 'creates a Stripe customer and charge' do
13+
expect(Stripe::Customer).to receive(:create).with(
14+
email: 'john@example.com',
15+
description: 'John Doe',
16+
source: 'tok_123'
17+
).and_return(double(id: 'cus_123'))
18+
19+
post :create, params: { amount: '1000', name: 'John Doe', data: { email: 'john@example.com', id: 'tok_123' } }
20+
expect(response).to be_successful
21+
end
22+
end
23+
24+
context 'with parameter filtering' do
25+
it 'filters unpermitted parameters' do
26+
# params.permit now filters the payment parameters
27+
post :create, params: { amount: '1000', name: 'John', data: { email: 'john@example.com', id: 'tok_123' }, hacker_field: 'malicious' }
28+
expect(response).to be_successful
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)