الانتقال إلى المحتوى
الأمان (Security)Secure Coding· 8 دقائق للقراءة

الـ CORS مش سور أمان: بيحمي إيه ومش بيحمي إيه

الـ CORS بيقول للمتصفحات أنهي مواقع تانية مسموح لها تقرا الـ responses بتاعتك. لكن السيرفر لسه بيرد على الكل، فالـ access control لازم يكون جوه الـ API نفسها.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

مراجعة لشريك شغل بتطلع بمفاجأة: اللي بيراجع نزّل ليستة الأسعار الداخلية بأمر curl واحد. الـ endpoint مفيهوش login، لأن الفريق كان شايفه محمي: الـ CORS policy مش بتسمح غير لـ https://shop.example. وعشان يثبت ده، developer بينادي نفس الـ endpoint من صفحة على domain تاني، والـ console في المتصفح بيطبع "blocked by CORS policy". النتيجتين حقيقيتين. بس ولا واحدة فيهم معناها اللي الفريق فاكره.

الفكرة الغلط الشائعة

الفكرة بتقول إن الـ CORS زي firewall على السيرفر للـ origins. تكتب الـ origins المسموحة، وأي request من حد تاني بيترفض، فالـ API مش بترد غير على الـ front end بتاعك.

والأدلة شكلها مقنع. نادي الـ API من موقع تاني، والـ console يقولك إن الـ request اتمنع. والـ policy قاعدة في Program.cs جنب الـ authentication، ورا method اسمها WithOrigins، وtutorials كتير بتوصفها إنها بتحدد مين يقدر يوصل للـ API بتاعك. وكمان أخطاء الـ CORS وجع دماغ يومي وانت بتطوّر، فبتحس إن الـ CORS بوّاب صارم. بس المنع ده بيحصل في المتصفح، بعد ما السيرفر بتاعك يكون رد خلاص.

اللي بيحصل فعلًا

السيرفر بيرد، والمتصفح بيقرر مين يقرا

دوكيومنتيشن الـ CORS بتاع Microsoft بيقولها على طول: "It's up to the client (browser) to enforce CORS. The server executes the request and returns the response, it's the client that returns an error and blocks the response." واحنا جرّبنا بـ app صغير على ASP.NET Core 10 الـ policy بتاعته مش بتسمح غير لـ https://shop.example، وناديناه بـ 4 طرق:

text
Client                              Status  CORS allow header     Bodycurl, no Origin                     200     none                  readcurl, Origin: https://evil.example  200     none                  readcurl, Origin: https://shop.example  200     https://shop.example  readChromium 156, page on other origin  200     none                  blocked

الـ log بتاع السيرفر بيورّي إن الـ handler اشتغل ورجّع 200 في المرات الأربعة. المتصفح بس هو اللي رفض يدّي الـ body للصفحة، بـ TypeError: Failed to fetch ورسالة في الـ console بتقول "No 'Access-Control-Allow-Origin' header is present on the requested resource."

الـ CORS بيخفف الـ same-origin policy، مش بيبني سور

الـ هي القاعدة الحقيقية في المتصفح، وMDN بتوصف شكلها: "Cross-origin writes are typically allowed. Examples are links, redirects, and form submissions." في حين إن "Cross-origin reads are typically disallowed". والـ هو الـ opt-in اللي بيخلّيك تعمل استثناءات للقراية. وعشان كده Microsoft بتوصفه إنه "Is not a security feature, CORS relaxes security. An API is not safer by allowing CORS."

وفيه requests بتتفحص الأول. للـ methods والـ headers اللي form عادية ماتقدرش تعملها، المتصفح بيبعت بـ OPTIONS ويستنى موافقة. لكن الـ request اللي شكله زي form submission بيطلع من غير ما يستأذن. وMDN بتوضّح إن السيرفر "still must opt-in using Access-Control-Allow-Origin to share the response with the script"، يعني ده بيتحكم في القراية، مش في إن الـ request يوصل ولا لأ.

الـ Origin ادعاء من المتصفح، مش هوية للـ client

جوه المتصفح، الـ Fetch Standard بيخلّي Origin من الـ forbidden headers "so the user agent remains in full control" عليه، وعشان كده بيساعد ضد الـ CSRF. لكن برّه المتصفح هو header عادي: الـ curl بتاعنا ادّعى إنه https://shop.example ورجع له الـ allow header. الـ CORS مايقدرش يفرّق بين الـ front end بتاعك وأي script، وهو أصلًا مش معمول لكده. والـ Fetch Standard بيقول إن Access-Control-Allow-Origin: * على resource عام بيشاركه مع الـ browser APIs "much like it is already shared with curl and wget."

الـ CORS بيحمي إيه فعلًا: الداتا اللي بتوصلها من خلال المستخدمين

