Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(google-docs): escape Drive searches and stop loading every folder (#16450)

Talal Jaberc083f04bba6a
diff --git a/bun.lock b/bun.lock
index d703a850322..26076bcac2b 100644
--- a/bun.lock
+++ b/bun.lock
@@ -3918,7 +3918,7 @@
},
"packages/pieces/community/google-docs": {
"name": "@activepieces/piece-google-docs",
- "version": "0.6.1",
+ "version": "0.6.2",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -4049,7 +4049,7 @@
},
"packages/pieces/community/google-sheets": {
"name": "@activepieces/piece-google-sheets",
- "version": "0.19.0",
+ "version": "0.19.1",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
diff --git a/packages/pieces/community/google-docs/package.json b/packages/pieces/community/google-docs/package.json
index 07dd26eed88..b9b8bebdd3b 100644
--- a/packages/pieces/community/google-docs/package.json
+++ b/packages/pieces/community/google-docs/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-google-docs",
- "version": "0.6.1",
+ "version": "0.6.2",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/google-docs/src/lib/actions/find-document.ts b/packages/pieces/community/google-docs/src/lib/actions/find-document.ts
index c8f730654a7..3b9ae1e61d3 100644
--- a/packages/pieces/community/google-docs/src/lib/actions/find-document.ts
+++ b/packages/pieces/community/google-docs/src/lib/actions/find-document.ts
@@ -7,6 +7,7 @@ import {
import { drive as googleDrive } from '@googleapis/drive';
import { docs as googleDocs } from '@googleapis/docs';
import { folderIdProp } from '../common/props';
+import { driveQuery } from '../common/drive-query';
import { findDocumentActionOutputSchema } from '../output-schemas';
export const findDocumentAction = createAction({
@@ -85,12 +86,12 @@ export const findDocumentAction = createAction({
// Search for the document in Google Drive
const query: string[] = [
- `name contains '${documentName}'`,
+ `name contains '${driveQuery.escapeLiteral(documentName)}'`,
`mimeType='application/vnd.google-apps.document'`,
'trashed=false',
];
- if (folderId) query.push(`'${folderId}' in parents`);
+ if (folderId) query.push(`'${driveQuery.escapeLiteral(folderId)}' in parents`);
const response = await drive.files.list({
q: query.join(' and '),
diff --git a/packages/pieces/community/google-docs/src/lib/actions/search-documents.action.ts b/packages/pieces/community/google-docs/src/lib/actions/search-documents.action.ts
index 3e20f98d6f8..eff18a1fda4 100644
--- a/packages/pieces/community/google-docs/src/lib/actions/search-documents.action.ts
+++ b/packages/pieces/community/google-docs/src/lib/actions/search-documents.action.ts
@@ -2,6 +2,7 @@ import { googleDocsAuth, createGoogleClient } from '../auth';
import { Property, createAction } from '@activepieces/pieces-framework';
import { drive as googleDrive } from '@googleapis/drive';
import { searchDocumentsActionOutputSchema } from '../output-schemas';
+import { driveQuery } from '../common/drive-query';
export const searchDocuments = createAction({
auth: googleDocsAuth,
@@ -74,12 +75,12 @@ export const searchDocuments = createAction({
const matchClauses: string[] = [];
if (name_contains) {
matchClauses.push(
- `name contains '${name_contains.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
+ `name contains '${driveQuery.escapeLiteral(name_contains)}'`
);
}
if (full_text_contains) {
matchClauses.push(
- `fullText contains '${full_text_contains.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
+ `fullText contains '${driveQuery.escapeLiteral(full_text_contains)}'`
);
}
diff --git a/packages/pieces/community/google-docs/src/lib/common/drive-query.test.ts b/packages/pieces/community/google-docs/src/lib/common/drive-query.test.ts
new file mode 100644
index 00000000000..d8fa706c66a
--- /dev/null
+++ b/packages/pieces/community/google-docs/src/lib/common/drive-query.test.ts
@@ -0,0 +1,16 @@
+import { describe, expect, it } from 'vitest';
+import { driveQuery } from './drive-query';
+
+describe('driveQuery.escapeLiteral', () => {
+ it('leaves plain text unchanged', () => {
+ expect(driveQuery.escapeLiteral('Q3 budget')).toBe('Q3 budget');
+ });
+
+ it('escapes single quotes', () => {
+ expect(driveQuery.escapeLiteral("Talal's report")).toBe("Talal\\'s report");
+ });
+
+ it('escapes backslashes before quotes so the quote escape is not undone', () => {
+ expect(driveQuery.escapeLiteral("a\\'b")).toBe("a\\\\\\'b");
+ });
+});
diff --git a/packages/pieces/community/google-docs/src/lib/common/drive-query.ts b/packages/pieces/community/google-docs/src/lib/common/drive-query.ts
new file mode 100644
index 00000000000..5442ab382f1
--- /dev/null
+++ b/packages/pieces/community/google-docs/src/lib/common/drive-query.ts
@@ -0,0 +1,5 @@
+function escapeLiteral(value: string): string {
+ return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
+}
+
+export const driveQuery = { escapeLiteral };
diff --git a/packages/pieces/community/google-docs/src/lib/common/props.ts b/packages/pieces/community/google-docs/src/lib/common/props.ts
index 35c7c772a5e..7775d4992f9 100644
--- a/packages/pieces/community/google-docs/src/lib/common/props.ts
+++ b/packages/pieces/community/google-docs/src/lib/common/props.ts
@@ -1,14 +1,16 @@
-import { googleDocsAuth, createGoogleClient, GoogleDocsAuthValue } from '../auth';
+import { googleDocsAuth, createGoogleClient } from '../auth';
import { DropdownOption, isNil, Property } from '@activepieces/pieces-framework';
import { drive as googleDrive, drive_v3 } from '@googleapis/drive';
+import { driveQuery } from './drive-query';
export const folderIdProp = Property.Dropdown({
displayName: 'Folder',
description: 'Leave empty to include all of Drive.',
refreshers: [],
+ refreshOnSearch: true,
auth: googleDocsAuth,
required: false,
- options: async ({ auth }) => {
+ options: async ({ auth }, { searchValue }) => {
if (!auth) {
return {
disabled: true,
@@ -16,40 +18,39 @@ export const folderIdProp = Property.Dropdown({
options: [],
};
}
- const authValue = auth as GoogleDocsAuthValue;
- const authClient = await createGoogleClient(authValue);
+ const authClient = await createGoogleClient(auth);
const drive = googleDrive({ version: 'v3', auth: authClient });
- const options: DropdownOption<string>[] = [];
+ const trimmedSearchValue = searchValue?.trim() ?? '';
+ const q = ["mimeType='application/vnd.google-apps.folder'", 'trashed = false'];
- let nextPageToken;
+ if (trimmedSearchValue.length > 0) {
+ q.push(`name contains '${driveQuery.escapeLiteral(trimmedSearchValue)}'`);
+ }
- do {
- const response: any = await drive.files.list({
- q: "mimeType='application/vnd.google-apps.folder' and trashed = false",
- supportsAllDrives: true,
- orderBy:'createdTime desc',
- includeItemsFromAllDrives: true,
- corpora: 'allDrives',
- pageToken: nextPageToken,
- });
+ const response = await drive.files.list({
+ q: q.join(' and '),
+ pageSize: DROPDOWN_PAGE_SIZE,
+ orderBy: 'createdTime desc',
+ fields: 'nextPageToken, files(id, name)',
+ supportsAllDrives: true,
+ includeItemsFromAllDrives: true,
+ corpora: 'allDrives',
+ });
- const fileList: drive_v3.Schema$FileList = response.data;
+ const fileList: drive_v3.Schema$FileList = response.data;
- if (fileList.files) {
- for (const file of fileList.files)
- options.push({
- label: file.name!,
- value: file.id!,
- });
- }
- nextPageToken = response.data.nextPageToken;
- } while (nextPageToken);
+ const options: DropdownOption<string>[] = (fileList.files ?? []).flatMap((file) =>
+ isNil(file.id) || isNil(file.name) ? [] : [{ label: file.name, value: file.id }],
+ );
return {
disabled: false,
+ placeholder: isNil(fileList.nextPageToken)
+ ? undefined
+ : `Showing the first ${options.length} folders. Type to narrow the list.`,
options,
};
},
@@ -80,12 +81,12 @@ export const documentIdProp = ({ description }: { description?: string } = {}) =
const q = ["mimeType='application/vnd.google-apps.document'", 'trashed = false'];
if (trimmedSearchValue.length > 0) {
- q.push(`name contains '${escapeDriveQueryLiteral(trimmedSearchValue)}'`);
+ q.push(`name contains '${driveQuery.escapeLiteral(trimmedSearchValue)}'`);
}
const response = await drive.files.list({
q: q.join(' and '),
- pageSize: DOCUMENT_DROPDOWN_PAGE_SIZE,
+ pageSize: DROPDOWN_PAGE_SIZE,
orderBy: 'createdTime desc',
fields: 'nextPageToken, files(id, name)',
supportsAllDrives: true,
@@ -109,8 +110,4 @@ export const documentIdProp = ({ description }: { description?: string } = {}) =
},
});
-function escapeDriveQueryLiteral(value: string): string {
- return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
-}
-
-const DOCUMENT_DROPDOWN_PAGE_SIZE = 1000;
+const DROPDOWN_PAGE_SIZE = 1000;
diff --git a/packages/pieces/community/google-docs/src/lib/triggers/new-document.ts b/packages/pieces/community/google-docs/src/lib/triggers/new-document.ts
index 5b764b23ef3..6083a8f4e00 100644
--- a/packages/pieces/community/google-docs/src/lib/triggers/new-document.ts
+++ b/packages/pieces/community/google-docs/src/lib/triggers/new-document.ts
@@ -6,6 +6,7 @@ import {
TriggerStrategy,
} from '@activepieces/pieces-framework';
import { folderIdProp } from '../common/props';
+import { driveQuery } from '../common/drive-query';
import { newDocumentTriggerOutputSchema } from '../output-schemas';
import dayjs from 'dayjs';
import { drive as googleDrive, drive_v3 } from '@googleapis/drive';
@@ -24,7 +25,7 @@ const polling: Polling<AppConnectionValueForAuthProperty<typeof googleDocsAuth>,
q.push(`createdTime > '${dayjs(lastFetchEpochMS).toISOString()}'`);
}
if (folderId) {
- q.push(`'${folderId}' in parents`);
+ q.push(`'${driveQuery.escapeLiteral(folderId)}' in parents`);
}
const authClient = await createGoogleClient(auth);
diff --git a/packages/pieces/community/google-sheets/package.json b/packages/pieces/community/google-sheets/package.json
index 3d8d526b86d..9024d1d45b8 100644
--- a/packages/pieces/community/google-sheets/package.json
+++ b/packages/pieces/community/google-sheets/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-google-sheets",
- "version": "0.19.0",
+ "version": "0.19.1",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"dependencies": {
diff --git a/packages/pieces/community/google-sheets/src/lib/actions/find-spreadsheets.ts b/packages/pieces/community/google-sheets/src/lib/actions/find-spreadsheets.ts
index aa4540d7883..5d7da64eef4 100644
--- a/packages/pieces/community/google-sheets/src/lib/actions/find-spreadsheets.ts
+++ b/packages/pieces/community/google-sheets/src/lib/actions/find-spreadsheets.ts
@@ -9,6 +9,7 @@ import { googleSheetsAuth } from '../common/common';
import { includeTeamDrivesProp } from '../common/props';
import { getAccessToken } from '../common/common';
import { findSpreadsheetsActionOutputSchema } from '../output-schemas';
+import { driveQuery } from '../common/drive-query';
export const findSpreadsheets = createAction({
name: 'find_spreadsheets',
@@ -43,9 +44,9 @@ export const findSpreadsheets = createAction({
const queries = ["mimeType='application/vnd.google-apps.spreadsheet'", 'trashed=false'];
if (propsValue.exact_match) {
- queries.push(`name = '${searchValue}'`);
+ queries.push(`name = '${driveQuery.escapeLiteral(searchValue)}'`);
} else {
- queries.push(`name contains '${searchValue}'`);
+ queries.push(`name contains '${driveQuery.escapeLiteral(searchValue)}'`);
}
const files = [];
diff --git a/packages/pieces/community/google-sheets/src/lib/actions/sheets-search-spreadsheets.ts b/packages/pieces/community/google-sheets/src/lib/actions/sheets-search-spreadsheets.ts
index bd2e9c35677..8b7181700fd 100644
--- a/packages/pieces/community/google-sheets/src/lib/actions/sheets-search-spreadsheets.ts
+++ b/packages/pieces/community/google-sheets/src/lib/actions/sheets-search-spreadsheets.ts
@@ -7,6 +7,7 @@ import {
} from '@activepieces/pieces-common';
import { getAccessToken, googleSheetsAuth } from '../common/common';
import { sheetsSearchSpreadsheetsActionOutputSchema } from '../output-schemas';
+import { driveQuery } from '../common/drive-query';
export const sheetsSearchSpreadsheets = createAction({
auth: googleSheetsAuth,
@@ -40,9 +41,9 @@ export const sheetsSearchSpreadsheets = createAction({
const queries = ["mimeType='application/vnd.google-apps.spreadsheet'", 'trashed=false'];
if (propsValue.exact_match) {
- queries.push(`name = '${searchValue}'`);
+ queries.push(`name = '${driveQuery.escapeLiteral(searchValue)}'`);
} else {
- queries.push(`name contains '${searchValue}'`);
+ queries.push(`name contains '${driveQuery.escapeLiteral(searchValue)}'`);
}
const files = [];
diff --git a/packages/pieces/community/google-sheets/src/lib/common/drive-query.test.ts b/packages/pieces/community/google-sheets/src/lib/common/drive-query.test.ts
new file mode 100644
index 00000000000..d8fa706c66a
--- /dev/null
+++ b/packages/pieces/community/google-sheets/src/lib/common/drive-query.test.ts
@@ -0,0 +1,16 @@
+import { describe, expect, it } from 'vitest';
+import { driveQuery } from './drive-query';
+
+describe('driveQuery.escapeLiteral', () => {
+ it('leaves plain text unchanged', () => {
+ expect(driveQuery.escapeLiteral('Q3 budget')).toBe('Q3 budget');
+ });
+
+ it('escapes single quotes', () => {
+ expect(driveQuery.escapeLiteral("Talal's report")).toBe("Talal\\'s report");
+ });
+
+ it('escapes backslashes before quotes so the quote escape is not undone', () => {
+ expect(driveQuery.escapeLiteral("a\\'b")).toBe("a\\\\\\'b");
+ });
+});
diff --git a/packages/pieces/community/google-sheets/src/lib/common/drive-query.ts b/packages/pieces/community/google-sheets/src/lib/common/drive-query.ts
new file mode 100644
index 00000000000..5442ab382f1
--- /dev/null
+++ b/packages/pieces/community/google-sheets/src/lib/common/drive-query.ts
@@ -0,0 +1,5 @@
+function escapeLiteral(value: string): string {
+ return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
+}
+
+export const driveQuery = { escapeLiteral };
diff --git a/packages/pieces/community/google-sheets/src/lib/common/props.ts b/packages/pieces/community/google-sheets/src/lib/common/props.ts
index 7717f72184b..7a6818fbb5c 100644
--- a/packages/pieces/community/google-sheets/src/lib/common/props.ts
+++ b/packages/pieces/community/google-sheets/src/lib/common/props.ts
@@ -1,6 +1,7 @@
import { DropdownOption, Property } from '@activepieces/pieces-framework';
import { sheets as googleSheets } from '@googleapis/sheets';
import { drive as googleDrive, drive_v3 } from '@googleapis/drive';
+import { driveQuery } from './drive-query';
import {
columnToLabel,
createGoogleClient,
@@ -49,7 +50,7 @@ export const spreadsheetIdProp = (displayName: string, description: string, requ
const q = ["mimeType='application/vnd.google-apps.spreadsheet'", 'trashed = false'];
if (searchValue) {
- q.push(`name contains '${searchValue}'`);
+ q.push(`name contains '${driveQuery.escapeLiteral(searchValue)}'`);
}
let nextPageToken;