Skip to content
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

Regular listeners shouldn't be called for paired response events #552

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions AEPCore/Sources/configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Configuration: Extension {
if event.isUpdateConfigEvent {
processUpdateConfig(event: event, sharedStateResolver: createPendingSharedState(event: event))
} else if event.isGetConfigEvent {
dispatchConfigurationResponse(triggerEvent: event, data: configState.environmentAwareConfiguration)
dispatchConfigurationResponse(requestEvent: event, data: configState.environmentAwareConfiguration)
} else if let appId = event.appId {
processConfigureWith(appId: appId, event: event, sharedStateResolver: createPendingSharedState(event: event))
} else if let filePath = event.filePath {
Expand Down Expand Up @@ -122,7 +122,7 @@ class Configuration: Extension {

configState.updateWith(programmaticConfig: updatedConfig)
// Create shared state and dispatch configuration response content
publishCurrentConfig(event: event, sharedStateResolver: sharedStateResolver)
publishCurrentConfig(sharedStateResolver: sharedStateResolver)
}

/// Interacts with the `ConfigurationState` to download the configuration associated with `appId`
Expand Down Expand Up @@ -155,7 +155,7 @@ class Configuration: Extension {
stopEvents()
configState.updateWith(appId: appId) { [weak self] config in
if let _ = config {
self?.publishCurrentConfig(event: event, sharedStateResolver: sharedStateResolver)
self?.publishCurrentConfig(sharedStateResolver: sharedStateResolver)
self?.startEvents()
} else {
// If downloading config failed try again later
Expand All @@ -182,7 +182,7 @@ class Configuration: Extension {
}

if configState.updateWith(filePath: filePath) {
publishCurrentConfig(event: event, sharedStateResolver: sharedStateResolver)
publishCurrentConfig(sharedStateResolver: sharedStateResolver)
} else {
// loading from bundled config failed, resolve shared state with current config without dispatching a config response event
sharedStateResolver(configState.environmentAwareConfiguration)
Expand All @@ -193,11 +193,20 @@ class Configuration: Extension {

/// Dispatches a configuration response content event with corresponding data
/// - Parameters:
/// - triggerEvent: The `Event` to which the newly dispatched `Event` is responding
/// - requestEvent: The `Event` to which the newly dispatched `Event` is responding, null if this is a generic response
/// - data: Optional data to be attached to the event
private func dispatchConfigurationResponse(triggerEvent: Event, data: [String: Any]?) {
let responseEvent = triggerEvent.createResponseEvent(name: CoreConstants.EventNames.CONFIGURATION_RESPONSE_EVENT, type: EventType.configuration, source: EventSource.responseContent, data: data)
private func dispatchConfigurationResponse(requestEvent: Event?, data: [String: Any]?) {
if let requestEvent = requestEvent {
let pairedResponseEvent = requestEvent.createResponseEvent(name: CoreConstants.EventNames.CONFIGURATION_RESPONSE_EVENT, type: EventType.configuration, source: EventSource.responseContent, data: data)
dispatch(event: pairedResponseEvent)
return
}

// send a generic event if this is not the response to a getter
let responseEvent = Event(name: CoreConstants.EventNames.CONFIGURATION_RESPONSE_EVENT, type: EventType.configuration, source: EventSource.responseContent, data: data)
dispatch(event: responseEvent)
return

}

/// Dispatches a configuration request content event with corresponding data
Expand All @@ -212,14 +221,13 @@ class Configuration: Extension {

/// Shares state with the current configuration and dispatches a configuration response event with the current configuration
/// - Parameters:
/// - event: The event at which this configuration should be published at
/// - sharedStateResolver: a closure which is resolved with the current configuration
private func publishCurrentConfig(event: Event, sharedStateResolver: SharedStateResolver) {
private func publishCurrentConfig(sharedStateResolver: SharedStateResolver) {
let config = configState.environmentAwareConfiguration
// Update the shared state with the new configuration
sharedStateResolver(config)
// Dispatch a Configuration Response Content event with the new configuration.
dispatchConfigurationResponse(triggerEvent: event, data: config)
dispatchConfigurationResponse(requestEvent: nil, data: config)
// notify the rules engine about the change of config
notifyRulesEngine(newConfiguration: config)
}
Expand Down
8 changes: 6 additions & 2 deletions AEPCore/Sources/eventhub/EventListenerContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct EventListenerContainer: Equatable {
/// - Parameter event: An `Event` being dispatched by the `EventHub`
/// - Returns: True if `listener` should be notified of `event`
func shouldNotify(_ event: Event) -> Bool {
if let listenerTriggerId = triggerEventId {
return listenerTriggerId == event.responseID
if event.responseID != nil {
return event.responseID == triggerEventId || self.isWildcard
}

return (event.type == type || type == EventType.wildcard)
nporter-adbe marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -62,4 +62,8 @@ internal extension EventListenerContainer {
init(listener: @escaping EventResponseListener, triggerEventId: UUID, timeout: DispatchWorkItem?) {
self.init(listener: listener, type: nil, source: nil, triggerEventId: triggerEventId, timeoutTask: timeout)
}

var isWildcard: Bool {
return self.source == EventSource.wildcard && self.type == EventType.wildcard
}
}
48 changes: 48 additions & 0 deletions AEPCore/Tests/EventHubTests/EventHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,54 @@ class EventHubTests: XCTestCase {
wait(for: [expectation], timeout: 0.5)
}

func testEventHubTestRegisterEventListenerNotInvokedForPairedResponseEvent() {
// setup
let expectation = XCTestExpectation(description: "Listener is not invoked for paired response event")
expectation.expectedFulfillmentCount = 1
expectation.assertForOverFulfill = true
let requestEvent = Event(name: "testEvent", type: EventType.analytics, source: EventSource.requestContent, data: nil)
let responseEvent = Event(name: "testResponseEvent1", type: EventType.analytics, source: EventSource.responseContent, data: nil)
let pairedResponseEvent = requestEvent.createResponseEvent(name: "testPairedResponseEvent1", type: EventType.analytics, source: EventSource.responseContent, data: nil)

// test
eventHub.registerEventListener(type: EventType.analytics, source: EventSource.responseContent) { event in
XCTAssertEqual(EventSource.responseContent, event.source)
XCTAssertNil(event.responseID)
expectation.fulfill()
}

eventHub.start()
eventHub.dispatch(event: responseEvent)
eventHub.dispatch(event: pairedResponseEvent)

// verify
wait(for: [expectation], timeout: 1)
}

func testEventHubTestRegisterEventListenerWildcardCalledForAllEvents() {
// setup
let expectation = XCTestExpectation(description: "Wildcard Listener is invoked for all events")
expectation.assertForOverFulfill = true
expectation.expectedFulfillmentCount = 2
let requestEvent = Event(name: "testEvent", type: EventType.analytics, source: EventSource.requestContent, data: nil)
let responseEvent = Event(name: "testResponseEvent1", type: EventType.analytics, source: EventSource.responseContent, data: nil)
let pairedResponseEvent = requestEvent.createResponseEvent(name: "testPairedResponseEvent1", type: EventType.analytics, source: EventSource.responseContent, data: nil)

// test
eventHub.registerEventListener(type: EventType.wildcard, source: EventSource.wildcard) { event in
if event.source == EventSource.responseContent {
expectation.fulfill()
}
}

eventHub.start()
eventHub.dispatch(event: responseEvent)
eventHub.dispatch(event: pairedResponseEvent)

// verify
wait(for: [expectation], timeout: 1)
}

func testEventHubTestResponseListener() {
// setup
let expectation = XCTestExpectation(description: "Response listener is invoked exactly once")
Expand Down
1 change: 0 additions & 1 deletion AEPServices/Sources/utility/unzip/URL+Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
governing permissions and limitations under the License.
*/


extension URL {
///
/// Validates a URL against a Zip Slip attack. Simply checks if there is any sort of traversal attempted in the url
Expand Down