Skip to content

Commit

Permalink
fix: prevent in-memory sessions from writing to custom spellchecker d…
Browse files Browse the repository at this point in the history
…ictionary (#22157)

* fix: prevent in-memory sessions from writing to custom dictionary

* docs

* spec
  • Loading branch information
erickzhao authored and we452366 committed Nov 16, 2020
1 parent fe107ea commit 1214234
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docs/api/session.md
Expand Up @@ -538,15 +538,17 @@ Resolves when the full dictionary is loaded from disk.

* `word` String - The word you want to add to the dictionary

Returns `Boolean` - Whether the word was successfully written to the custom dictionary.
Returns `Boolean` - Whether the word was successfully written to the custom dictionary. This API
will not work on non-persistent (in-memory) sessions.

**Note:** On macOS and Windows 10 this word will be written to the OS custom dictionary as well

#### `ses.removeWordFromSpellCheckerDictionary(word)`

* `word` String - The word you want to remove from the dictionary

Returns `Boolean` - Whether the word was successfully removed from the custom dictionary.
Returns `Boolean` - Whether the word was successfully removed from the custom dictionary. This API
will not work on non-persistent (in-memory) sessions.

**Note:** On macOS and Windows 10 this word will be removed from the OS custom dictionary as well

Expand Down
12 changes: 12 additions & 0 deletions shell/browser/api/electron_api_session.cc
Expand Up @@ -868,6 +868,12 @@ v8::Local<v8::Promise> Session::ListWordsInSpellCheckerDictionary() {
}

bool Session::AddWordToSpellCheckerDictionary(const std::string& word) {
// don't let in-memory sessions add spellchecker words
// because files will persist unintentionally
bool is_in_memory = browser_context_->IsOffTheRecord();
if (is_in_memory)
return false;

SpellcheckService* service =
SpellcheckServiceFactory::GetForContext(browser_context_.get());
if (!service)
Expand All @@ -883,6 +889,12 @@ bool Session::AddWordToSpellCheckerDictionary(const std::string& word) {
}

bool Session::RemoveWordFromSpellCheckerDictionary(const std::string& word) {
// don't let in-memory sessions remove spellchecker words
// because files will persist unintentionally
bool is_in_memory = browser_context_->IsOffTheRecord();
if (is_in_memory)
return false;

SpellcheckService* service =
SpellcheckServiceFactory::GetForContext(browser_context_.get());
if (!service)
Expand Down
7 changes: 7 additions & 0 deletions spec-main/spellchecker-spec.ts
Expand Up @@ -105,6 +105,13 @@ describe('spellchecker', () => {
const wordList = await ses.listWordsInSpellCheckerDictionary
expect(wordList).to.have.length(0)
})

// remove API will always return false because we can't add words
it('should fail for non-persistent sessions', async () => {
const tempSes = session.fromPartition('temporary')
const result = tempSes.addWordToSpellCheckerDictionary('foobar')
expect(result).to.equal(false)
})
})

describe('ses.removeWordFromSpellCheckerDictionary', () => {
Expand Down

0 comments on commit 1214234

Please sign in to comment.