Tuesday, November 19, 2019

CORS là gì?

Trong quá trình đi làm, nhất là nếu làm frontend thì chắc chắn sẽ có một lúc nào đó các bạn bị Chrome (trình duyệt nói chung) chửi vô mặt những thứ đại loại liên quan đến cái của nợ Access-Control-Allow-Origin header.
Tôi đã từng nghĩ đại đa số các bạn làm Web developer đều hiểu cái này rồi nhưng sự thật không hẳn là như vậy. Sau khi tổ chức một cuộc điều tra nho nhỏ, thì có vẻ như các bạn backend hiểu về CORS nhiều hơn là frontend. Và có kha khá bạn không hiểu, hay hiểu một cách mơ hồ, không thể giải thích được về CORS.
Và vì thế, tada, tôi viết bài viết này để lỡ chẳng may có bạn nào chưa hiểu thì cũng có thể vớt vát được chút xíu sau khi đọc xong!

Sinh ra CORS chi cho phức tạp vậy?

Mọi việc trên đời này xảy ra đều có nguồn cơn của nó và cái của nợ CORS cũng không ngoại lệ.
Đầu tiên phải nói đến same origin policy, đây là một security concept quan trọng được hiện thực trên các thể loại trình duyệt nhằm ngăn chặn JavaScript code có thể tạo ra những request đến những nguồn khác với nguồn mà nó được trả về (ví dụ đơn giản như request tới những domain khác), và cách để so sánh same origin được mô tả ở đây, tuy nhiên để nói một cách đơn giản thì domain sẽ phải giống nhau từ đầu tới cuối từ protocol đến host, port.
Tại sao việc này nguy hiểm, thì các bạn cứ nghĩ đơn giản, nếu các bạn vô Facebook, trong khi đó ở một tab khác các bạn mở một trang web chứa mã độc. Tab Facebook sử dụng JavaScript để request lên server, nếu không có same origin policy, JavaScript ở web chứa mã độc kia cũng có thể tạo request lên server của Facebook với resource của tab Facebook, vì thế trình duyệt phải có cơ chế để phân biệt JavaScript của nguồn nào thì được access vào resource của nguồn nào.
Okay, đó là cách để bảo vệ người dùng, tuy nhiên thì nó gây ra không ít hạn chế cho việc giao tiếp giữa client và server mặc dù chúng đều là những nguồn có thể tin tưởng được (ví dụ client là portal.codeaholicguy.com và server là api.codeaholicguy.com, hai domain này chắc chắn là không same origin rồi).
CORS (hay nói một cách giông dài là Cross-Origin Resource Sharing) là một kĩ thuật được sinh ra để làm cho việc tương tác giữa client và server được dễ dàng hơn, nó cho phép JavaScript ở một trang web có thể tạo request lên một REST API được host ở một domain khác.

Cơ chế hoạt động của CORS như thế nào?

Trong trường hợp đơn giản nhất, phía client (tức là cái web app chạy ở browser đó) sẽ tạo request GET, POST, PUT, HEAD, etc để yêu cầu server làm một việc gì đó. Những request này sẽ được đính kèm một header tên là Origin để chỉ định origin của client code (giá trị của header này chính là domain của trang web).
Server sẽ xem xét Origin để biết được nguồn này có phải là nguồn hợp lệ hay không. Nếu hợp lệ, server sẽ trả về response kèm với header Access-Control-Allow-Origin. Header này sẽ cho biết xem client có phải là nguồn hợp lệ để browser tiếp tục thực hiện quá trình request.
Trong trường hợp thông thường, Access-Control-Allow-Origin sẽ có giá trị giống như Origin, một số trường hợp giá trị của Access-Control-Allow-Origin sẽ nhìn giống giống như Regex hay chỉ đơn giản là *, tuy nhiên thì cách dùng * thường được coi là không an toàn, ngoại trừ trường hợp API của bạn được public hoàn toàn và ai cũng có thể truy cập được.
Và như thế, nếu không có header Access-Control-Allow-Origin hoặc giá trị của nó không hợp lệ thì browser sẽ chửi vào mặt chúng ta.

Vẫn chưa hết đâu, các bạn phải biết về Pre-flight requests nữa!

