Fix patch policy query (#41915)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #41857 # Checklist for submitter If some of the following don't apply, delete the relevant line. - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. - [ ] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements), JS inline code is prevented especially for url redirects, and untrusted data interpolated into shell scripts/commands is validated against shell metacharacters. - [ ] If paths of existing endpoints are modified without backwards compatibility, checked the frontend/CLI for any necessary changes ## Testing - [x] Added/updated automated tests - [ ] Where appropriate, [automated tests simulate multiple hosts and test for host isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing) (updates to one hosts's records do not affect another) - [x] QA'd all new/changed functionality manually - Tested 010 Editor on macOS
This commit is contained in:
@@ -2625,7 +2625,7 @@ func (ds *Datastore) generatePatchPolicy(ctx context.Context, teamID uint, title
|
||||
switch {
|
||||
case installer.Platform == string(fleet.MacOSPlatform):
|
||||
policy.Query = fmt.Sprintf(
|
||||
"SELECT 1 FROM apps WHERE bundle_identifier = '%s' AND version_compare(bundle_short_version, '%s') >= 0;",
|
||||
"SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM apps WHERE bundle_identifier = '%s' AND version_compare(bundle_short_version, '%s') < 0);",
|
||||
installer.BundleIdentifier,
|
||||
installer.Version,
|
||||
)
|
||||
@@ -2634,7 +2634,7 @@ func (ds *Datastore) generatePatchPolicy(ctx context.Context, teamID uint, title
|
||||
case installer.Platform == "windows":
|
||||
// TODO: use upgrade code to improve accuracy?
|
||||
policy.Query = fmt.Sprintf(
|
||||
"SELECT 1 FROM programs WHERE name = '%s' AND version_compare(bundle_short_version, '%s') >= 0;",
|
||||
"SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM programs WHERE name = '%s' AND version_compare(bundle_short_version, '%s') < 0);",
|
||||
installer.SoftwareTitle,
|
||||
installer.Version,
|
||||
)
|
||||
|
||||
@@ -7573,7 +7573,7 @@ func testTeamPatchPolicy(t *testing.T, ds *Datastore) {
|
||||
PatchSoftwareTitleID: &titleID,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "SELECT 1 FROM apps WHERE bundle_identifier = 'fleet.maintained1' AND version_compare(bundle_short_version, '1.0') >= 0;", p1.Query)
|
||||
require.Equal(t, "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM apps WHERE bundle_identifier = 'fleet.maintained1' AND version_compare(bundle_short_version, '1.0') < 0);", p1.Query)
|
||||
|
||||
_, err = ds.NewTeamPolicy(ctx, team1.ID, &user1.ID, fleet.PolicyPayload{
|
||||
Name: "p2",
|
||||
@@ -7595,7 +7595,7 @@ func testTeamPatchPolicy(t *testing.T, ds *Datastore) {
|
||||
require.Equal(t, "Outdated software might introduce security vulnerabilities or compatibility issues.", p3.Description)
|
||||
require.Equal(t, "Install the latest version from self-service.", *p3.Resolution)
|
||||
require.Equal(t, "darwin", p3.Platform)
|
||||
require.Equal(t, "SELECT 1 FROM apps WHERE bundle_identifier = 'fleet.maintained1' AND version_compare(bundle_short_version, '1.0') >= 0;", p3.Query)
|
||||
require.Equal(t, "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM apps WHERE bundle_identifier = 'fleet.maintained1' AND version_compare(bundle_short_version, '1.0') < 0);", p3.Query)
|
||||
|
||||
_, err = ds.DeleteTeamPolicies(ctx, team1.ID, []uint{p3.ID})
|
||||
require.NoError(t, err)
|
||||
@@ -7613,7 +7613,7 @@ func testTeamPatchPolicy(t *testing.T, ds *Datastore) {
|
||||
require.Equal(t, "description", p4.Description)
|
||||
require.Equal(t, "resolution", *p4.Resolution)
|
||||
require.Equal(t, "darwin", p4.Platform)
|
||||
require.Equal(t, "SELECT 1 FROM apps WHERE bundle_identifier = 'fleet.maintained1' AND version_compare(bundle_short_version, '1.0') >= 0;", p4.Query)
|
||||
require.Equal(t, "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM apps WHERE bundle_identifier = 'fleet.maintained1' AND version_compare(bundle_short_version, '1.0') < 0);", p4.Query)
|
||||
|
||||
// test GetPatchPolicy
|
||||
data, err := ds.GetPatchPolicy(ctx, &team1.ID, titleID)
|
||||
|
||||
@@ -26429,7 +26429,7 @@ func (s *integrationEnterpriseTestSuite) TestPatchPolicies() {
|
||||
require.NotNil(t, policy.PatchSoftware)
|
||||
require.Equal(t, titleID, policy.PatchSoftware.SoftwareTitleID)
|
||||
require.Equal(t, fleet.PolicyTypePatch, policy.Type)
|
||||
require.Equal(t, fmt.Sprintf(`SELECT 1 FROM programs WHERE name = 'Zoom Workplace (X64)' AND version_compare(bundle_short_version, '%s') >= 0;`, version), policy.Query)
|
||||
require.Equal(t, fmt.Sprintf(`SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM programs WHERE name = 'Zoom Workplace (X64)' AND version_compare(bundle_short_version, '%s') < 0);`, version), policy.Query)
|
||||
require.Equal(t, name, policy.Name)
|
||||
}
|
||||
|
||||
@@ -26515,7 +26515,7 @@ func (s *integrationEnterpriseTestSuite) TestPatchPolicies() {
|
||||
require.Equal(t, "macOS - DummyApp up to date", policyResp.Policy.Name)
|
||||
require.Equal(t, "Outdated software might introduce security vulnerabilities or compatibility issues.", policyResp.Policy.Description)
|
||||
require.Equal(t, "Install the latest version from self-service.", *policyResp.Policy.Resolution)
|
||||
require.Contains(t, policyResp.Policy.Query, "SELECT 1 FROM apps WHERE bundle_identifier =")
|
||||
require.Equal(t, policyResp.Policy.Query, "SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM apps WHERE bundle_identifier = 'com.example.dummy' AND version_compare(bundle_short_version, '1.0.0') < 0);")
|
||||
require.NotNil(t, policyResp.Policy.PatchSoftware)
|
||||
require.Equal(t, titleID, policyResp.Policy.PatchSoftware.SoftwareTitleID)
|
||||
policyID := policyResp.Policy.ID
|
||||
|
||||
Reference in New Issue
Block a user