159 行
7.6 KiB
Ruby
159 行
7.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe StaticMirror do
|
|
include_context 'Ainu locale'
|
|
|
|
let(:tag) { create(:tag, primary_name: 'mirror-public-tag') }
|
|
let(:post) { create(:post, title: '<img src=x onerror=alert(1)>') }
|
|
let(:mirror) { described_class.new(locale: Locale.nipponese, origin: 'https://source.test') }
|
|
|
|
def snapshot_tag(record, event: 'create', number: 1)
|
|
TagVersion.create!(tag: record, version_no: number, event_type: event,
|
|
name: record.name('ja'), category: record.category, aliases: '', parent_tag_ids: '',
|
|
names: { ja: { name: record.name('ja'), script_code: 'Jpan',
|
|
auto_generated: false, aliases: [] } }, created_at: Time.current)
|
|
end
|
|
|
|
def snapshot_post(record, tags: [])
|
|
PostVersion.create!(post: record, version_no: 1, event_type: 'create',
|
|
title: record.title, url: record.url, tags: '', tags_json: tags,
|
|
parent_post_ids: '', created_at: Time.current)
|
|
end
|
|
|
|
# Read STORE entries independently of the production writer and verify CRCs.
|
|
def archive_files(bytes)
|
|
offset = 0
|
|
files = { }
|
|
while bytes.byteslice(offset, 4).unpack1('V') == 0x04034b50
|
|
header = bytes.byteslice(offset, 30).unpack('VvvvvvVVVvv')
|
|
size, name_size, extra_size = header[7], header[9], header[10]
|
|
path = bytes.byteslice(offset + 30, name_size).force_encoding('UTF-8')
|
|
data = bytes.byteslice(offset + 30 + name_size + extra_size, size)
|
|
expect(Zlib.crc32(data)).to eq(header[6])
|
|
files[path.delete_prefix('archive/')] = data.force_encoding('UTF-8')
|
|
offset += 30 + name_size + extra_size + size
|
|
end
|
|
expect(bytes.byteslice(-22, 4).unpack1('V')).to eq(0x06054b50)
|
|
files
|
|
end
|
|
|
|
before do
|
|
snapshot_tag(tag)
|
|
PostTag.create!(post:, tag:)
|
|
snapshot_post(post, tags: [{ tag_id: tag.id, version_no: 1, sections: [] }])
|
|
end
|
|
|
|
it 'generates a navigable ZIP, escaped HTML, search data and verified hashes' do
|
|
progress = []
|
|
files = archive_files(mirror.export { |done, total| progress << [done, total] })
|
|
expect(progress.last).to eq([2, 2])
|
|
expect(files.keys).to include('index.html', 'robots.txt', 'assets/archive.css',
|
|
'assets/archive.js', 'archive-config.js', 'search-index.json', 'manifest.json',
|
|
'ja/posts/index.html', "ja/posts/#{ post.id }/index.html",
|
|
'ja/tags/index.html', "ja/tags/#{ tag.id }/index.html")
|
|
files.select { |path, _| path.end_with?('.html') }.each do |path, html|
|
|
document = Nokogiri::HTML(html)
|
|
expect(document.at_css('meta[name="robots"]')['content']).to include('noindex')
|
|
expect(document.text).to include('静的保存版', mirror.data_at)
|
|
expect(document.css('img')).to be_empty
|
|
expect(document.at_css('[data-original]')['href']).to start_with('https://source.test/ja')
|
|
document.css('a[href], link[href], script[src]').each do |node|
|
|
target = node['href'] || node['src']
|
|
next if target.start_with?('https://')
|
|
|
|
expect(target).not_to start_with('/')
|
|
resolved = Pathname.new(path).dirname.join(target).cleanpath.to_s
|
|
expect(files).to have_key(resolved)
|
|
end
|
|
end
|
|
detail = Nokogiri::HTML(files.fetch("ja/posts/#{ post.id }/index.html"))
|
|
expect(detail.at_css('h1').text).to eq(post.title)
|
|
expect(detail.css('main a').map { _1['href'] })
|
|
.to include("../../tags/#{ tag.id }/index.html")
|
|
manifest = JSON.parse(files.fetch('manifest.json'))
|
|
expect(manifest).to include('format_version' => 1, 'locale' => 'ja',
|
|
'data_at' => mirror.data_at, 'resources' => { 'posts' => 1, 'tags' => 1 })
|
|
expect(Time.iso8601(manifest.fetch('generated_at'))).to be >= Time.iso8601(mirror.data_at)
|
|
expect(manifest.fetch('files').map { _1.fetch('path') })
|
|
.to match_array(files.keys - ['manifest.json'])
|
|
manifest.fetch('files').each do |entry|
|
|
digest = Digest::SHA256.hexdigest(files.fetch(entry.fetch('path')))
|
|
expect(entry.fetch('sha256')).to eq(digest)
|
|
end
|
|
expect(JSON.parse(files.fetch('search-index.json')).map { _1.fetch('path') })
|
|
.to include("ja/posts/#{ post.id }/index.html", "ja/tags/#{ tag.id }/index.html")
|
|
end
|
|
|
|
it 'keeps private and unversioned records out even for an admin caller' do
|
|
private_tag = create(:tag, primary_name: 'mirror-secret-tag')
|
|
snapshot_tag(private_tag)
|
|
snapshot_tag(private_tag, event: 'discard', number: 2)
|
|
private_post = create(:post, title: 'mirror-secret-post')
|
|
snapshot_post(private_post)
|
|
PostVersion.create!(post: private_post, version_no: 2, event_type: 'discard',
|
|
url: private_post.url, tags: '', tags_json: [], parent_post_ids: '',
|
|
created_at: Time.current)
|
|
legacy = create(:post, title: 'mirror-unversioned-secret')
|
|
referencing = create(:post, title: 'mirror-secret-reference')
|
|
snapshot_post(referencing,
|
|
tags: [{ tag_id: private_tag.id, version_no: 1, sections: [] }])
|
|
files = HistoryAccess.set(user: create(:user, :admin)) { archive_files(mirror.export) }
|
|
expect(files.values.join).not_to include('mirror-secret', legacy.title, private_post.url)
|
|
expect(files.keys).not_to include("ja/tags/#{ private_tag.id }/index.html",
|
|
"ja/posts/#{ private_post.id }/index.html", "ja/posts/#{ referencing.id }/index.html")
|
|
expect(mirror.authority.fetch('admin')).to be(false)
|
|
end
|
|
|
|
it 'applies later discard boundaries after a restoration' do
|
|
snapshot_tag(tag, event: 'discard', number: 2)
|
|
snapshot_tag(tag, event: 'restore', number: 3)
|
|
tag.update_columns(version_no: 3)
|
|
expect(archive_files(mirror.export)).to have_key("ja/tags/#{ tag.id }/index.html")
|
|
snapshot_tag(tag, event: 'discard', number: 4)
|
|
second = described_class.new(locale: Locale.nipponese, origin: 'https://source.test')
|
|
files = archive_files(second.export)
|
|
expect(files.keys).not_to include("ja/tags/#{ tag.id }/index.html")
|
|
expect(files.values.join).not_to include('mirror-public-tag')
|
|
end
|
|
|
|
it 'does not include credentials or executable URL schemes' do
|
|
post.update_columns(url: 'https://secret-user:secret-password@source.test/path')
|
|
content = archive_files(mirror.export).values.join
|
|
expect(content).not_to include('secret-password', 'secret-user')
|
|
post.update_columns(url: 'javascript:alert(1)')
|
|
expect(archive_files(mirror.export).values.join).not_to include('javascript:')
|
|
end
|
|
|
|
it 'fails if publication changes during generation' do
|
|
expect do
|
|
mirror.export do |done, _|
|
|
snapshot_tag(tag, event: 'discard', number: 2) if done.zero?
|
|
end
|
|
end.to raise_error(HistoryAccess::Forbidden)
|
|
end
|
|
|
|
it 'bounds output and never reports hidden population counts' do
|
|
stub_const('StaticMirror::RESOURCE_LIMIT', 1)
|
|
second = create(:post, title: 'mirror-beyond-limit')
|
|
snapshot_post(second)
|
|
files = archive_files(mirror.export)
|
|
expect(files.values.join).not_to include(second.title, 'population_count', 'excluded_count')
|
|
stub_const('StaticMirror::MAX_BYTES', 16)
|
|
expect { mirror.export }.to raise_error(StaticMirror::TooLarge)
|
|
end
|
|
|
|
it 'keeps all registered language names and aliases without locale filtering' do
|
|
locale
|
|
create(:tag_name, :alias, tag:, name: 'mirror-alias', language_code: 'ja', script_code: 'Jpan')
|
|
files = archive_files(described_class.new(locale:, origin: 'https://source.test').export)
|
|
detail = files.fetch("ain-Kana/tags/#{ tag.id }/index.html")
|
|
expect(detail).to include('mirror-public-tag', 'mirror-alias', 'lang="ain"', 'lang="ja"')
|
|
end
|
|
|
|
it 'fails rather than truncating registered aliases at the name limit' do
|
|
create(:tag_name, :alias, tag:, name: 'mirror-extra-alias')
|
|
stub_const('StaticMirror::NAME_LIMIT', 1)
|
|
expect { mirror.export }.to raise_error(StaticMirror::TooLarge)
|
|
end
|
|
end
|