-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1546 from /issues/2970
protect unsafe multithreaded access to httpse cache
- Loading branch information
Showing
2 changed files
with
130 additions
and
93 deletions.
There are no files selected for viewing
114 changes: 71 additions & 43 deletions
114
components/brave_shields/browser/https_everywhere_recently_used_cache.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,85 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
/* Copyright 2016 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include <unordered_map> | ||
#ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ | ||
#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
template <class T> class RingBuffer | ||
{ | ||
private: | ||
int currentIdx = 0; | ||
int count; | ||
std::vector<T> data; | ||
public: | ||
RingBuffer(int fixedSize) : count(fixedSize), data(count) {} | ||
|
||
const T& at(int i) { | ||
return data[(currentIdx - (i % count) + count) % count]; | ||
} | ||
#include "base/synchronization/lock.h" | ||
|
||
void add(const T& newValue) { | ||
currentIdx = (currentIdx + 1) % count; | ||
data[currentIdx] = newValue; | ||
} | ||
template <class T> class RingBuffer { | ||
public: | ||
explicit RingBuffer(int fixedSize) : count(fixedSize), data(count) {} | ||
|
||
T oldest() { | ||
return data[(currentIdx + 1) % count]; | ||
} | ||
const T& at(int i) { | ||
return data[(currentIdx - (i % count) + count) % count]; | ||
} | ||
|
||
void clear() { | ||
data = std::vector<T>(count); | ||
} | ||
void add(const T& newValue) { | ||
currentIdx = (currentIdx + 1) % count; | ||
data[currentIdx] = newValue; | ||
} | ||
|
||
T oldest() { | ||
return data[(currentIdx + 1) % count]; | ||
} | ||
|
||
void clear() { | ||
data = std::vector<T>(count); | ||
} | ||
|
||
private: | ||
int currentIdx = 0; | ||
int count; | ||
std::vector<T> data; | ||
}; | ||
|
||
template <class T> class HTTPSERecentlyUsedCache | ||
{ | ||
private: | ||
RingBuffer<T> keysByAge; | ||
public: | ||
std::unordered_map<std::string, T> data; | ||
|
||
HTTPSERecentlyUsedCache(unsigned int size = 100) : keysByAge(size) {} | ||
|
||
void add(const std::string& key, const T& value) { | ||
std::string old = keysByAge.oldest(); | ||
if (!old.empty()) { | ||
keysByAge.data.erase(old); | ||
} | ||
keysByAge[key] = value; | ||
} | ||
template <class T> class HTTPSERecentlyUsedCache { | ||
public: | ||
explicit HTTPSERecentlyUsedCache(unsigned int size = 100) : keysByAge(size) {} | ||
|
||
void add(const std::string& key, const T& value) { | ||
base::AutoLock create(lock_); | ||
|
||
data_[key] = value; | ||
// https://github.com/brave/brave-browser/issues/3193 | ||
// std::string old = keysByAge.oldest(); | ||
// if (!old.empty()) { | ||
// keysByAge.data.erase(old); | ||
// } | ||
// keysByAge[key] = value; | ||
} | ||
|
||
bool get(const std::string& key, T* value) { | ||
base::AutoLock create(lock_); | ||
|
||
void clear() { | ||
data.clear(); | ||
keysByAge.clear(); | ||
auto search = data_.find(key); | ||
if (search != data_.end()) { | ||
*value = search->second; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void remove(const std::string& key) { | ||
data_.erase(key); | ||
} | ||
|
||
void clear() { | ||
data_.clear(); | ||
// https://github.com/brave/brave-browser/issues/3193 | ||
// keysByAge.clear(); | ||
} | ||
|
||
private: | ||
std::unordered_map<std::string, T> data_; | ||
base::Lock lock_; | ||
RingBuffer<T> keysByAge; | ||
}; | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_HTTPS_EVERYWHERE_RECENTLY_USED_CACHE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters