Skip to content

Commit 361a33d

Browse files
committed
テスト追加: Document モデルのファイル名サニタイズ機能
ActiveStorage::Filename#sanitized の動作を検証するテストを追加: - 基本的なファイル名処理 - パストラバーサル攻撃の防御 (../ → ..-) - バックスラッシュの変換 (\ → -) - null バイトの処理(そのまま保持される) - 許可リストとの連携確認 7 examples, 0 failures
1 parent 2653610 commit 361a33d

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

spec/models/document_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Document do
4+
describe '#initialize' do
5+
it 'ActiveStorage::Filename を使用してファイル名をサニタイズする' do
6+
doc = Document.new("test_file")
7+
expect(doc.filename).to eq("test_file")
8+
end
9+
10+
it 'パストラバーサル攻撃を防ぐ' do
11+
doc = Document.new("../../../etc/passwd")
12+
# ActiveStorage::Filename はパス区切り文字 / を - に変換
13+
expect(doc.filename).to eq("..-..-..-etc-passwd")
14+
end
15+
16+
it 'null バイトが含まれる場合もそのまま処理する' do
17+
doc = Document.new("test\x00file")
18+
# ActiveStorage::Filename は null バイトをそのまま保持
19+
expect(doc.filename).to eq("test\x00file")
20+
end
21+
22+
it 'バックスラッシュを処理する' do
23+
doc = Document.new("..\\..\\..\\windows\\system32")
24+
# バックスラッシュは - に変換される
25+
expect(doc.filename).to eq("..-..-..-windows-system32")
26+
end
27+
end
28+
29+
describe '#exist?' do
30+
it '存在するドキュメントの場合 true を返す' do
31+
allow(Document).to receive(:all).and_return([
32+
double(filename: 'existing')
33+
])
34+
doc = Document.new('existing')
35+
expect(doc.exist?).to be true
36+
end
37+
38+
it '存在しないドキュメントの場合 false を返す' do
39+
allow(Document).to receive(:all).and_return([
40+
double(filename: 'existing')
41+
])
42+
doc = Document.new('nonexistent')
43+
expect(doc.exist?).to be false
44+
end
45+
46+
it 'サニタイズ後のファイル名で許可リストをチェックする' do
47+
allow(Document).to receive(:all).and_return([
48+
double(filename: '..-..-..-etc-passwd')
49+
])
50+
doc = Document.new('../../../etc/passwd')
51+
# サニタイズ後の名前で存在チェック
52+
expect(doc.exist?).to be true
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)