الـ Fetch Standard بيشرح ليه الـ CORS لازم يبقى opt-in: "to prevent leaking data from responses behind a firewall (intranets). Additionally, for requests including credentials it needs to be opt-in to prevent leaking potentially-sensitive data." يعني الخطر الحقيقي عكس الفكرة الغلط بالظبط: policy مفتوحة زيادة عن اللزوم. لو بترجّع أي origin زي ما هو وبتسمح بالـ credentials، أي موقع المستخدم يفتحه وهو عامل login يقدر يقرا داتاه بالـ cookies بتاعته. والـ standard بيسمّي الكومبينيشن ده "rather unsafe"، والـ OWASP Top 10 2025 بيعدّ "CORS misconfiguration allows API access from unauthorized or untrusted origins" تحت A01 Broken Access Control.

حط الـ access control جوه الـ API

C# Program.cs
var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication().AddCookie();builder.Services.AddAuthorization();builder.Services.AddSingleton<PriceList>().AddSingleton<OrderStore>();builder.Services.AddCors(o => o.AddDefaultPolicy(p => p    .SetIsOriginAllowed(_ => true) // added to silence CORS errors    .AllowAnyHeader().AllowAnyMethod().AllowCredentials()));var app = builder.Build();app.UseCors();app.UseAuthentication();app.UseAuthorization();// "Only our front end can call this: CORS is configured"app.MapGet("/api/prices/internal", (PriceList prices) => prices.Internal());// Any site can now read this with the visitor's cookieapp.MapGet("/api/me/orders", (ClaimsPrincipal user, OrderStore orders) =>    orders.ForUser(user.Identity!.Name!)).RequireAuthorization();app.Run();

النسخة المصلّحة بتفصل بين الشغلانتين. الـ RequireAuthorization بيقرر مين مسموح له ينادي الـ endpoint، وبيشتغل مع أي client. أما الـ CORS فبيقرر بس أنهي صفحات ويب تقدر تقرا الرد جوه المتصفح، فبيكتب الـ origin الوحيد اللي محتاجه. وMicrosoft بتسمّي الفخ اللي في النسخة الغلط: "Bypassing the built-in checks by using SetIsOriginAllowed(_ => true) together with AllowCredentials is also an insecure configuration."

السؤالالـ CORS بيجاوبه؟مين اللي بيجاوبه
script على موقع تاني يقدر يقرا الـ response ده؟أيوه، في المتصفحاتالـ CORS headers
الـ curl أو Postman أو سيرفر يقدر ينادي الـ endpoint ده؟لأالـ authentication والـ authorization
الـ handler بيشتغل لـ origin مش مسموح؟أيوه، بيشتغل عاديالـ authorization جوه الـ API
موقع تاني يقدر يخلّي متصفح المستخدم يبعت form POST؟لأالـ CSRF tokens والـ SameSite cookies
صاحب الـ request ده مسموح له يشوف الـ record ده بالذات؟لأchecks للـ authorization على مستوى الـ object

تطبقها إزاي في مشروعك

  1. افترض إن أي client يقدر ينادي أي endpointحط authentication و authorization على كل endpoint مش معمول إنه يبقى عام. والـ CORS مش بيغيّر الليستة دي.
  2. خلّي الـ policy ليستة محددةاكتب كل origin مسموح باسمه. ممنوع ترجّع الـ origin زي ما هو، وممنوع * مع credentials، والـ wildcard subdomains بس لو بتثق في كل subdomain.
  3. اسمح بالـ credentials بس لما الـ front end يحتاجهالو الـ front end بينادي الـ API بـ bearer token، يبقى مش محتاج AllowCredentials، ولما تشيلها بتشيل خطر الـ cookies.
  4. حمّل الـ origins من الـ configurationخلّي لكل environment ليستة لوحدها، عشان localhost عمره ما يوصل للـ production، ومحدش يوسّع الـ policy عشان يحل error في الـ dev.
  5. اختبر الطريقين في الـ testsنادي كل endpoint محمي بـ curl من غير credentials، ومن صفحة على origin مش مسموح. الاتنين لازم يفشلوا، كل واحد لسبب مختلف.

الخلاصة

  • الـ CORS بيتحكم في أنهي صفحات ويب تقدر تقرا الـ responses بتاعتك جوه المتصفح، مش في مين يقدر ينادي الـ API.
  • السيرفر بينفّذ الـ request في الحالتين، والمتصفح بس هو اللي بيمنع الـ response عن الصفحة.
  • الـ clients اللي مش متصفحات مش بتلتزم بالـ CORS، وتقدر تبعت أي Origin header هي عايزاه.
  • الـ access control الحقيقي هو الـ authentication والـ authorization، على السيرفر، في كل request.
  • الـ CORS policy المفتوحة مع credentials هي الحالة الخطيرة، فخلّي الـ origins المسموحة محددة بالظبط.

📚 المصادر

  1. Cross-Origin Resource Sharing (CORS) — MDN Web Docs
  2. Same-origin policy — MDN Web Docs
  3. Fetch Standard, §3.3 CORS protocol — WHATWG
  4. Enable Cross-Origin Requests (CORS) in ASP.NET Core — Microsoft Learn
  5. A01:2025 Broken Access Control — OWASP Top 10:2025

التعليقات (0)