AI: address issue #483

このコミットが含まれているのは:
Codex Night Worker
2026-11-17 04:09:38 +09:00
コミット 9c71a9fc02
7個のファイルの変更、358行の追加、46行の削除
+91 -3
ファイルの表示
@@ -38,7 +38,8 @@ RSpec.describe 'Static Mirror API', type: :request do
get "/static_mirror/jobs/#{ id }"
expect(json).to include('status' => 'ready', 'processed' => 1, 'total' => 1)
expect(json.fetch('data_at')).to be_present
expect(json.keys).not_to include('owner', 'origin', 'actor_id', 'policy_revision')
expect(json.keys).not_to include('owner', 'origin', 'actor_id', 'policy_revision',
'publication_policy', 'history_revision', 'post_ids', 'tag_ids', 'content_digest')
get "/static_mirror/files/#{ id }"
expect(response).to have_http_status(:ok)
expect(response.media_type).to eq('application/zip')
@@ -47,6 +48,21 @@ RSpec.describe 'Static Mirror API', type: :request do
expect(response.headers['Cache-Control']).to include('no-store')
end
it 'bakes the canonical source origin regardless of request and forwarded hosts' do
host! 'mirror-intake.test'
post '/static_mirror/jobs', params: { locale: 'ja' }, as: :json,
headers: { 'X-Forwarded-Host' => 'untrusted-source.test',
'X-Forwarded-Proto' => 'https' }
expect(response).to have_http_status(:accepted)
id = json.fetch('job')
expect(StaticMirrorJob.state).not_to have_key('origin')
StaticMirrorJob.perform_now(id)
get "/static_mirror/files/#{ id }"
expect(response).to have_http_status(:ok)
expect(response.body).to include('https://hub.nizika.monster/ja')
expect(response.body).not_to include('mirror-intake.test', 'untrusted-source.test')
end
it 'rejects historical and unknown input instead of pretending to restore it' do
['at', 'data_at', 'admin', 'limit'].each do |key|
post '/static_mirror/jobs', params: { locale: 'ja', key => '2020-01-01' }, as: :json
@@ -87,6 +103,21 @@ RSpec.describe 'Static Mirror API', type: :request do
expect(response).to have_http_status(:not_found)
end
it 'rechecks ready ZIPs after a non-discard content change' do
record = create(:post, title: 'mirror-withdrawn-title')
PostVersion.create!(post: record, version_no: 1, event_type: 'create',
title: record.title, url: record.url, tags: '', tags_json: [], parent_post_ids: '',
created_at: Time.current)
id = create_job
StaticMirrorJob.perform_now(id)
record.update!(title: 'mirror-corrected-title')
get "/static_mirror/jobs/#{ id }"
expect(response).to have_http_status(:not_found)
get "/static_mirror/files/#{ id }"
expect(response).to have_http_status(:not_found)
expect(response.body).not_to include('mirror-withdrawn-title')
end
it 'fails lost jobs and rejects expired downloads' do
id = create_job
travel 80.seconds do
@@ -110,11 +141,68 @@ RSpec.describe 'Static Mirror API', type: :request do
end
end
it 'keeps simultaneous generation bounded after the cooldown' do
it 'preserves the queued reservation after the cooldown' do
id = create_job
travel 61.seconds do
post '/static_mirror/jobs', params: { locale: 'ja' }, as: :json
expect(response).to have_http_status(:too_many_requests)
expect(StaticMirrorJob.state).to include('id' => id, 'status' => 'queued')
expect(StaticMirrorJob).to have_received(:perform_later).once
StaticMirrorJob.perform_now(id)
get "/static_mirror/jobs/#{ id }"
expect(json).to include('status' => 'failed', 'error' => 'generation_timeout')
end
end
it 'reclaims a stalled queued reservation without waiting for a status request' do
id = create_job
travel 80.seconds do
second_id = create_job
expect(second_id).not_to eq(id)
StaticMirrorJob.perform_now(id)
expect(StaticMirrorJob.state).to include('id' => second_id, 'status' => 'queued')
StaticMirrorJob.perform_now(second_id)
get "/static_mirror/jobs/#{ second_id }"
expect(json).to include('status' => 'ready')
end
end
it 'waits for brief intake lock contention instead of losing the queued worker' do
id = create_job
path = StaticMirrorJob.directory.join('.generation.lock')
lock = File.open(path, File::RDWR)
lock.flock(File::LOCK_EX)
attempts = Queue.new
worker = StaticMirrorJob.new
allow(worker).to receive(:generate)
allow(File).to receive(:open).and_call_original
allow(File).to receive(:open).with(path, File::RDWR).and_wrap_original do |original, *args, &block|
original.call(*args) do |file|
allow(file).to receive(:flock).and_wrap_original do |flock, mode|
attempts << mode
flock.call(mode)
end
block.call(file)
end
end
thread = Thread.new { worker.perform(id) }
Timeout.timeout(5) do
expect(attempts.pop).to eq(File::LOCK_EX)
lock.close
thread.join
end
expect(worker).to have_received(:generate).with(id, hash_including('status' => 'queued'))
ensure
lock&.close unless lock&.closed?
thread&.kill
thread&.join
end
it 'keeps the generation lock reserved even when stale metadata expires' do
create_job
lock = File.open(StaticMirrorJob.directory.join('.generation.lock'), File::RDWR)
lock.flock(File::LOCK_EX)
travel 61.seconds do
travel 80.seconds do
post '/static_mirror/jobs', params: { locale: 'ja' }, as: :json
expect(response).to have_http_status(:too_many_requests)
end
+93 -2
ファイルの表示
@@ -2,6 +2,7 @@ require 'rails_helper'
RSpec.describe StaticMirror do
include_context 'Ainu locale'
include ActiveSupport::Testing::TimeHelpers
let(:tag) { create(:tag, primary_name: 'mirror-public-tag') }
let(:post) { create(:post, title: '<img src=x onerror=alert(1)>') }
@@ -72,7 +73,8 @@ RSpec.describe StaticMirror do
.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 })
'data_at' => mirror.data_at, 'data_at_basis' => 'capture_started_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'])
@@ -101,7 +103,9 @@ RSpec.describe StaticMirror do
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)
expect(mirror.authority.fetch('publication_policy'))
.to eq('static-mirror-public-projection-1')
expect(mirror.authority.keys).not_to include('admin', 'actor_id', 'artifact_policy')
end
it 'applies later discard boundaries after a restoration' do
@@ -132,6 +136,74 @@ RSpec.describe StaticMirror do
end.to raise_error(HistoryAccess::Forbidden)
end
it 'owns the static publication contract independently of Counts' do
allow(HistoryAccess).to receive(:authority).and_raise('Counts metadata is not applicable')
allow(HistoryAccess).to receive(:authorised_artifact?).and_return(false)
archive_files(mirror.export)
expect(described_class.authorised_artifact?(mirror.authority)).to be(true)
metadata = mirror.authority.except('publication_policy')
.merge('artifact_policy' => HistoryAccess::ARTIFACT_POLICY)
expect(described_class.authorised_artifact?(metadata)).to be(false)
stub_const('HistoryAccess::ARTIFACT_POLICY', 'another-counts-policy')
expect(described_class.authorised_artifact?(mirror.authority)).to be(true)
end
it 'rejects stale projections after edits without discard, including for admins' do
create(:tag_name, :alias, tag:, name: 'mirror-old-alias')
archive_files(mirror.export)
HistoryAccess.set(user: create(:user, :admin)) do
expect(described_class.authorised_artifact?(mirror.authority)).to be(true)
TagName.find_by!(tag:, name: 'mirror-old-alias').update!(name: 'mirror-new-alias')
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
end
end
it 'rejects a deprecated tag and its names without requiring a discard' do
archive_files(mirror.export)
tag.update!(deprecated_at: Time.current)
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
files = archive_files(mirror.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 'rejects stale post content and relations without a discard' do
archive_files(mirror.export)
post.update!(title: 'mirror-corrected-title')
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
archive_files(mirror.export)
PostTag.where(post:, tag:).delete_all
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
end
it 'rejects an artifact after its locale is deprecated' do
archive_files(mirror.export)
Locale.nipponese.update!(deprecated_at: Time.current)
expect(described_class.authorised_artifact?(mirror.authority)).to be(false)
end
it 'fails if a tag is deprecated during generation' do
expect do
mirror.export do |processed, _total|
tag.update!(deprecated_at: Time.current) if processed.zero?
end
end.to raise_error(HistoryAccess::Forbidden)
end
it 'records capture entry rather than claiming the exact DB read-view time' do
freeze_time do
capture_at = Time.current.utc.iso8601(6)
allow(CountsExporter).to receive(:with_read_snapshot).and_wrap_original do |original, &block|
travel 5.seconds
original.call(&block)
end
files = archive_files(mirror.export)
manifest = JSON.parse(files.fetch('manifest.json'))
expect(manifest).to include('data_at' => capture_at, 'data_at_basis' => 'capture_started_at')
expect(Time.iso8601(manifest.fetch('generated_at'))).to be > Time.iso8601(capture_at)
end
end
it 'bounds output and never reports hidden population counts' do
stub_const('StaticMirror::RESOURCE_LIMIT', 1)
second = create(:post, title: 'mirror-beyond-limit')
@@ -148,6 +220,25 @@ RSpec.describe StaticMirror do
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"')
index = JSON.parse(files.fetch('search-index.json'))
entry = index.find { _1.fetch('path') == "ain-Kana/posts/#{ post.id }/index.html" }
expect(entry.fetch('names')).to include('mirror-public-tag', 'mirror-alias')
end
it 'indexes only names of public linked tags within the output limit' do
other = create(:tag, primary_name: 'mirror-unlinked-tag')
snapshot_tag(other)
private_tag = create(:tag, primary_name: 'mirror-private-tag')
snapshot_tag(private_tag, event: 'discard')
PostTag.create!(post:, tag: private_tag)
files = archive_files(mirror.export)
index = JSON.parse(files.fetch('search-index.json'))
entry = index.find { _1.fetch('path') == "ja/posts/#{ post.id }/index.html" }
expect(entry.fetch('names')).to include('mirror-public-tag')
expect(entry.fetch('names')).not_to include('mirror-private-tag', 'mirror-unlinked-tag')
stub_const('StaticMirror::RESOURCE_LIMIT', 1)
expect(archive_files(mirror.export).fetch('search-index.json'))
.not_to include('mirror-unlinked-tag', 'mirror-private-tag')
end
it 'fails rather than truncating registered aliases at the name limit' do