#!/usr/bin/env bash set -euo pipefail usage() { cat <<'EOF' Использование: cookie_check.sh EOF } url="" if [[ $# -eq 0 ]]; then usage >&2 exit 2 fi while [[ $# -gt 0 ]]; do case "$1" in -h|--help) usage; exit 0 ;; -*) echo "Неизвестный параметр: $1" >&2; usage >&2; exit 2 ;; *) if [[ -n "$url" ]]; then echo "Укажите только один URL" >&2 exit 2 fi url="$1" shift ;; esac done if [[ ! "$url" =~ ^https?:// ]]; then echo "Нужен URL с http или https" >&2 exit 2 fi if ! command -v curl >/dev/null 2>&1; then echo "Не найден curl" >&2 exit 2 fi tmp="$(mktemp)" trap 'rm -f "$tmp"' EXIT if ! curl -sS -D "$tmp" -o /dev/null --max-time 15 "$url"; then echo "Не удалось получить заголовки" >&2 exit 1 fi cookies="$(grep -i '^Set-Cookie:' "$tmp" | sed 's/^[^:]*:[[:space:]]*//' | tr -d '\r' || true)" if [[ -z "$cookies" ]]; then echo "Set-Cookie не найден" exit 0 fi total=0 issues=0 https=0 [[ "$url" == https://* ]] && https=1 while IFS= read -r cookie; do [[ -z "$cookie" ]] && continue total=$((total + 1)) name="$(printf '%s' "$cookie" | cut -d= -f1 | xargs)" secure="нет" http_only="нет" same_site="нет" printf '%s' "$cookie" | grep -qi '; *Secure' && secure="да" printf '%s' "$cookie" | grep -qi '; *HttpOnly' && http_only="да" printf '%s' "$cookie" | grep -qi '; *SameSite=' && same_site="да" printf 'Cookie %s: Secure=%s, HttpOnly=%s, SameSite=%s\n' "$name" "$secure" "$http_only" "$same_site" if [[ "$https" -eq 1 && "$secure" == "нет" ]]; then issues=$((issues + 1)) fi if [[ "$http_only" == "нет" ]]; then issues=$((issues + 1)) fi if [[ "$same_site" == "нет" ]]; then issues=$((issues + 1)) fi done < <(printf '%s\n' "$cookies") echo "Cookie: $total, замечаний: $issues" if (( issues > 0 )); then exit 1 fi