Unable to disable cookies by setting HTTPCookieStorage's cookieAcceptPolicy to .never

swift
(Daniil Subbotin) #1

I use HTTPCookieStorage class to insert my own HTTPCookie. I noticed that if I set cookieAcceptPolicy to .never then cookies still writes to storage. Why?

let someCookie = HTTPCookie(properties: [
    .name: "cookie",
    .value: "value",
    .path: "/",
    .domain: "domain.com",
    .expires: Date(timeIntervalSinceNow: 60)
])!

let storage = HTTPCookieStorage.shared
storage.cookieAcceptPolicy = .never
print(storage.cookies?.count) // 0
storage.setCookie(someCookie)
print(storage.cookies?.count) // 1

As you can see setCookie works even if I set cookieAcceptPolicy to .never

I also looked at the HTTPCookieStorage implementation:


Why
guard cookieAcceptPolicy != .never else { return }
doesn't work?

1 Like
(Quinn “The Eskimo!”) #2

The cookie accept policy controls whether the store accepts cookies from the ‘wire’. It does not control the behaviour of cookies set programmatically.

I also looked at the HTTPCookieStorage implementation:

That code only runs on non-Apple platfroms. On Apple platforms, Swift Foundation calls through to Foundation which calls through to CFNetwork, which has a completely different implementation. IMO this is a bug in that code, and I’d appreciate you filing it as such

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple