@@ -62,6 +62,7 @@ import {
6262 generateIndexPatternSearchResults ,
6363 setupAuthorizeFunc ,
6464 setupAuthorizeFind ,
65+ HIDDEN_TYPE ,
6566} from '../test_helpers/repository.test.common' ;
6667import { savedObjectsExtensionsMock } from '../mocks/saved_objects_extensions.mock' ;
6768import { arrayMapsAreEqual } from '@kbn/core-saved-objects-utils-server' ;
@@ -845,14 +846,108 @@ describe('SavedObjectsRepository Security Extension', () => {
845846 } ) ;
846847 } ) ;
847848
849+ test ( `returns empty authorization map for partially authorized if the authorized types are not part of the query` , async ( ) => {
850+ setupAuthorizeFind ( mockSecurityExt , 'partially_authorized' ) ;
851+ setupRedactPassthrough ( mockSecurityExt ) ;
852+
853+ await findSuccess (
854+ client ,
855+ repository ,
856+ {
857+ type : [ type , NAMESPACE_AGNOSTIC_TYPE ] ,
858+ namespaces : [ namespace , 'ns-1' ] ,
859+ } , // include multiple types and spaces
860+ namespace
861+ ) ;
862+
863+ expect ( mockGetSearchDsl . mock . calls [ 0 ] . length ) . toBe ( 3 ) ; // Find success verifies this is called once, this should always pass
864+ const {
865+ typeToNamespacesMap : actualMap ,
866+ } : { typeToNamespacesMap : Map < string , string [ ] | undefined > } =
867+ mockGetSearchDsl . mock . calls [ 0 ] [ 2 ] ;
868+
869+ expect ( actualMap ) . not . toBeUndefined ( ) ;
870+ const expectedMap = new Map < string , string [ ] | undefined > ( ) ;
871+
872+ expect ( arrayMapsAreEqual ( actualMap , expectedMap ) ) . toBeTruthy ( ) ;
873+ } ) ;
874+
875+ test ( `returns empty authorization map for fully authorized if the authorized types are not part of the query` , async ( ) => {
876+ setupAuthorizeFind ( mockSecurityExt , 'fully_authorized' ) ;
877+ setupRedactPassthrough ( mockSecurityExt ) ;
878+
879+ await findSuccess (
880+ client ,
881+ repository ,
882+ {
883+ type : [ type , NAMESPACE_AGNOSTIC_TYPE ] ,
884+ namespaces : [ namespace , 'ns-1' ] ,
885+ } , // include multiple types and spaces
886+ namespace
887+ ) ;
888+
889+ expect ( mockGetSearchDsl . mock . calls [ 0 ] . length ) . toBe ( 3 ) ; // Find success verifies this is called once, this should always pass
890+ const {
891+ typeToNamespacesMap : actualMap ,
892+ } : { typeToNamespacesMap : Map < string , string [ ] | undefined > } =
893+ mockGetSearchDsl . mock . calls [ 0 ] [ 2 ] ;
894+
895+ expect ( actualMap ) . not . toBeUndefined ( ) ;
896+ const expectedMap = new Map < string , string [ ] | undefined > ( ) ;
897+
898+ expect ( arrayMapsAreEqual ( actualMap , expectedMap ) ) . toBeTruthy ( ) ;
899+ } ) ;
900+
848901 test ( `uses the authorization map when partially authorized` , async ( ) => {
849902 setupAuthorizeFind ( mockSecurityExt , 'partially_authorized' ) ;
850903 setupRedactPassthrough ( mockSecurityExt ) ;
851904
852905 await findSuccess (
853906 client ,
854907 repository ,
855- { type : [ type , NAMESPACE_AGNOSTIC_TYPE ] , namespaces : [ namespace , 'ns-1' ] } , // include multiple types and spaces
908+ {
909+ type : [
910+ type ,
911+ 'foo' ,
912+ // Explicitly request the hidden type despite the repository not having access to it to confirm that it's not authorized.
913+ HIDDEN_TYPE ,
914+ NAMESPACE_AGNOSTIC_TYPE ,
915+ ] ,
916+ namespaces : [ namespace , 'ns-1' ] ,
917+ } , // include multiple types and spaces
918+ namespace
919+ ) ;
920+
921+ expect ( mockGetSearchDsl . mock . calls [ 0 ] . length ) . toBe ( 3 ) ; // Find success verifies this is called once, this should always pass
922+ const {
923+ typeToNamespacesMap : actualMap ,
924+ } : { typeToNamespacesMap : Map < string , string [ ] | undefined > } =
925+ mockGetSearchDsl . mock . calls [ 0 ] [ 2 ] ;
926+
927+ expect ( actualMap ) . not . toBeUndefined ( ) ;
928+ const expectedMap = new Map < string , string [ ] | undefined > ( ) ;
929+ expectedMap . set ( 'foo' , [ 'bar' ] ) ; // this is what is hard-coded in authMap
930+
931+ expect ( arrayMapsAreEqual ( actualMap , expectedMap ) ) . toBeTruthy ( ) ;
932+ } ) ;
933+
934+ test ( `uses the authorization map when fully authorized` , async ( ) => {
935+ setupAuthorizeFind ( mockSecurityExt , 'fully_authorized' ) ;
936+ setupRedactPassthrough ( mockSecurityExt ) ;
937+
938+ await findSuccess (
939+ client ,
940+ repository ,
941+ {
942+ type : [
943+ type ,
944+ 'foo' ,
945+ // Explicitly request the hidden type despite the repository not having access to it to confirm that it's not authorized.
946+ HIDDEN_TYPE ,
947+ NAMESPACE_AGNOSTIC_TYPE ,
948+ ] ,
949+ namespaces : [ namespace , 'ns-1' ] ,
950+ } , // include multiple types and spaces
856951 namespace
857952 ) ;
858953
0 commit comments