Skip to content

CBG-5637 Fix ConvertBackQuotedStrings corrupting backquotes inside JSON strings#8476

Open
torcolvin wants to merge 1 commit into
mainfrom
CBG-5637
Open

CBG-5637 Fix ConvertBackQuotedStrings corrupting backquotes inside JSON strings#8476
torcolvin wants to merge 1 commit into
mainfrom
CBG-5637

Conversation

@torcolvin

Copy link
Copy Markdown
Collaborator

CBG-5637 Fix ConvertBackQuotedStrings corrupting backquotes inside JSON strings

Replace the regex with a single-pass scanner that tracks whether it's currently inside a JSON string and only treats backquotes and a string to escape if it is outside of a double quote block.

Fixes code:

PUT https://<sgw-node>:4985/e1/
{
  "bucket": "travel-sample",
  "num_index_replicas": 0,
  "enable_shared_bucket_access": true,
  "scopes": {
    "inventory": {
      "collections": {
        "airline": {
          "sync": "function (doc, oldDoc, meta) {\n  // Test`ing` comment \n  channel(\"airline\");\n}"
        }
      }
    }

Note that the only backticks can occur in comments since otto does not support ES6 template literals.

Pre-review checklist

  • Removed debug logging (fmt.Print, log.Print, ...)
  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Integration Tests

…ON strings

Replace the regex with a single-pass scanner that tracks whether it's
currently inside a JSON string and only treats backquotes and a string to escape if it is outside of a double quote
block.
Copilot AI review requested due to automatic review settings July 22, 2026 20:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes base.ConvertBackQuotedStrings so it no longer corrupts backticks that appear inside existing JSON strings (e.g., in sync function source embedded in JSON). It replaces the previous regex-based implementation with a single-pass scanner that tracks JSON string context, and expands test coverage (including fuzzing) to lock in the corrected behavior.

Changes:

  • Replaced the regex backquote conversion logic with a stateful scanner that ignores backticks inside JSON double-quoted strings.
  • Added extensive unit tests and fuzz tests validating idempotence and “valid JSON in => semantically identical JSON out”.
  • Updated an admin API test to exercise more sync-function variants containing backticks.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
rest/adminapitest/admin_api_test.go Expands the collection sync-function test into subtests covering multiple backtick scenarios.
base/util.go Reimplements ConvertBackQuotedStrings using a single-pass scanner with helpers for closing-delimiter detection and escaping.
base/util_test.go Adds many new cases plus fuzz targets to validate correctness, idempotence, and preservation of valid JSON.

Comment on lines +3396 to 3400
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Start each subtest from a clean slate.
_ = rt.SendAdminRequest(http.MethodDelete, "/db/", "")

@torcolvin
torcolvin requested a review from bbrks July 24, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants