Skip to content

Commit 320a999

Browse files
authored
Don't insert content script on chrome:// pages when autofill is enabled (#1214)
* Fix #1203 * copy even if autofill fails * remove unnecessary dependency * address review comments * fix
1 parent 6f2c492 commit 320a999

File tree

7 files changed

+57
-76
lines changed

7 files changed

+57
-76
lines changed

package-lock.json

Lines changed: 18 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"@types/uuid": "^3.4.9",
3535
"@typescript-eslint/eslint-plugin": "^2.34.0",
3636
"@typescript-eslint/parser": "^2.34.0",
37-
"@vue/runtime-dom": "^3.2.47",
3837
"@vue/test-utils": "^1.1.1",
3938
"base64-loader": "^1.0.0",
4039
"buffer": "^6.0.3",
@@ -60,7 +59,7 @@
6059
"util": "^0.12.5",
6160
"vue-loader": "^15.10.1",
6261
"vue-svg-loader": "^0.16.0",
63-
"vue-template-compiler": "^2.7.0",
62+
"vue-template-compiler": "^2.7.16",
6463
"webpack": "^5.11.0",
6564
"webpack-cli": "^5.0.0",
6665
"webpack-merge": "^5.0.0"
@@ -73,7 +72,7 @@
7372
"qrcode-generator": "^1.4.4",
7473
"qrcode-reader": "^1.0.4",
7574
"uuid": "^3.4.0",
76-
"vue": "^2.6.12",
75+
"vue": "^2.7.16",
7776
"vue2-dragula": "^2.5.4",
7877
"vuex": "^3.4.0"
7978
}

src/background.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { Encryption } from "./models/encryption";
33
import { EntryStorage, ManagedStorage } from "./models/storage";
44
import { Dropbox, Drive, OneDrive } from "./models/backup";
55
import * as uuid from "uuid/v4";
6-
import { getSiteName, getMatchedEntries, getCurrentTab } from "./utils";
6+
import {
7+
getSiteName,
8+
getMatchedEntries,
9+
getCurrentTab,
10+
okToInjectContentScript,
11+
} from "./utils";
712
import { CodeState } from "./models/otp";
813

914
import { getOTPAuthPerLineFromOPTAuthMigration } from "./models/migration";
@@ -456,7 +461,7 @@ chrome.commands.onCommand.addListener(async (command: string) => {
456461
}
457462

458463
tab = await getCurrentTab();
459-
if (tab.id) {
464+
if (okToInjectContentScript(tab)) {
460465
await chrome.scripting.executeScript({
461466
target: { tabId: tab.id },
462467
files: ["/dist/content.js"],
@@ -473,7 +478,7 @@ chrome.commands.onCommand.addListener(async (command: string) => {
473478

474479
case "autofill":
475480
tab = await getCurrentTab();
476-
if (tab.id) {
481+
if (okToInjectContentScript(tab)) {
477482
await chrome.scripting.executeScript({
478483
target: { tabId: tab.id },
479484
files: ["/dist/content.js"],

src/components/Popup/AddMethodPage.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</template>
1515
<script lang="ts">
1616
import Vue from "vue";
17-
import { getCurrentTab } from "../../utils";
17+
import { getCurrentTab, okToInjectContentScript } from "../../utils";
1818
export default Vue.extend({
1919
methods: {
2020
showInfo(page: string) {
@@ -33,7 +33,7 @@ export default Vue.extend({
3333
3434
// Insert content script
3535
const tab = await getCurrentTab();
36-
if (tab.id) {
36+
if (okToInjectContentScript(tab)) {
3737
await chrome.scripting.executeScript({
3838
target: { tabId: tab.id },
3939
files: ["/dist/content.js"],

src/components/Popup/EntryComponent.vue

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import { mapState } from "vuex";
9090
import * as QRGen from "qrcode-generator";
9191
import { OTPEntry, OTPType, CodeState, OTPAlgorithm } from "../../models/otp";
9292
import { EntryStorage } from "../../models/storage";
93-
import { getCurrentTab } from "../../utils";
93+
import { getCurrentTab, okToInjectContentScript } from "../../utils";
9494
9595
import IconMinusCircle from "../../../svg/minus-circle.svg";
9696
import IconRedo from "../../../svg/redo.svg";
@@ -223,13 +223,12 @@ export default Vue.extend({
223223
if (this.$store.state.menu.useAutofill) {
224224
await insertContentScript();
225225
const tab = await getCurrentTab();
226-
if (!tab || !tab.id) {
227-
return;
226+
if (tab && tab.id) {
227+
chrome.tabs.sendMessage(tab.id, {
228+
action: "pastecode",
229+
code: entry.code,
230+
});
228231
}
229-
chrome.tabs.sendMessage(tab.id, {
230-
action: "pastecode",
231-
code: entry.code,
232-
});
233232
}
234233
235234
const lastActiveElement = document.activeElement as HTMLElement;
@@ -296,8 +295,8 @@ function getQrUrl(entry: OTPEntry) {
296295
}
297296
298297
async function insertContentScript() {
299-
const tab = await getCurrentTab();
300-
if (tab.id) {
298+
let tab = await getCurrentTab();
299+
if (okToInjectContentScript(tab)) {
301300
await chrome.scripting.executeScript({
302301
target: { tabId: tab.id },
303302
files: ["/dist/content.js"],

src/components/Popup/MainHeader.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<script lang="ts">
7575
import Vue from "vue";
7676
import { mapState } from "vuex";
77-
import { getCurrentTab } from "../../utils";
77+
import { getCurrentTab, okToInjectContentScript } from "../../utils";
7878
7979
// Icons
8080
import IconCog from "../../../svg/cog.svg";
@@ -160,7 +160,7 @@ export default Vue.extend({
160160
161161
const tab = await getCurrentTab();
162162
// Insert content script
163-
if (tab.id) {
163+
if (okToInjectContentScript(tab)) {
164164
await chrome.scripting.executeScript({
165165
target: { tabId: tab.id },
166166
files: ["/dist/content.js"],

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,20 @@ export async function getCurrentTab() {
143143
const [tab] = await chrome.tabs.query(queryOptions);
144144
return tab;
145145
}
146+
147+
interface TabWithIdAndURL extends chrome.tabs.Tab {
148+
id: number;
149+
url: string;
150+
}
151+
152+
export function okToInjectContentScript(
153+
tab: chrome.tabs.Tab
154+
): tab is TabWithIdAndURL {
155+
return (
156+
tab.id !== undefined &&
157+
tab.url !== undefined &&
158+
(tab.url.startsWith("https://") ||
159+
tab.url.startsWith("http://") ||
160+
tab.url.startsWith("file://"))
161+
);
162+
}

0 commit comments

Comments
 (0)