Skip to content

Generate queries with comments #3573

Closed
@AlekSi

Description

What should be done?

When find command is invoked with a comment argument, we pass its value to Collection.Query. It is not used after that.

We should use it in the generated SQL query. Instead of sending SELECT _json … to the database, we should send SELECT /* <comment> */ _jsonb …. We should also properly escape it:

if c := p.comment; c != "" {
// prevent SQL injections
c = strings.ReplaceAll(c, "/*", "/ *")
c = strings.ReplaceAll(c, "*/", "* /")
query += `/* ` + c + ` */ `
}

We already have tests for that. Let's check if they fail if we don't escape the comment:

func TestFindCommentMethod(t *testing.T) {
ctx, collection := setup.Setup(t, shareddata.Scalars)
name := collection.Database().Name()
databaseNames, err := collection.Database().Client().ListDatabaseNames(ctx, bson.D{})
require.NoError(t, err)
comment := "*/ 1; DROP SCHEMA " + name + " CASCADE -- "
var doc bson.D
opts := options.FindOne().SetComment(comment)
err = collection.FindOne(ctx, bson.D{{"_id", "string"}}, opts).Decode(&doc)
require.NoError(t, err)
assert.Contains(t, databaseNames, name)
}

Where?

See references to this issue.

Definition of Done

  • both PostgreSQL and SQLite backends updated;
  • handler updated;
  • spot refactorings done.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

code/enhancementSome user-visible feature could work bettercommunityIssues and PRs assigned to community membersgood first issueGood issues for new external contributors

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions