Skip to content
4 changes: 3 additions & 1 deletion app/controllers/account_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class AccountController < ApplicationController
def update
@old_pinned_post = @account.pinned_post

@account.update!(account_params)
unless @account.update(account_params)
return render plain: @account.errors.full_messages.to_sentence, status: :unprocessable_entity
end

respond_to do |format|
format.html { redirect_to page_posts_path(@account), notice: 'Success', status: :see_other }
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/marketing_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def showcase_accounts
end

def featured_account
# Read the current account and pin on every request. Caching these records can
# expose a post after it is hidden, unpublished, archived or no longer pinned.
showcase_accounts.includes(pinned_post: :rich_text_body).find_by(slug: 'philipithomas')
end
end
1 change: 1 addition & 0 deletions app/controllers/public_posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def generate_og_image

def set_post
@post = @account.posts.published.friendly.find(params[:slug])
response.set_header('X-Robots-Tag', 'noindex, nofollow') if @post.visibility_hidden?

return if @post.slug == params[:slug]

Expand Down
18 changes: 18 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Account < ApplicationRecord
validate :email_is_valid, on: :create
validates :name, presence: true
validates :accent_color, format: { with: /\A#[0-9a-fA-F]{6}\z/ }, allow_nil: true
validate :pinned_post_can_be_featured, if: :will_save_change_to_pinned_post_id?
VALID_SLUG_REGEX = /\A[a-z0-9]+(-[a-z0-9]+)*\z/
validates :slug, presence: true, length: { minimum: 2, maximum: 255 },
format: { with: VALID_SLUG_REGEX, message: "can only contain letters, numbers and '-'" },
Expand Down Expand Up @@ -127,6 +128,12 @@ def show_posts_page?
posts.published.length > 1
end

# Older records may contain a pin that predates the ownership validation.
# Never render those posts on a public account page.
def public_pinned_post
pinned_post if pinnable_post?(pinned_post)
Comment thread
philipithomas marked this conversation as resolved.
Resolved
Comment thread
philipithomas marked this conversation as resolved.
Resolved
end

def active_subscription?
return true if Rails.configuration.solo_mode

Expand Down Expand Up @@ -313,6 +320,17 @@ def register_first_sign_in_actions # rubocop:disable Metrics/AbcSize

private

def pinnable_post?(post)
post.present? && post.account_id == id && post.published? && !post.archived? && !post.visibility_hidden?
end

def pinned_post_can_be_featured
return if pinned_post_id.blank?
return if pinnable_post?(pinned_post)

errors.add(:pinned_post, 'must be one of your published, visible posts')
end

def updates_subscription_email_cutoffs
addresses = {}
audits.where("audited_changes ? 'email'").reorder(:version).pluck(:audited_changes, :created_at).each do |changes, changed_at|
Expand Down
2 changes: 1 addition & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Post < ApplicationRecord
friendly_id :subject, use: %i[slugged history scoped], scope: :account

before_destroy :remove_pinned_by
after_save :remove_pinned_by, if: proc { archived? or visibility_hidden? }
after_save :remove_pinned_by, if: proc { archived? || visibility_hidden? || draft? }

enum :visibility, { public: 0, unlisted: 1, hidden: 2 }, prefix: true, default: :public

Expand Down
5 changes: 1 addition & 4 deletions app/views/marketing_pages/_how_it_works.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ steps = {
}
%>

<%
featured_post = @featured_account&.pinned_post
featured_post = nil unless featured_post&.published? && !featured_post.archived? && !featured_post.visibility_hidden? && featured_post.account_id == @featured_account.id
%>
<% featured_post = @featured_account&.public_pinned_post %>
<div class="dashboard-container">
<div class="grid grid-cols-1 gap-y-16 gap-x-8 sm:gap-y-20 <%= featured_post ? 'lg:grid-cols-2' : '' %> lg:items-start">
<% if featured_post %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/public_pages/_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ simple_mode = defined?(:simple_mode) ? simple_mode : false

<div>
<div class="w-full py-12 mt-12 bg-gray-050">
<% if account.pinned_post.present? %>
<% if (pinned_post = account.public_pinned_post) %>
<div class="pb-12 public-page-container">
<%= render :partial => 'shared/post_card', locals: { post: account.pinned_post } %>
<%= render :partial => 'shared/post_card', locals: { post: pinned_post } %>
</div>
<% end %>

Expand Down
127 changes: 127 additions & 0 deletions test/controllers/marketing_featured_cache_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# frozen_string_literal: true

require 'test_helper'
require 'minitest/mock'

class MarketingFeaturedCacheTest < ActionDispatch::IntegrationTest
setup do
@original_solo_mode = Rails.configuration.solo_mode
@original_multiuser_mode = Rails.configuration.multiuser_mode
@original_cache = Rails.cache
Rails.configuration.solo_mode = false
Rails.configuration.multiuser_mode = true
Rails.cache = ActiveSupport::Cache::MemoryStore.new
@account = accounts(:grandfathered_user)
@account.update!(slug: 'philipithomas')
File.open(Rails.root.join('app/assets/images/logo/icon.png')) do |photo|
@account.photo.attach(io: photo, filename: 'profile.png', content_type: 'image/png')
end
@post = @account.posts.create!(
subject: 'Cached feature original title', body: 'Cached feature original body',
published_at: Time.current
)
@account.update!(pinned_post: @post)
host! Rails.configuration.base_host
end

teardown do
@account.photo.purge
Rails.configuration.solo_mode = @original_solo_mode
Rails.configuration.multiuser_mode = @original_multiuser_mode
Rails.cache = @original_cache
end

{
'hidden' => { visibility: :hidden },
'unpublished' => { published_at: nil },
'archived' => { archived: true }
}.each do |state, attributes|
test "warm marketing cache stops exposing a post immediately after it becomes #{state}" do
with_production_cache do
get '/'
assert_featured_content(@post)

# Exercise real persistence and pin-removal callbacks after warming the
# cache. Neither the test nor production code clears the cache.
@post.update!(attributes)

get '/'
assert_feature_absent(@post)
end
end
end

test 'warm marketing cache immediately reflects removal of the featured pin' do
with_production_cache do
get '/'
assert_featured_content(@post)

@account.update!(pinned_post: nil)

get '/'
# The public "Read newest post" link can remain after unpinning; the
# hero card and social preview must stop featuring the post.
assert_feature_absent(@post, check_url: false)
end
end

test 'warm marketing cache shows a replacement pin instead of the previous post' do
with_production_cache do
get '/'
assert_featured_content(@post)

replacement = @account.posts.create!(
subject: 'Replacement feature title', body: 'Replacement feature body', published_at: Time.current
)
@account.update!(pinned_post: replacement)

get '/'
assert_feature_absent(@post)
assert_featured_content(replacement)
end
end

test 'a legacy cached account cannot disclose a post hidden after the entry was written' do
with_production_cache do
Rails.cache.write('homepage-featured-account', Account.includes(:pinned_post).find(@account.id))
@post.update!(visibility: :hidden)

get '/'
assert_feature_absent(@post)
end
end

test 'warm marketing cache stops featuring an account after it is locked' do
with_production_cache do
get '/'
assert_featured_content(@post)
@account.update!(locked_at: Time.current)

get '/'
assert_feature_absent(@post)
assert_not_includes response.body, @account.name
end
end

private

def with_production_cache(&block)
# Keep the test environment and its offline mail/job adapters. Only select
# the production cache path after the app has already booted normally.
Rails.env.stub(:production?, true, &block)
end

def assert_featured_content(post)
assert_response :success
assert_select 'p', text: "I just published \"#{post.subject}\" on my website - check it out:", count: 1
assert_select 'img[alt=?]', post.subject, count: 1
assert_includes response.body, post.slug
end

def assert_feature_absent(post, check_url: true)
assert_response :success
assert_not_includes response.body, post.subject
assert_not_includes response.body, post.body.to_plain_text
assert_not_includes response.body, post.slug if check_url
end
end
119 changes: 119 additions & 0 deletions test/controllers/pinned_posts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# frozen_string_literal: true

require 'test_helper'

class PinnedPostsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers

setup do
@original_solo_mode = Rails.configuration.solo_mode
@original_multiuser_mode = Rails.configuration.multiuser_mode
Rails.configuration.solo_mode = false
Rails.configuration.multiuser_mode = true
@account = accounts(:new_user)
@other_account = accounts(:grandfathered_user)
host! Rails.configuration.base_host
sign_in @account
end

teardown do
Rails.configuration.solo_mode = @original_solo_mode
Rails.configuration.multiuser_mode = @original_multiuser_mode
end

test 'can feature an owned public post and remove the feature' do
post = create_post(@account)

put page_account_path(@account), params: { account: { pinned_post_id: post.id } }

assert_redirected_to page_posts_path(@account)
assert_equal post.id, @account.reload.pinned_post_id

put page_account_path(@account), params: { account: { pinned_post_id: '' } }

assert_redirected_to page_posts_path(@account)
assert_nil @account.reload.pinned_post_id
end

test 'can continue to feature an owned unlisted post' do
post = create_post(@account, visibility: :unlisted)

put page_account_path(@account), params: { account: { pinned_post_id: post.id } }

assert_redirected_to page_posts_path(@account)
assert_equal post.id, @account.reload.pinned_post_id
end

test 'cannot feature another account published post or draft' do
[Time.current, nil].each do |published_at|
post = create_post(@other_account, published_at: published_at)

put page_account_path(@account), params: { account: { pinned_post_id: post.id } }

assert_response :unprocessable_entity
assert_nil @account.reload.pinned_post_id
end
end

test 'cannot feature a draft hidden or archived post' do
[{ published_at: nil }, { visibility: :hidden }, { archived: true }].each do |attributes|
post = create_post(@account, **attributes)

put page_account_path(@account), params: { account: { pinned_post_id: post.id } }

assert_response :unprocessable_entity
assert_nil @account.reload.pinned_post_id
end
end

test 'invalid pin leaves the existing feature unchanged' do
existing_post = create_post(@account)
@account.update!(pinned_post: existing_post)

put page_account_path(@account), params: { account: { pinned_post_id: -1 } }

assert_response :unprocessable_entity
assert_equal existing_post.id, @account.reload.pinned_post_id
end

test 'cannot update the feature on another account' do
post = create_post(@other_account)

put page_account_path(@other_account), params: { account: { pinned_post_id: post.id } }

assert_redirected_to page_path(@account)
assert_nil @other_account.reload.pinned_post_id
end

test 'public homepage does not disclose a foreign draft from a historical invalid pin' do
post = create_post(@other_account, published_at: nil, subject: 'Confidential draft', body: 'Private draft content')
@account.update_column(:pinned_post_id, post.id)
sign_out @account
host! @account.postcard_host

get root_path

assert_response :success
assert_no_match 'Confidential draft', response.body
assert_no_match 'Private draft content', response.body
end

test 'marketing homepage excludes historical invalid featured pins' do
@account.update!(slug: 'philipithomas')
post = create_post(@other_account, published_at: nil, subject: 'Confidential draft', body: 'Private draft content')
@account.update_column(:pinned_post_id, post.id)
sign_out @account

get '/'

assert_response :success
assert_no_match 'Confidential draft', response.body
assert_no_match 'Private draft content', response.body
end

private

def create_post(account, **attributes)
account.posts.create!({ subject: 'A post', body: 'Post content', published_at: Time.current }.merge(attributes))
end
end
Loading