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\x00 file" )
18+ # ActiveStorage::Filename は null バイトをそのまま保持
19+ expect ( doc . filename ) . to eq ( "test\x00 file" )
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