-
Notifications
You must be signed in to change notification settings - Fork 31
Introduce parameterized query attributes for GrantTypeSqlQueryBuilder #2931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aktaskaan
wants to merge
12
commits into
master
Choose a base branch
from
ccd_5259_ccd_6209
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a4aa16
Introduce parameterized query attributes for GrantTypeSqlQueryBuilder…
aktaskaan 89c2ab5
Merge branch 'master' into ccd_5259_ccd_6209
aktaskaan 28db210
Make `escapeLikeWildcards` static and apply style improvements across…
aktaskaan 65a9028
Merge branch 'master' into ccd_5259_ccd_6209
aktaskaan ea140eb
Merge branch 'master' into ccd_5259_ccd_6209
patelila 4dca368
Merge branch 'master' into ccd_5259_ccd_6209
patelila f2c5429
Merge branch 'master' into ccd_5259_ccd_6209
patelila 3607a56
Introduce namespace isolation for access control parameters to improv…
aktaskaan 7dffcf1
Merge remote-tracking branch 'origin/ccd_5259_ccd_6209' into ccd_5259…
aktaskaan da5dbf8
Merge branch 'master' into ccd_5259_ccd_6209
aktaskaan cb699f7
Add test cases for access control parameter namespace validation and …
aktaskaan a0e5aeb
Update build.gradle
aktaskaan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/java/uk/gov/hmcts/ccd/data/casedetails/search/SortOrderQueryBuilderIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package uk.gov.hmcts.ccd.data.casedetails.search; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import uk.gov.hmcts.ccd.WireMockBaseTest; | ||
| import uk.gov.hmcts.ccd.endpoint.exceptions.BadRequestException; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| /** | ||
| * Executes the clause produced by {@link SortOrderQueryBuilder} against the Testcontainers | ||
| * Postgres instance already used by the integration tests. | ||
| * Scope of what this proves: a quote-bearing case field id is now rejected by | ||
| * CASE_FIELD_ID_PATTERN at build/validation time, so it never reaches the database. The | ||
| * well-formed control case still executes cleanly end to end, proving the happy path works. | ||
| */ | ||
| public class SortOrderQueryBuilderIT extends WireMockBaseTest { | ||
|
|
||
| private static final String CASE_TYPE_ID = "CaseTypeOne"; | ||
| private static final String JURISDICTION_ID = "JurisdictionOne"; | ||
|
|
||
| @Inject | ||
| private SortOrderQueryBuilder sortOrderQueryBuilder; | ||
|
|
||
| private JdbcTemplate template; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| template = new JdbcTemplate(db); | ||
| } | ||
|
|
||
| private String sortClauseFor(String caseFieldId) { | ||
| MetaData metaData = new MetaData(CASE_TYPE_ID, JURISDICTION_ID); | ||
| metaData.addSortOrderField(SortOrderField.sortOrderWith() | ||
| .caseFieldId(caseFieldId) | ||
| .metadata(false) | ||
| .direction("ASC") | ||
| .build()); | ||
| return sortOrderQueryBuilder.buildSortOrderClause(metaData); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("A well-formed field id produces a clause Postgres can execute") | ||
| void wellFormedFieldId_producesExecutableQuery() { | ||
| String sql = "SELECT reference FROM case_data ORDER BY " + sortClauseFor("PersonFirstName"); | ||
|
|
||
| assertDoesNotThrow(() -> template.queryForList(sql)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("A quote-bearing field id is rejected at validation and never reaches Postgres") | ||
| void singleQuoteFieldId_rejectedAtValidation_neverReachesDatabase() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> sortClauseFor("foo'bar")); | ||
|
|
||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
| } |
118 changes: 118 additions & 0 deletions
118
src/test/java/uk/gov/hmcts/ccd/data/casedetails/search/SortOrderQueryBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package uk.gov.hmcts.ccd.data.casedetails.search; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import uk.gov.hmcts.ccd.endpoint.exceptions.BadRequestException; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.junit.jupiter.api.Assertions.assertAll; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| /** | ||
| * These tests pin the behaviour of {@link SortOrderQueryBuilder}'s CASE_FIELD_ID_PATTERN, now a | ||
| * strict identifier allow-list: only letters, digits, underscore, dot and square brackets are | ||
| * permitted. Single quotes, whitespace and hyphens - which could otherwise reach a JSONB path | ||
| * literal that cannot be parameterised - are rejected with a BadRequestException. | ||
| * The rejection cases below guard the tightened allow-list: they must keep failing closed. Do | ||
| * not relax them to make a change pass. | ||
| */ | ||
| class SortOrderQueryBuilderTest { | ||
|
|
||
| private static final String CASE_TYPE_ID = "CaseTypeOne"; | ||
| private static final String JURISDICTION_ID = "JurisdictionOne"; | ||
|
|
||
| private SortOrderQueryBuilder sortOrderQueryBuilder; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| sortOrderQueryBuilder = new SortOrderQueryBuilder(); | ||
| } | ||
|
|
||
| private String buildForNonMetadataField(String caseFieldId) { | ||
| MetaData metaData = new MetaData(CASE_TYPE_ID, JURISDICTION_ID); | ||
| metaData.addSortOrderField(SortOrderField.sortOrderWith() | ||
| .caseFieldId(caseFieldId) | ||
| .metadata(false) | ||
| .direction("ASC") | ||
| .build()); | ||
| return sortOrderQueryBuilder.buildSortOrderClause(metaData); | ||
| } | ||
|
|
||
| private String buildForMetadataField(String caseFieldId) { | ||
| MetaData metaData = new MetaData(CASE_TYPE_ID, JURISDICTION_ID); | ||
| metaData.addSortOrderField(SortOrderField.sortOrderWith() | ||
| .caseFieldId(caseFieldId) | ||
| .metadata(true) | ||
| .direction("ASC") | ||
| .build()); | ||
| return sortOrderQueryBuilder.buildSortOrderClause(metaData); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("A single quote in a case field id is rejected") | ||
| void pattern_rejectsSingleQuote() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> buildForNonMetadataField("foo'bar")); | ||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Whitespace in a case field id is rejected") | ||
| void pattern_rejectsSpace() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> buildForNonMetadataField("foo bar")); | ||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("A hyphen in a case field id is rejected") | ||
| void pattern_rejectsHyphen() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> buildForNonMetadataField("foo-bar")); | ||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Current limit: a semicolon in a case field id is rejected") | ||
| void pattern_rejectsSemicolon() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> buildForNonMetadataField("foo;bar")); | ||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Current limit: an equals sign in a case field id is rejected") | ||
| void pattern_rejectsEquals() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> buildForNonMetadataField("foo=bar")); | ||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("A single quote is rejected at validation before any JSONB path is produced") | ||
| void nonMetadataField_singleQuote_rejectedBeforeSqlIsProduced() { | ||
| BadRequestException exception = | ||
| assertThrows(BadRequestException.class, () -> buildForNonMetadataField("foo'bar")); | ||
| assertThat(exception.getMessage(), containsString("Sort order field is invalid.")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("A well-formed nested field id is converted to a JSONB path as expected") | ||
| void nonMetadataField_dottedPath_isConvertedToJsonbPath() { | ||
| assertThat(buildForNonMetadataField("Parent.Child"), containsString("data #>> '{Parent,Child}'")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Metadata fields route through the CaseField enum and are never concatenated") | ||
| void metadataField_routesThroughEnum_notConcatenated() { | ||
| assertAll( | ||
| () -> assertThrows(IllegalArgumentException.class, () -> buildForMetadataField("notAMetadataField")), | ||
| () -> assertThrows(IllegalArgumentException.class, () -> buildForMetadataField("[notAMetadataField]")), | ||
| () -> assertThat(buildForMetadataField("[STATE]"), containsString("state ASC")), | ||
| () -> assertThat(buildForMetadataField("[STATE]"), not(containsString("data #>>"))) | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These ABAC parameter names are predictable and share the same namespace as user search criteria params. A user field like
case.region_1_basicis allowed, becomesregion_1_basic, and is bound after ABAC params inSearchQueryFactoryOperation, overwriting this predicate value. Please use a reserved/collision-proof prefix for all ABACparams and add a regression test.
Supporting refs if needed:
SearchQueryFactoryOperation.java:89-90
Criterion.java:26-27
FieldMapSanitizeOperation.java:19