Kawiesh

Api notez

Oct 6th, 2021 (edited)
1,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <div class="main">
  2. There are tons of web sites with public API that we could
  3. access, but most of them use the default CORS security
  4. setting (by not setting <b>Access-Control-Allow-Origin</b>)
  5. which means we can only access their API from the
  6. command line or from the server and not from the browser
  7. <a href="https://code-maven.com/cors">[1]</a>.
  8.  
  9. <hr>
  10. So you can only use an API from your browser if
  11. the API has Access-Control-Allow-Origin enabled.
  12.  
  13. <hr>
  14. CORS proxies let you bypass the security restrictions that CORS applies, with just a tiny change of URL.
  15. Rather than the browser sending a request to the target server directly, it sends the request to a CORS proxy with the target URL, which might look like https://corsproxy.example/https://other.example (using the target URL as a path).
  16. The CORS proxy then forwards the request to the real server, and then returns the response plus the correct CORS headers.
  17.  
  18. From the browser's point of view, any request via the proxy is just a request to the proxy's origin which does seem to support CORS. It's not aware you're talking to the real target address at all.
  19. Of course, this only works for publicly accessible sites, which the CORS proxy can directly access from wherever it's hosted.
  20.  
  21. Caution:
  22. The CORS proxy can read and do anything with the full request & response of all traffic through it .
  23. Don't use public proxies for for sensitive data exchange
  24. <a href="https://httptoolkit.tech/blog/cors-proxies/">[2]</a>.
  25.  
  26.  
  27. Same-Origin Policy
  28. a different domain (eg. site at example.com calls api.com)
  29. a different sub domain (eg. site at example.com calls api.example.com)
  30. a different port (eg. site at example.com calls example.com:3001)
  31. a different protocol (eg. site at https://example.com calls http://example.com)
  32.  
  33.  
  34.  
  35. Cross-Origin Resource Sharing (CORS)
  36. CORS is a mechanism which gets triggered, when you make a request that does not comply to the rules mentioned above.
  37. It protects your data and the data on the site you’re calling.
  38. For example, CORS can prevent attackers that use malicious scripts on websites/ads
  39. (and try to access your bank credentials and make transactions on your behalf).
  40.  
  41.  
  42. Access-Control-Allow-Origin
  43. This header is meant to be returned by the server.
  44. It indicates what client-domains are allowed to access its resources.
  45. The value can be: * (allow any domain) or specific qualified domain
  46. Sometimes the lack of proper headers is result of wrong client-side implementation (eg. missing API key).
  47. <a href="https://medium.com/@baphemot/understanding-cors-18ad6b478e2b">[3]</a>
  48.  
  49. Fixing CORS "error"
  50. Local fix
  51. Use an extension to temporary make your browser ignore the CORS mechanism
  52. <a href="https://chrome.google.com/webstore/detail/cors-unblock/lfhmikememgdcahcdlaciloancbhjino">Chrome extension</a>
  53. <a href="https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/">Firefox addon</a>
  54. These will disable the mechanism for every website for the duration of your whole browser session. Use with caution.
  55.  
  56. Durable fix
  57. Use a CORS proxy
  58. •https://api.codetabs.com/v1/proxy?quest=
  59. •https://api.allorigins.win/raw?url=
  60. •https://cors.krishan.workers.dev/?
  61. •https://cors.kawiesh.workers.dev/?
  62.  
  63.  
  64.  
  65.  
  66. //------------
  67. //----------DOM PARSER
  68. **assuming d is retrieved from r.text()
  69. let document= new DOMParser().parseFromString(d,"text/html");
  70.  
  71.  
  72.  
  73. ------
  74.  
  75.  
  76. //POST
  77. fetch("/path/to/server", {
  78.   method:"POST"
  79. , body:new URLSearchParams("[email protected]&password=pw")
  80. })
  81.  
  82.  
  83. let options= {
  84. method: "POST",
  85. body: new URLSearchParams(`key=${value}`)
  86. }
  87.  
  88.  
  89. let formData = new FormData();
  90. formData.append('name', 'John');
  91. formData.append('password', 'John123');
  92. let options= {
  93. method: "POST",
  94. body: formData
  95. }
  96.  
  97.  
  98. fetch(url, options)
  99. .then(r=> r.json())
  100. .then(d=> {
  101. //Do sth
  102. })
  103. .catch(x=> alert(x));
  104.  
  105.  
  106.  
  107.  
  108. //------FETCH WITH FALLBACK
  109.  
  110. let url= ".....";
  111.  
  112. fetch(url)
  113. .then(r=> r.clone().json().catch(() => r.text()))
  114. .then(d=> {
  115. //DO SOMETHING
  116. }).catch(x=> alert(x));
Add Comment
Please, Sign In to add comment