Skip to content

GDrive query breaks on filenames containing an apostrophe (unescaped single quote in query string) #105

Description

@MoeLa

Symptom

Sync fails for individual files with HttpError 400 ... "Invalid Value", e.g.: This is a file name with 'apostrophes'.pdf

Error

<HttpError 400 when requesting .../files?q=%271weJUiVGs...%27+in+parents+and+name+%3D+%27Ich+gebe+%27250602_kLiq... returned "Invalid Value". Details: [{'reason': 'invalid', 'location': 'q', ...}]

Root Cause

GDriveConnector._find_file() in connectors/gdrive.py builds the Drive API query
via an f-string and interpolates both the folder segment (segment) and the
filename (search_name) without any escaping:

q=f"'{current_folder}' in parents and name = '{segment}' and mimeType = '...folder' and trashed = false"
q=f"'{current_folder}' in parents and name = '{search_name}' and trashed = false"

If the filename itself contains a ', it breaks the Drive API v3 query syntax,
since ' is used there as the string delimiter.

Fix

Added an escape helper and applied it at both call sites:

def _escape_drive_query_value(value: str) -> str:
    """Escape a value for safe use inside a Drive API v3 query string."""
    return value.replace("\\", "\\\\").replace("'", "\\'")

segment and search_name are now passed through _escape_drive_query_value()
before being interpolated into the query. folder_id/current_folder (Drive IDs)
don't need escaping since they're never user-supplied free text.

Verification

  • Sync run no longer produces Invalid Value / 400 Bad Request errors for
    files with an apostrophe in the name
  • Spot check: the affected file (Ich gebe '...' für Sie frei.pdf) imports
    successfully

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions