-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance SRI script robustness and refine Restaurant docs #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,28 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #!/bin/bash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculate_sri() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url="$1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hash=$(curl -sL "$url" | openssl dgst -sha384 -binary | openssl base64 -A) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| temp_file=$(mktemp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # curl options: -f (fail on HTTP error), -S (show error), -s (silent equivalent), -L (follow redirects) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ! curl -fS -sL "$url" -o "$temp_file"; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "Error downloading $url" >&2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rm -f "$temp_file" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Check for empty response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ ! -s "$temp_file" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "Error: Empty response from $url" >&2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rm -f "$temp_file" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hash=$(openssl dgst -sha384 -binary < "$temp_file" | openssl base64 -A) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "$url sha384-$hash" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rm -f "$temp_file" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一時ファイルのクリーンアップに Line 23 の 🛡️ trap を使った修正案 calculate_sri() {
url="$1"
temp_file=$(mktemp)
+ trap 'rm -f "$temp_file"' RETURN
# curl options: -f (fail on HTTP error), -S (show error), -s (silent equivalent), -L (follow redirects)
if ! curl -fS -sL "$url" -o "$temp_file"; then
echo "Error downloading $url" >&2
- rm -f "$temp_file"
return 1
fi
# Check for empty response
if [ ! -s "$temp_file" ]; then
echo "Error: Empty response from $url" >&2
- rm -f "$temp_file"
return 1
fi
hash=$(openssl dgst -sha384 -binary < "$temp_file" | openssl base64 -A)
echo "$url sha384-$hash"
- rm -f "$temp_file"
}
📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calculate_sri "https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: myoshi2891/Algorithm-DataStructures-Math-SQL
Length of output: 948
set -euo pipefailの追加により、厳格なシェルモードが有効になりました。-eオプションによるエラー発生時の即座の終了は、スクリプトの堅牢性を向上させます。Line 28 のcalculate_sriが失敗した場合、スクリプト全体が終了し Line 29 の呼び出しが実行されないのは、エラー時の適切なフェイルファーストの動作です。ただし、重要な問題として、Line 23 のパイプライン内で
opensslコマンドが失敗した場合、set -eにより即座にスクリプトが終了するため、Line 25 のrm -f "$temp_file"が実行されず、一時ファイルがクリーンアップされないままになります。 この問題を解決するには、関数内で以下のようなトラップを設定してください:このトラップを関数の先頭に追加することで、予期しない終了時にも一時ファイルが確実にクリーンアップされます。
🤖 Prompt for AI Agents