diff --git a/6-data-storage/01-cookie/article.md b/6-data-storage/01-cookie/article.md index 2d1161e34..1e6a8e929 100644 --- a/6-data-storage/01-cookie/article.md +++ b/6-data-storage/01-cookie/article.md @@ -1,58 +1,55 @@ # Cookies, document.cookie -Cookies are small strings of data that are stored directly in the browser. They are a part of HTTP protocol, defined by [RFC 6265](https://tools.ietf.org/html/rfc6265) specification. +Çerezler, doğrudan tarayıcıda depolanan küçük veri dizileridir. Çerezler [RFC 6265](https://tools.ietf.org/html/rfc6265) teknik şartnamesi tarafından tanımlanan HTTP protokolünün bir parçasıdırlar. -Most of the time, cookies are set by a web server. Then they are automatically added to every request to the same domain. +Çoğu zaman, çerezler bir web sunucusu tarafından ayarlanır. Daha sonra aynı etki alanına yapılan her isteğe otomatik olarak eklenirler. -One of the most widespread use cases is authentication: +En yaygın kullanım alanlarından biri kimlik doğrulamadır: -1. Upon sign in, the server uses `Set-Cookie` HTTP-header in the response to set a cookie with "session identifier". -2. Next time when the request is set to the same domain, the browser sends the over the net using `Cookie` HTTP-header. -3. So the server knows who made the request. +1. Oturum açıldığında, sunucu "session identifier" içeren bir çerez ayarlamak için, gönderilen isteğe verdiği yanıtta `Set-Cookie` HTTP başlığını kullanır. +2. Gelecek sefere istek aynı etki alanından yapıldığında, tarayıcı `Cookie` HTTP-header başlağını kullanarak ağ üzerinden gönderir. +3. Böylece sunucu isteğin kim tarafından yapıldığını bilir. -We can also access cookies from the browser, using `document.cookie` property. +Ayrıca `document.cookie` özelliğini kullarak çerezelere tarayıcıdan da erişebiliriz. -There are many tricky things about cookies and their options. In this chapter we'll cover them in detail. +Çerezler ve seçenekleri hakkında birçok ince detay var. Bu bölümde bunları ayrıntılı olarak ele alacağız. ## Reading from document.cookie ```online -Do you have any cookies on this site? Let's see: +Bu sitede hiç çerezin var mı? Hadi görelim: ``` ```offline -Assuming you're on a website, it's possible to see the cookies, like this: +Bir web sitesinde olduğunuzu varsayalım, çerezleri şu şekilde görmek mümkündür: ``` ```js run -// At javascript.info, we use Google Analytics for statistics, -// so there should be some cookies +// javascript.info sitesinde, biz istatistikler için Google Analytics kullanırız, +// bu yüzden bazı çerezler olmalı alert( document.cookie ); // cookie1=value1; cookie2=value2;... ``` +`document.cookie` değeri `; ` ile ayrılmış `name=value` çiftlerinden oluşur. Her biri ayrı bir çerezdir. +Bellirli bir çerezi bulmak için, `document.cookie` yi `; ` ile ayırabiliriz ve sonra doğru ismi bulabiliriz. Ayrıca bunu yapmak için düzenli ifadeler (regular expresion) veya dizi methodlarını da kullanabiliriz. -The value of `document.cookie` consists of `name=value` pairs, delimited by `; `. Each one is a separate cookie. +Bunu okuyucu için bir egseriz olarak bırakıyoruz. Ayrıca, bu bölümün sonunda yardımcı fonksiyonlar ve üzerinde değişiklik yapabileceğiniz çerezler bulacaksınız. -To find a particular cookie, we can split `document.cookie` by `; `, and then find the right name. We can use either a regular expression or array functions to do that. +## document.cookie' ye yazma -We leave it as an exercise for the reader. Also, at the end of the chapter you'll find helper functions to manipulate cookies. +`document.cookie` ye yazabilir. Ancak bu bir veri özelliği değildir, bu bir erişimdir. -## Writing to document.cookie +**`document.cookie` yazılan bir işlemi, tarayıcında sayesinde belirttiğimiz çerezleri günceller fakat bu, diğer çerezleri etkilemez.** -We can write to `document.cookie`. But it's not a data property, it's an accessor. - -**A write operation to `document.cookie` passes through the browser that updates cookies mentioned in it, but doesn't touch other cookies.** - -For instance, this call sets a cookie with the name `user` and value `John`: +Örneğin, bu ismi `user` ve değeri `John` olan bir çerezi ayarlar : ```js run -document.cookie = "user=John"; // update only cookie named 'user' -alert(document.cookie); // show all cookies +document.cookie = "user=John"; // sadece 'user' isimli çerezi günceller +alert(document.cookie); // tüm çerezleri göster ``` +Eğer komutu çalıştırırsanız, muhtemelen birden fazla çerez göreceksiniz. Bunun nedeni `document.cookie=` işlem tüm çerezleri etkilemediği içindir. Sadece `user` adlı çerezi değiştirir. -If you run it, then probably you'll see multiple cookies. That's because `document.cookie=` operation does not overwrite all cookies. It only sets the mentioned cookie `user`. - -Technically, name and value can have any characters, but to keep the formatting valid they should be escaped using a built-in `encodeURIComponent` function: +Tekinik olarak, isim ve değer herhangi bir karakter içerebilir, fakat geçerli bir atama yapmak için `encodeURIComponent` yerleşik fonksiyonu kullanılmalıdır: ```js run // special values, need encoding @@ -65,16 +62,16 @@ document.cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value); alert(document.cookie); // ...; my%20name=John%20Smith ``` - ```warn header="Limitations" -There are few limitations: -- The `name=value` pair, after `encodeURIComponent`, should not exceed 4kb. So we can't store anything huge in a cookie. -- The total number of cookies per domain is limited to around 20+, the exact limit depends on a browser. +Birkaç kısıtlama vardır: +- `encodeURIComponent` fonksiyonunu kullanırken `name=value` çifti 4kb yi geçmemelidir. Yani br çerezde çok büyük bir değer tutulamaz. +- +- Kesin sınır tarayıcının türüne bağlı olmakla birlilte, etki alanı başına toplam çerez sayısı 20+ ile sınırlıdır. ``` -Cookies have several options, many of them are important and should be set. +Çerezlerin birkaç seçeneği vardır, bunların çoğu önemli ve ayarlanması gerekir. -The options are listed after `key=value`, delimited by `;`, like this: +Seçenekler `key=value` ile belirtilmiş ve `;` ile ayrılmış, aşağıdaki gibi listelenir: ```js run document.cookie = "user=John; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT" @@ -84,59 +81,59 @@ document.cookie = "user=John; path=/; expires=Tue, 19 Jan 2038 03:14:07 GMT" - **`path=/mypath`** -The url path prefix, where the cookie is accessible. Must be absolute. By default, it's the current path. +Çerezin URL öneki ulaşılabilir olmalıdır. Tam olmalıdır. Varsayılan olarak, geçerli yoldur. -If a cookie is set with `path=/admin`, it's visible at pages `/admin` and `/admin/something`, but not at `/home` or `/adminpage`. +Eğer bir çerez `path=/admin` olarak ayarlandıysa, bu çerez `/admin` ve `/admin/something` sayfalarında görülebilir, ancak `/home` ya da `/adminpage` sayfalarında görünmez. -Usually, we set `path=/` to make the cookie accessible from all website pages. +Genelde, biz çerezin tüm web sayfalarında erişebilir olması için `path=/` şeklinde ayarlarız. ## domain - **`domain=site.com`** -A domain where the cookie is accessible. In practice though, there are limitations. We can't set any domain. +Çerezlere etki alanı üzerinden erişilebilir. Ancak pratikte bazı kısıtlamalar vardır. Bu çerezleri herhangi bir etki alanına ayarlayamayız. + +Varsayılan olarak, bir çerez sadece onu ayarladığımız etki alanından erişilebilir. Yani, eğer çerez `site.com` etki alanına ayarlanmışsa, biz ona `other.com` etki alanından erişemeyiz. -By default, a cookie is accessible only at the domain that set it. So, if the cookie was set by `site.com`, we won't get it `other.com`. +...Ancak daha ilginç olanı, `forum.site.com` alt etki alnından da çerezlere erişilemez. -...But what's more tricky, we also won't get the cookie at a subdomain `forum.site.com`! ```js -// at site.com +// diyelim ki site.com alan adlı sitede şöyle bir çerez ataması yapılmış olsun document.cookie = "user=John" -// at forum.site.com -alert(document.cookie); // no user +// forum.site.com alt etki alanından aynı çereze erişmeye çalıştığımızda çıktı aşağıdaki gibi olur +alert(document.cookie); // kullanıcı yok ``` -**There's no way to let a cookie be accessible from another 2nd-level domain, so `other.com` will never receive a cookie set at `site.com`.** +**Bir çerezin başka 2.seviye bir etki alanından erişilebilmesini sağlamanın bir yolu yok, bu nedenle `other.com` sitesi hiçbir zaman `site.com` sitesinde ayarlanmış bir çereze erişemeyeektir.** -It's a safety restriction, to allow us to store sensitive data in cookies. +Bunun sebebi, önemli verileri çerezlerde saklamamıza izin veren bir günvelik sınırlamasıdır. -...But if we'd like to grant access to subdomains like `forum.site.com`, that's possible. We should explicitly set `domain` option to the root domain: `domain=site.com`: +...Ancak `forum.site.com` gibi alt alan adlarına erişim izni vermek istiyorsak, bu mümkündür. Bunun için `domain` seçeneğini açıkça `domain=site.com` seçeneğine ayarlamamız gerekiyor: ```js -// at site.com, make the cookie accessible on any subdomain: +// site.com etki alnında, herhangi bir alt etki alanına şöyle ayarlayabiliriz: document.cookie = "user=John; domain=site.com" -// at forum.site.com -alert(document.cookie); // with user +// forum.site.com alt alanından çerezlere erişelim +alert(document.cookie); // çıktı: kullanıcılar ``` +Geçmişten gelen nedenlerden dolayı, `domain=.site.com` (başında bir nokta ile) şeklinde de çalışır, çok eski tarayıcıları desteklemek içn noktayı eklemek daha iyi olabilir. -For historical reasons, `domain=.site.com` (a dot at the start) also works this way, it might better to add the dot to support very old browsers. - -So, `domain` option allows to make a cookie accessible at subdomains. +Dolayısıyla, `domain` seçeneği, çerezlere alt alan adlarından da erişmeyi izin verir. ## expires, max-age -By default, if a cookie doesn't have one of these options, it disappears when the browser is closed. Such cookies are called "session cookies" +Varsayılan olarak, eğer bir çerez bu seçeneklerden birine sahip değilse, tarayıcı kapatıldığında çerezler de yok olur. Bu tür çerezlere "session cookies" denir. -To let cookies survive browser close, we can set either `expires` or `max-age` option. +Tarayıcı kapatıldığında bile çerezlerin yok olmasını engellemek için `expires` ya da `max-age` seçeneklerinden birini ayarlamak gerekir. - **`expires=Tue, 19 Jan 2038 03:14:07 GMT`** -Cookie expiration date, when the browser will delete it automatically. +Bu örnekte, tarayıcının 19 Ocak 2038 e kadar çerezi otomatik olarak tutumasını ve süresi dolunca silmesini sağlar. -The date must be exactly in this format, in GMT timezone. We can use `date.toUTCString` to get it. For instance, we can set the cookie to expire in 1 day: +Tarih, kesinlikle GMT zaman dilimi formatında olmalı. Bu formatı elde etmek için `date.toUTCString` methodunu kullanabiliriz. Örneğin, çerezi 1 gün sonra yok olacak şekilde ayarlabiliriz: ```js // +1 day from now @@ -144,20 +141,19 @@ let date = new Date(Date.now() + 86400e3); date = date.toUTCString(); document.cookie = "user=John; expires=" + date; ``` - -If we set `expires` to a date in the past, the cookie is deleted. +Eğer çerezin `expires` seçeneğini geçmişteki bir tarihe ayarlarsak, çerez silinir. - **`max-age=3600`** -An alternative to `expires`, specifies the cookie expiration in seconds from the current moment. +`expires` seçeneğine alternatif olarak, çerezi geçerli andan itibaren saniye türünden yok olmasını belirtir. -If zero or negative, then the cookie is deleted: +Eğer saniye, sıfır ya da negatif bir sayı olursa, çerez silinir. ```js -// cookie will die +1 hour from now +// çerez ayarlandığı zamandan bir saat sonra silinir. document.cookie = "user=John; max-age=3600"; -// delete cookie (let it expire right now) +// çerezi sil (çerezin süresinin sona ermesine izin ver) document.cookie = "user=John; max-age=0"; ``` @@ -165,14 +161,13 @@ document.cookie = "user=John; max-age=0"; - **`secure`** -The cookie should be transferred only over HTTPS. +Çerez sadece HTTPS üzerinden gönderilmelidir. -**By default, if we set a cookie at `http://site.com`, then it also appears at `https://site.com` and vice versa.** +**Varsayılan olarak, eğer `http://site.com` sitesi üzerinden bir çerez ayarlarsak, bu aynı zamanda `https://site.com` sitesi üzerinden görünür ve tersi de mümkündür.** -That is, cookies are domain-based, they do not distinguish between the protocols. - -With this option, if a cookie is set by `https://site.com`, then it doesn't appear when the same site is accessed by HTTP, as `http://site.com`. So if a cookie has sensitive content that should never be sent over unencrypted HTTP, then the flag is the right thing. +Yani, çerezler etki alanı tabanlıdır, protokoller arasında ayırım yapmaz. +Bu seçenekle beraber, eğer bir çerez hassas bilgiler içeriyorsa, durum değişir. Yani bir çerez `https://site.com` etki alanına ayarlamışsa, bu çereze `http://site.com` üzerinden erişem mümkün değildir, çünkü HTTP ile HTTPS arasında S günvelik flagı vardır. Bu da çerezlere erişilmesini engeller. Sonuç olarak hassas bilgilere sahip çerezleriniz varsa HTTPS protokülünü kullanmanız daha doğru olur. ```js // assuming we're on https:// now // set the cookie secure (only accessible if over HTTPS) @@ -181,107 +176,110 @@ document.cookie = "user=John; secure"; ## samesite -That's another security option, to protect from so-called XSRF (cross-site request forgery) attacks. +Bu, XSRF (siteler arası sahte istek) saldırılarından korunmak için başka bir güvenlik seçeneğidir. -To understand when it's useful, let's introduce the following attack scenario. +Bu seçeneğin ne zaman işimize yarayacağını anlamak için aşağıdaki senaryoya bakalım. ### XSRF attack -Imagine, you are logged into the site `bank.com`. That is: you have an authentication cookie from that site. Your browser sends it to `bank.com` with every request, so that it recognizes you and performs all sensitive financial operations. +`bank.com` sitesine giriş yaptığınızı düşünün. Yani: bu siteden bir tane kimlik doğrulama çereziniz var. Tarayıcınızla `bank.com` sitesine her giriş yaptığınızda, tarayıcınız bu çerezi `bank.com` sitesinin bulunduğu sunucuya gönderir, böylece `bank.com` sitesi sizi tanır ve tüm hassas finansal işlemlerinizi gerçekleştirir. + +Şimdi, başka bir sekmede internette gezinirken (`evil.com`), bu sitede (`evil.com`) de bilgisayar korsanına ait bir giriş hesabı var ve bu site zaman zaman