Skip to content

Commit 75ccea0

Browse files
Add test for cache pollution bug
1 parent de02b1a commit 75ccea0

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

test/controllers/controller_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3667,6 +3667,23 @@ def test_show_author_recursive
36673667
end
36683668
end
36693669

3670+
class Api::V2::AuthorsControllerTest < ActionController::TestCase
3671+
def test_cache_pollution_for_non_admin_indirect_access_to_banned_books
3672+
cache = ActiveSupport::Cache::MemoryStore.new
3673+
with_resource_caching(cache) do
3674+
$test_user = Person.find(5)
3675+
get :show, params: {id: '2', include: 'books'}
3676+
assert_response :success
3677+
assert_equal 2, json_response['included'].length
3678+
3679+
$test_user = Person.find(1)
3680+
get :show, params: {id: '2', include: 'books'}
3681+
assert_response :success
3682+
assert_equal 1, json_response['included'].length
3683+
end
3684+
end
3685+
end
3686+
36703687
class Api::BoxesControllerTest < ActionController::TestCase
36713688
def test_complex_includes_base
36723689
assert_cacheable_get :index

test/fixtures/active_record.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,9 @@ class LikesController < JSONAPI::ResourceController
798798

799799
module V2
800800
class AuthorsController < JSONAPI::ResourceController
801+
def context
802+
{current_user: $test_user}
803+
end
801804
end
802805

803806
class PeopleController < JSONAPI::ResourceController
@@ -1448,6 +1451,24 @@ class PreferencesResource < PreferencesResource; end
14481451
class PersonResource < PersonResource; end
14491452
class PostResource < PostResource; end
14501453

1454+
class AuthorResource < JSONAPI::Resource
1455+
model_name 'Person'
1456+
attributes :name
1457+
1458+
has_many :books, inverse_relationship: :authors
1459+
1460+
def records_for(rel_name)
1461+
records = _model.public_send(rel_name)
1462+
if rel_name == :books
1463+
# Hide indirect access to banned books unless current user is a book admin
1464+
unless context[:current_user].try(:book_admin)
1465+
records = records.where(banned: false)
1466+
end
1467+
end
1468+
return records
1469+
end
1470+
end
1471+
14511472
class BookResource < JSONAPI::Resource
14521473
attribute :title
14531474
attributes :isbn, :banned
@@ -1483,7 +1504,7 @@ def records(options = {})
14831504
context = options[:context]
14841505
current_user = context ? context[:current_user] : nil
14851506

1486-
records = _model_class
1507+
records = _model_class.all
14871508
# Hide the banned books from people who are not book admins
14881509
unless current_user && current_user.book_admin
14891510
records = records.where(not_banned_books)

test/fixtures/book_authors.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ book_author_2_1:
99
book_author_2_2:
1010
book_id: 2
1111
person_id: 2
12+
13+
book_author_654_2:
14+
book_id: 654 # Banned book
15+
person_id: 2

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class CatResource < JSONAPI::Resource
289289

290290
jsonapi_resource :preferences, except: [:create, :destroy]
291291

292+
jsonapi_resources :authors
292293
jsonapi_resources :books
293294
jsonapi_resources :book_comments
294295
end

0 commit comments

Comments
 (0)