Khi bạn thực hiện những request ảnh hưởng tới data như POST, PUT, DELETE, etc thì browser sẽ tự động thưc hiện một request gọi là preflight request trước khi thực sự thực hiện request để kiểm tra xem phía server đã thực hiện CORS hay chưa, cũng như để biết được rằng request này có hợp lệ hay không. Ngoài ra thì nếu bạn có thêm những custom header vào trong request thì việc gửi một preflight request cũng là cần thiết.
Preflight request được gửi lên server với dạng là OPTIONS (đây là lý do tại sao khi bạn debug ở client bạn thường thấy có hai request giống nhau nhưng khác request method, một cái là OPTIONS một cái là method thật sự bạn muốn gửi).
Ví dụ bạn muốn gửi request DELETE lên server. Browser sẽ tự tạo một request OPTIONS sẽ hỏi xem server có cho phép việc gửi request DELETE hay không. Nếu server cho phép, nó sẽ gửi về response đính kèm những header như Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Max-Age, etc.
  • Access-Control-Allow-Methods: mô tả những method nào client có thể gửi đi.
  • Access-Control-Max-Age: mô tả thời gian hợp lệ của preflight request, nếu quá hạn, browser sẽ tự tạo một preflight request mới.
Sau đó browser sẽ có thể gửi request DELETE và nhận response như bình thường. Và ngược lại, browser sẽ chửi vào mặt bạn.

Kết luận

Và đó là tất cả những gì tôi biết về CORS, hy vọng nó sẽ giúp ích được cho các bạn, cũng như lý giải được thắc mắc của một số bạn về CORS từ trước đến giờ.
Nếu các bạn có góp ý gì thêm hay chủ đề nào hay ho mà các bạn muốn tôi viết thì đừng quên để lại cho tôi một lời nhắn nho nhỏ ở phần comment bên dưới nhé.
Tạm biệt.
Một số nguồn các bạn có thể tham khảo thêm:
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Advertisements
REPORT THIS AD
REPORT THIS AD

20 thoughts on “CORS là gì?”

  1. Ví dụ e đàng dùng nodejs nếu e không dùng package CORS thì request trang khác không có quyền truy cập đúng ko anh còn nếu e có sữ dụng thì những trang khác có quyền truy cập đúng ko anh
  2. Em chào anh!
    Phần cơ chế hoạt động của CORS? Em ko hiểu chỗ: ” …để chỉ định origin của client code”, anh giải thích cho em đoạn này với ạ!
  3. Chào anh , cho em hỏi sao em lại bị xóa comments vậy ạ. em có vào để xem anh trả lời thì thấy bị anh xóa. anh cho hỏi hỏi lí do được không ạ. Câu hỏi của em là dù có truy vấn từ site khác nhưng đâu có thể gửi kèm cookie từ trang khách thì đâu có nguy hiểm như đoạn nay a nói. “JavaScript ở web chứa mã độc kia cũng có thể tạo request lên server của Facebook với resource của tab Facebook, “
    1. sao lại có nhỉ lại hiện lên rồi . sorry chắc em nhầm :3 vừa rồi ctrl+ F tìm mà không thấy giờ lại có
  4. Cho em hỏi đoạn này ” Tab Facebook sử dụng JavaScript để request lên server, nếu không có same origin policy, JavaScript ở web chứa mã độc kia cũng có thể tạo request lên server của Facebook với resource của tab Facebook” . ví dụ request tới facebook.com/me thì đâu lấy được gì vì cơ bản request đó đâu có kèm cookie facebook. tương tự với các trang khác. Em có reuqest như vậy bản chất là trình duyệt trả về kết quả lỗi nhưng vẫn sẽ thực hiện
    1. Bạn hiểu đơn giản thế này. Ví dụ bạn truy cập từ trình duyệt bình thường vào api.codeaholicguy.com thì ra result còn dùng javascript truy cập từ trang portal.codeaholicguy.com tới api thì sẽ bị chặn do CORS. cách postman truy cập là giống như cách 1 đó
    2. Bạn bên trên giải thích có ý đúng đó, như a có giải thích bên trên, Browser sẽ thực hiện cơ chế preflight request để kiểm tra cors, còn với postman nó ko được implement cơ chế preflight request mà request sẽ được gọi trực tiếp luôn vì postman ko cần kiểm tra policy về cors
  5. Hi am cho me hỏi có cách nào chặn OPTIONS requests k anh, nhiều khi nó làm duplicate action mà ta đã code á. Ví dụ send mail chẳn hạn ?
  6. Chào @codeaholicguy!
    Gần đây mình có thực hành một số project trên freeCodeCamp và nhiều lần bị lỗi CORS. Và để khắc phục thì có 2 cách là:
    Sử dụng cors-anywhere: bằng cách gắn địa chỉ cần request vào sau: https://cors-anywhere.herokuapp.com/
    Sử dụng jsonp.
    Về cách sử dụng jsonp thì tạm thời mình chưa thảo luận ở đây. Còn cách sử dụng cors-anywhere thì mình vẫn chưa hiểu nguyên lý của nó cho lắm.
    Bạn có thể giải thích giúp mình lý do tại sao thằng cors-everywhere có thể hoạt động được mà không bị lỗi CORS không?
    Thanks!

