Browse Source

fix: remove port number in Google OAuth callback URL

close #1571
Gerald 3 years ago
parent
commit
9b3549ce83
2 changed files with 8 additions and 1 deletions
  1. 4 0
      src/background/sync/base.js
  2. 4 1
      src/background/sync/googledrive.js

+ 4 - 0
src/background/sync/base.js

@@ -609,6 +609,10 @@ export async function openAuthPage(url, redirectUri) {
   unregister = () => {
     browser.webRequest.onBeforeRequest.removeListener(handler);
   };
+  // Note: match pattern does not support port number
+  // - In Chrome, the port number is ignored and the pattern still works
+  // - In Firefox, the pattern is ignored and won't match any URL
+  redirectUri = redirectUri.replace(/:\d+/, '');
   browser.webRequest.onBeforeRequest.addListener(handler, {
     // Do not filter by tabId here, see above
     urls: [`${redirectUri}*`],

+ 4 - 1
src/background/sync/googledrive.js

@@ -1,6 +1,6 @@
 // Reference:
+// - https://developers.google.com/identity/protocols/oauth2/native-app
 // - https://developers.google.com/drive/v3/reference/files
-// - https://github.com/google/google-api-nodejs-client
 import { getUniqId, noop } from '#/common';
 import { objectGet } from '#/common/object';
 import { loadQuery, dumpQuery } from '../utils';
@@ -14,6 +14,9 @@ import {
 const config = {
   client_id: process.env.SYNC_GOOGLE_DESKTOP_ID,
   client_secret: process.env.SYNC_GOOGLE_DESKTOP_SECRET,
+  // We use native app approach with code challenge for better security.
+  // Google OAuth for native app only allows loopback IP address for callback URL.
+  // The URL will be intercepted and blocked so the port doesn't matter.
   redirect_uri: 'http://127.0.0.1:45678/',
   // redirect_uri: 'https://violentmonkey.github.io/auth_googledrive.html',
   scope: 'https://www.googleapis.com/auth/drive.appdata',