Skip to content

Conversation

@ClickAndGoScript
Copy link

This pull request introduces several critical security enhancements to the application's backend and frontend.
The primary goal is to harden the platform against common web vulnerabilities, specifically Cross-Site
Request Forgery (CSRF) and Clickjacking, while also improving cookie management policies.

Key Changes

  • Origin Validation Middleware:

    • A new validateOrigin middleware has been implemented to verify the Origin header for all incoming
      requests to protected admin routes (/api/admin).
    • This feature can be enabled via the validate_Origin setting. When active, it ensures that only requests
      from the application's own domain or a configurable list of whitelisted domains (allowed_origins) are
      processed. This is a fundamental defense against CSRF attacks.
  • Clickjacking Protection with Content-Security-Policy (CSP):

    • A cspFrameAncestorsMiddleware has been added to set the Content-Security-Policy header on server
      responses.
    • The frame_ancestors_domains setting allows administrators to specify a space-separated list of domains
      that are permitted to embed the application within an <iframe>. This effectively prevents Clickjacking
      attacks.
  • Secure HTTP Method for Message Deletion:

    • The API endpoint for deleting a message (/api/admin/delete-message/{id}) has been changed from GET to
      POST.
    • This aligns with security best practices, which dictate that state-changing operations should not be
      performed via GET requests.
    • The admin.service.ts on the frontend has been updated accordingly to use the POST method for this
      action.
  • Enhanced Cookie Security:

    • The SameSite attribute for session cookies is now configurable via the COOKIE_SAMESITE_POLICY
      environment variable.
    • Supported values are Lax (default), Strict, and None. The application automatically sets the Secure
      flag when None is used, as required by modern browsers. This provides granular control over when
      cookies are sent, mitigating cross-site information leakage.
  • Updated Documentation and Configuration:

    • The SET.md file has been updated to document the new security settings.
    • The sample.env file now includes the COOKIE_SAMESITE_POLICY variable as an example.

How to Test

  1. Origin Validation:

    • In the admin panel, set validate_Origin to 1.
    • Perform an admin action (e.g., delete a message) from the web interface. The action should succeed.
    • Attempt to send the same request from an external tool (e.g., Postman) without a matching Origin
      header. The request should be rejected with a 403 Forbidden error.
  2. Iframe (Clickjacking) Protection:

    • Set frame_ancestors_domains to a specific domain (e.g., https://example.com).
    • Create a test page on that domain and embed the application in an <iframe>. The application should load
      successfully.
    • Attempt to embed the application from any other domain. The browser should block it from rendering
      inside the frame.
  3. Message Deletion:

    • Log in as a user with the appropriate privileges.
    • Navigate to the chat and delete a message.
    • Verify that the message is deleted and that the network request made was a POST request.

AMAARETS added 2 commits October 28, 2025 19:23
…on and frame protection

- Add middleware to validate request origins based on configuration
- Implement Content Security Policy (CSP) for frame ancestors
- Update backend routes to support new security middleware
- Add configuration options for allowed origins and frame domains
- Modify delete message route from GET to POST for better security
- Update frontend admin service to use POST for message deletion
- Add SameSite cookie policy configuration in session management
- Update sample environment configuration with new security settings
Improves application security by providing more granular control over request origins, frame embedding, and cookie policies.
if r.TLS != nil {
scheme = "https"
}
appHostOrigin := scheme + "://" + r.Host
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ואם התוקף משנה גם את זה?
לכאורה צריך להעביר באמצעות משתנה סביבה את הדומיין ברירת מחדל שמאושר, אחרת גם יש מצב שמוגדרת הבדיקה הזאת ובפועל אין דומיין מוגדר ונשארים נעולים מחוץ למערכת.
https://www.calhoun.io/csrf-protection-via-headers-in-go-125/?utm_source=chatgpt.com
תראה גם את זה.
https://github.com/NetFree-Community/TheChannel/blob/master/backend/main.go#L19
בכל אופן, עדיף לשלב את הבדיקה הזאת כאן, בלי לעשות פונקציה נפרדת על זה.

Copy link
Author

@ClickAndGoScript ClickAndGoScript Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

את מה התוקף ישנה? את הHost?
זה נשלח ע"י הדפדפן, אי אפשר לשנות את זה.

אין שום אפשרות להינעל מחוץ למערכת, כי הדומיין של האתר עצמו תמיד מאושר בצורה קשיחה בקוד

ההגנה שביצעתי היא כמו מה שמופיע במדריך שקישרת, ומה שיש שם בגירסה 1.25 לא רלוונטי.

הפונקציה הנפרדת זה כיון שזה בדיקה נוספת ומיותרת בעיקרון, שמופעלת רק אם הפעילו אותה בהגדרות, ואין צורך לבלגן את הפונקצית התחברות הרגילה.

@Baruch055
Copy link
Contributor

יש מצב שמוגדרת הבדיקה הזאת ובפועל אין דומיין מוגדר ונשארים נעולים מחוץ למערכת.

....

@ClickAndGoScript
Copy link
Author

יש מצב שמוגדרת הבדיקה הזאת ובפועל אין דומיין מוגדר ונשארים נעולים מחוץ למערכת.

ראה עריכה בתגובה הקודמת. הדומיין של האתר עצמו תמיד מאושר.

@ynigun
Copy link

ynigun commented Dec 29, 2025

למה צריך אופציה בכלל לבצע בקשות מדומיין אחר?
למה לא תמיד להגביל את העוגיות רק לדומיין בו הוא נוצר (דהיינו תמיד להגדיר SameSiteStrictMode)?

@ClickAndGoScript
Copy link
Author

על מנת לאפשר טעינה של הערוצים בi frame שהוא חוצה דומיינים.
לדוגמה בפרוייקט הזה https://thechannel-viewer.clickandgo.cfd/

@ClickAndGoScript
Copy link
Author

לא, כיון שצריך לאפשר התחברות ע"י גוגל, שזה אפשרי רק ע"י שיתוף העוגיות.

@ynigun
Copy link

ynigun commented Dec 29, 2025

צריך להבהיר שאם אתה נותן הרשאות לדומיין הוא יכול לעשות כל פעולה באתר שלך
במצב שיש כרגע כל אתר יכול לבצע פעולות באתר (כי אין שום מגבלות כרגע)

@ynigun
Copy link

ynigun commented Dec 29, 2025

כל פעולה באתר שלך

אמנם אי אפשר לעשות כל פעולה באופן ישיר אבל בשילוב XSS אפשר לקבל שליטה מלאה

@ClickAndGoScript
Copy link
Author

לעשות פעולות ישירות באתר אי אפשר כמובן בגלל CROS.
וגם כל הסיכון הוא נמוך מאוד בהתחשב בכך שמדובר באתר שלרוב המשתמשים אין בו כלל הרשאות, והעוגיות שלהם משמשות לזיהוי בלבד.
הסיכון לחדירה לחשבון מנהל ע"י XSS הוא אפסי.

וכמובן גם הענין ניתן להגדרה בקובץ המשתני סביבה.
מבירור עם מנהלי ערוצים כולם מעוניינים בזה למרות הסיכון המזערי שבענין.

@DVORI9031
Copy link

DVORI9031 commented Dec 29, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants