From 121423415d86e516452148cc4fb1076c3745077b Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Tue, 10 Mar 2020 00:45:43 -0700 Subject: [PATCH] fix: prevent in-memory sessions from writing to custom spellchecker dictionary (#22157) * fix: prevent in-memory sessions from writing to custom dictionary * docs * spec --- docs/api/session.md | 6 ++++-- shell/browser/api/electron_api_session.cc | 12 ++++++++++++ spec-main/spellchecker-spec.ts | 7 +++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/api/session.md b/docs/api/session.md index 8d82a77a4b8aa..4b4495592f33d 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -538,7 +538,8 @@ 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 @@ -546,7 +547,8 @@ Returns `Boolean` - Whether the word was successfully written to the custom dict * `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 diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 25c83b1249f3f..c297258fdd831 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -868,6 +868,12 @@ v8::Local 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) @@ -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) diff --git a/spec-main/spellchecker-spec.ts b/spec-main/spellchecker-spec.ts index 8af3fbc26d61a..4c26afbda94b3 100644 --- a/spec-main/spellchecker-spec.ts +++ b/spec-main/spellchecker-spec.ts @@ -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', () => {