# Irule for disabling CORS functionality.

**URL:** <https://community.f5.com/t/irule-for-disabling-cors-functionality/54101>\
**Category:** Technical Forums\
**Tags:** irules, big-ip, security\
**Created:** [August 16, 2021, 2:28pm UTC](https://community.f5.com/t/irule-for-disabling-cors-functionality/54101 "2021-08-16T14:28:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZANOOB](https://avatars.discourse-cdn.com/v4/letter/z/a6a055/32.png) [@ZANOOB](https://community.f5.com/u/ZANOOB)\
**Post date:** [August 16, 2021, 2:28pm UTC](https://community.f5.com/t/irule-for-disabling-cors-functionality/54101/1 "2021-08-16T14:28:54Z")

</div>

Hello All,

Hope you all doing good. Wondering if you would have a answer for to a CORS issue.

I am trying to disable CORS for an web app that we have published and we can see it is failing to load because of CROS error.

I even tried disabling ASM policy from the virtual server , still getting the CROS error on the browser.

I am trying to figure out if the iRule for disabling CORS will work or not. However, when i create the irule mentioned in the article ([CORS implementation - Community CodeShare - DevCentral - An F5 Technical Community](https://devcentral.f5.com/s/articles/cors-implementation)) I get an error on F5 .

Is there an issue with allowed\_origins as class ,since i get the error :

01070151:3: Rule [/Common/Allowed\_CROS] error: /Common/Allowed\_CROS:2: error: [command is not valid in the current scope][class allowed\_origins {

“.example.com”

“.example2.com”

“.goodpartner.com”

}]

1. # Domains that are allowed to make cross-domain calls to [example.com](https://example.com/)
2. class allowed\_origins {
3. “.[example.com](https://example.com/)”
4. “.[example2.com](https://example2.com/)”
5. “.[goodpartner.com](https://goodpartner.com/)”
6. }
7. when HTTP\_REQUEST {
8. unset -nocomplain cors\_origin
9. if { [class match [HTTP::header Origin] ends\_with allowed\_origins] } {
10. if { ( [HTTP::method] equals “OPTIONS” ) and ( [HTTP::header exists “Access-Control-Request-Method”] ) } {
11. # CORS preflight request - return response immediately
12. HTTP::respond 200 “Access-Control-Allow-Origin” [HTTP::header “Origin”] \
13. “Access-Control-Allow-Methods” [HTTP::header “Access-Control-Request-Method”] \
14. “Access-Control-Allow-Headers” [HTTP::header “Access-Control-Request-Headers”] \
15. “Access-Control-Max-Age” “86400” \
16. “Vary” “Origin”
17. } else {
18. # CORS GET/POST requests - set cors\_origin variable
19. set cors\_origin [HTTP::header “Origin”]
20. }
21. }
22. }
23. when HTTP\_RESPONSE {
24. # CORS GET/POST response - check cors\_origin variable set in request
25. if { [info exists cors\_origin] } {
26. HTTP::header insert “Access-Control-Allow-Origin” $cors\_origin
27. HTTP::header insert “Access-Control-Allow-Credentials” “true”
28. HTTP::header insert “Vary” “Origin”
29. }
30. }

---

<div class="post-metadata">

**Author:** ![samstep](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@samstep](https://community.f5.com/u/samstep)\
**Post date:** [September 15, 2021, 1:33pm UTC](https://community.f5.com/t/irule-for-disabling-cors-functionality/54101/2 "2021-09-15T13:33:54Z")

</div>

you should remove this text from your irule:

1. class allowed\_origins {
2. “.[example.com](https://example.com/)”
3. “.[example2.com](https://example2.com/)”
4. “.[goodpartner.com](https://goodpartner.com/)”
5. }

^-this was an example of a datagroup called “allowed\_origins” which you should create (easiest way is to do it via the F5 GUI):

Local Traffic → iRules-\>DataGroups and instead of “.example.com” you should put your domain (mind the first dot which is needed in this case)