Tuesday, January 15, 2019

MIME types

Multipurpose Internet Mail Extensions (MIME) type is a standard that indicates the nature and format of a document, file, or assortment of bytes. It is defined and standardized in IETF RFC 6838.
The Internet Assigned Numbers Authority (IANA) is responsible for all official MIME types, and you can find the most up-to-date and complete list at their Media Types page.
Browsers use the MIME type, not the file extension, to determine how to process a URL — it is important that servers send the correct MIME type in the response's Content-Typeheader.

SyntaxSection

General structureSection

type/subtype
A MIME type consists of a type and a subtype — two strings separated by /. No whitespace is allowed. The type represents the category and can be a discrete or a multipart type. The subtype is specific to each type.
MIME types are case-insensitive but traditionally written in lowercase.

Discrete typesSection

text/plain
text/html
text/javascript
text/css
image/jpeg
image/png
audio/mpeg
audio/ogg
audio/*
video/mp4
application/*
application/json
application/ecmascript
application/octet-stream
…
Discrete types indicate the category of the document. They can be one of the following:
TypeDescriptionExample of typical subtypes
textAny document that contains text and is theoretically human readabletext/plaintext/htmltext/csstext/javascripttext/markdown
imageAny kind of image. Videos are not included, though animated images (like animated GIF) are described with an image type.image/gifimage/pngimage/jpegimage/bmpimage/webpimage/vnd.microsoft.icon
audioAny kind of audio fileaudio/midiaudio/mpegaudio/webmaudio/oggaudio/wav
videoAny kind of video filevideo/webmvideo/ogg
applicationAny kind of binary data, especially data that will be executed or interpreted somehow.application/octet-streamapplication/pkcs12application/vnd.mspowerpointapplication/xhtml+xmlapplication/xmlapplication/pdf
For text documents without a specific subtype, text/plain should be used.
Similarly, for binary documents without a specific or known subtype, application/octet-stream should be used.

Multipart typesSection

multipart/form-data
multipart/byteranges
Multipart types indicate a category of document broken into pieces, often with different MIME types. They represent a composite document. With the exception of multipart/form-data, used in the POST method of HTML Forms, and multipart/byteranges, used with 206Partial Content to send part of a document, HTTP doesn't handle multipart documents in a special way: the message is transmitted to the browser (which will likely show a "Save As" window if it doesn't know how to display the document.)

Important MIME types for Web developersSection

application/octet-streamSection

This is the default for binary files. As it means unknown binary file, browsers usually don't execute it, or even ask if it should be executed. They treat it as if the Content-Dispositionheader was set to attachment, and propose a "Save As" dialog.

text/plainSection

This is the default for textual files. Even if it really means unknown textual file, browsers assume they can display it.
Note that text/plain does not mean any kind of textual data. If they expect a specific kind of textual data, they will likely not consider it a match. Specifically if they download a text/plain file from a <link> element declaring a CSS files, they will not recognize it as a valid CSS files if presented with text/plain. The CSS mime type text/css must be used.

text/cssSection

CSS files used to style a Web page must be sent with text/css. If a server doesn't recognize the .css suffix for CSS files, it may send them with text/plain or application/octet-stream MIME types. If so, they won't be recognized as CSS by most browsers and will be ignored.

text/htmlSection

All HTML content should be served with this type. Alternative MIME types for XHTML (like application/xhtml+xml) are mostly useless nowadays.
Note: Use application/xml or application/xhtml+xml if you want XML’s strict parsing rules, <![CDATA[…]]> sections, or elements that aren't from HTML/SVG/MathML namespaces.

text/javascriptSection

The Scripting languages section of the HTML Standard states:
Servers should use text/javascript for JavaScript resources. Servers should not use other JavaScript MIME types for JavaScript resources, and must not use non-JavaScript MIME types.
The other JavaScript MIME types that should not be used are defined in the MIME Sniffing Standard as follows:
  • application/javascript 
  • application/ecmascript 
  • application/x-ecmascript  
  • application/x-javascript  
  • text/ecmascript 
  • text/javascript1.0  
  • text/javascript1.1  
  • text/javascript1.2  
  • text/javascript1.3  
  • text/javascript1.4  
  • text/javascript1.5  
  • text/jscript  
  • text/livescript  
  • text/x-ecmascript  
  • text/x-javascript  

Image typesSection

Only a few image types are widely recognized enough to be safe for use in a Web page:
MIME typeImage type
image/gifGIF images (lossless compression, superseded by PNG)
image/jpegJPEG images
image/pngPNG images
image/svg+xmlSVG images (vector images)
image/x-iconimage/vnd.microsoft.icon[1]Windows icons
There is a discussion to add WebP (image/webp) to this list, but browser vendors are cautious in accepting it.
Other kinds of images can be found in Web documents. For example, many browsers support ICO images for favicons with the image/x-icon MIME type.
Footnote 1
Despite image/vnd.microsoft.icon being registered with IANA, it is largely unsupported, and image/x-icon is being used instead.

Audio and video typesSection

Like images, HTML doesn't define supported types for the <audio> and<video> elements, so only some can be used on the Web. Media formats supported by the HTML audio and video elements explains both the codecs and container formats which can be used.
The MIME type of audiovisual files mostly indicate the container formats. The most common ones on the Web are:
MIME typeAudio or video type
audio/wave
audio/wav
audio/x-wav
audio/x-pn-wav
An audio file in the WAVE container format. The PCM audio codec (WAVE codec "1") is often supported, but other codecs have limited support (if any).
audio/webmAn audio file in the WebM container format. Vorbis and Opus are the most common audio codecs.
video/webmA video file, possibly with audio, in the WebM container format. VP8 and VP9 are the most common video codecs; Vorbis and Opus the most common audio codecs.
audio/oggAn audio file in the OGG container format. Vorbis is the most common audio codec used in such a container.
video/oggA video file, possibly with audio, in the OGG container format. Theora is the usual video codec used within it; Vorbis is the usual audio codec.
application/oggAn audio or video file using the OGG container format. Theora is the usual video codec used within it; Vorbis is the usual audio codec.

multipart/form-dataSection

The multipart/form-data type can be used when sending the values of a completed HTML Form from browser to server.
As a multipart document format, it consists of different parts, delimited by a boundary (a string starting with a double dash --). Each part is its own entity with its own HTTP headers, Content-Disposition, and Content-Type for file uploading fields.
Content-Type: multipart/form-data; boundary=aBoundaryString
(other headers associated with the multipart document as a whole)

--aBoundaryString
Content-Disposition: form-data; name="myFile"; filename="img.jpg"
Content-Type: image/jpeg

(data)
--aBoundaryString
Content-Disposition: form-data; name="myField"

(data)
--aBoundaryString
(more subparts)
--aBoundaryString--

The following <form>:
<form action="http://localhost:8000/" method="post" enctype="multipart/form-data">
  <label>Name: <input name="myTextField" value="Test"></label>
  <label><input type="checkbox" name="myCheckBox"> Check</label>
  <label>Upload file: <input type="file" name="myFile" value="test.txt"></label>
  <button>Send the file</button>
</form>
will send this message:
POST / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Content-Type: multipart/form-data; boundary=---------------------------8721656041911415653955004498
Content-Length: 465

-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myTextField"

Test
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myCheckBox"

on
-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myFile"; filename="test.txt"
Content-Type: text/plain

Simple file.
-----------------------------8721656041911415653955004498--

multipart/byterangesSection

The multipart/byteranges MIME type is used to send partial responses to the browser.
When the 206 Partial Content status code is sent, this MIME type indicates that the document is composed of several parts, one for each of the requested ranges. Like other multipart types, the Content-Type uses a boundary to separate the pieces. Each piece has a Content-Type header with its actual type and a Content-Range of the range it represents.
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Content-Length: 385

--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 100-200/1270

eta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="vieport" content
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 300-400/1270

-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: "Open Sans", "Helvetica
--3d6b6a416f9b5--

Importance of setting the correct MIME typeSection

Most web servers send unrecognized resources as the application/octet-stream MIME type. For security reasons, most browsers do not allow setting a custom default action for such resources, forcing the user to save it to disk to use it.
Some common incorrect server configurations:
  • RAR-compressed files. In this case, the ideal would be the true type of the original files; this is often impossible as .RAR files can hold several resources of different types. In this case, configure the server to send application/x-rar-compressed.
  • Audio and video. Only resources with the correct MIME Type will be played in <video>or <audio> elements. Be sure to use the correct type for audio and video.
  • Proprietary file types. Avoid using application/octet-stream as most browsers do not allow defining a default behavior (like "Open in Word") for this generic MIME type. A specific type like application/vnd.mspowerpoint lets users open such files automatically in the presentation software of their choice.

MIME sniffingSection

In the absence of a MIME type, or in certain cases where browsers believe they are incorrect, browsers may perform MIME sniffing — guessing the correct MIME type by looking at the bytes of the resource.
Each browser performs MIME sniffing differently and under different circumstances. (For example, Safari will look at the file extension in the URL if the sent MIME type is unsuitable.) There are security concerns as some MIME types represent executable content. Servers can prevent MIME sniffing by sending the X-Content-Type-Options header.