仰望星辰工作室

miaopanel

miaopanel/ scripts/smoke-files.sh 2.9 KB · 52 行 原始文件
1#!/usr/bin/env bash
2# File manager smoke test executed on the target host.
3set -uo pipefail
4ENTRY="${1:?usage: smoke-files.sh <entry>}"
5BASE="http://127.0.0.1:8888/$ENTRY"
6PASS=$(awk -F': ' '/^password:/{print $2}' /www/server/panel/data/initial_password.txt)
7
8curl -s -c /tmp/ck.txt -H 'Content-Type: application/json' -H 'Origin: http://127.0.0.1:8888' \
9 -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/tmp/login.out
10CSRF=$(python3 -c "import json;print(json.load(open('/tmp/login.out'))['data']['csrf'])")
11H=(-b /tmp/ck.txt -H "X-CSRF-Token: $CSRF" -H 'Content-Type: application/json' -H 'Origin: http://127.0.0.1:8888')
12
13echo "=== 1. 列目录 ==="
14curl -s -b /tmp/ck.txt "$BASE/api/v1/files?path=/" |
15 python3 -c "import json,sys;d=json.load(sys.stdin)['data'];print('root:',d['root']);print('entries:',[e['name'] for e in d['entries']])"
16
17echo "=== 2. 新建目录 + 写入 + 读取 ==="
18curl -s "${H[@]}" -d '{"path":"/_mptest"}' "$BASE/api/v1/files/mkdir"; echo
19curl -s "${H[@]}" -X PUT -d '{"path":"/_mptest/a.txt","content":"hello-miaopanel"}' "$BASE/api/v1/files/write"; echo
20curl -s -b /tmp/ck.txt "$BASE/api/v1/files/read?path=/_mptest/a.txt"; echo
21
22echo "=== 3. 上传 / 下载 ==="
23printf 'uploaded-by-smoke' > /tmp/up.txt
24curl -s -b /tmp/ck.txt -H "X-CSRF-Token: $CSRF" -H 'Origin: http://127.0.0.1:8888' \
25 -F "file=@/tmp/up.txt" "$BASE/api/v1/files/upload?dir=/_mptest"; echo
26curl -s -b /tmp/ck.txt -o /tmp/down.txt -w 'download http=%{http_code} ' "$BASE/api/v1/files/download?path=/_mptest/up.txt"
27echo "content=$(cat /tmp/down.txt) owner=$(stat -c '%U:%G' /www/wwwroot/_mptest/a.txt)"
28
29echo "=== 4. 压缩 / 解压 ==="
30curl -s "${H[@]}" -d '{"path":"/_mptest","format":"zip"}' "$BASE/api/v1/files/compress"; echo
31curl -s "${H[@]}" -d '{"path":"/_mptest.zip","dest":"/_mpout"}' "$BASE/api/v1/files/extract"; echo
32curl -s -b /tmp/ck.txt "$BASE/api/v1/files/read?path=/_mpout/_mptest/a.txt"; echo
33
34echo "=== 5. 回收站:删除 / 列表 / 恢复 ==="
35DEL=$(curl -s "${H[@]}" -X DELETE "$BASE/api/v1/files?path=/_mpout")
36echo "$DEL"
37ID=$(echo "$DEL" | python3 -c "import json,sys;print(json.load(sys.stdin)['data']['id'])")
38curl -s -b /tmp/ck.txt "$BASE/api/v1/files/trash" | head -c 200; echo
39curl -s "${H[@]}" -d "{\"id\":\"$ID\"}" "$BASE/api/v1/files/trash/restore"; echo
40curl -s -b /tmp/ck.txt -o /dev/null -w 'restored read http=%{http_code}\n' "$BASE/api/v1/files/read?path=/_mpout/_mptest/a.txt"
41
42echo "=== 6. 路径穿越(应全部拒绝) ==="
43for p in '%2F..%2F..%2Fetc%2Fpasswd' '%2F.%2E%2Fetc%2Fpasswd' '%2F_mptest%2F..%2F..%2Fetc'; do
44 printf '%-32s -> %s\n' "$p" "$(curl -s -o /dev/null -w '%{http_code}' -b /tmp/ck.txt "$BASE/api/v1/files/read?path=$p")"
45done
46
47echo "=== 7. 清理 ==="
48for p in /_mptest /_mptest.zip /_mpout; do
49 curl -s -o /dev/null "${H[@]}" -X DELETE "$BASE/api/v1/files?path=$p"
50done
51curl -s -o /dev/null "${H[@]}" -X DELETE "$BASE/api/v1/files/trash"
52ls /www/wwwroot/ | paste -sd' '