Skip to content

Commit

Permalink
Merge pull request #5711 from thc202/spider/parsers-ctx
Browse files Browse the repository at this point in the history
spider: expose the context to the parsers
  • Loading branch information
kingthorin authored Sep 5, 2024
2 parents dd54e30 + 76eaa50 commit e700138
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion addOns/spider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased
### Added
- Allow the parsers to obtain the user being used by/in the current spidering scan (Issue 7739).
- Allow the parsers to obtain the context and user being used by/in the current spidering scan (Issue 8021 and 7739).

### Changed
- Maintenance changes.
Expand Down
10 changes: 10 additions & 0 deletions addOns/spider/src/main/java/org/zaproxy/addon/spider/Spider.java
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,16 @@ public void resume() {
}
}

/**
* Gets the context that will be used in the scanning.
*
* @return the context
* @since 0.12.0
*/
protected Context getScanContext() {
return scanContext;
}

/**
* Sets the spider so it will scan from the point of view of a user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ static void processResource(Spider parent, int depth, HttpMessage message) {
new ParseContext(
parent.getSpiderParam(),
parent.getExtensionSpider().getValueGenerator(),
parent.getScanContext(),
parent.getScanUser(),
message,
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.htmlparser.jericho.Source;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.addon.spider.SpiderParam;
import org.zaproxy.zap.model.Context;
import org.zaproxy.zap.model.ValueGenerator;
import org.zaproxy.zap.users.User;

Expand All @@ -37,6 +38,7 @@ public class ParseContext {
private final ValueGenerator valueGenerator;
private final HttpMessage httpMessage;
private final String path;
private final Context context;
private final User user;
private final int depth;
private String baseUrl;
Expand All @@ -59,14 +61,15 @@ public ParseContext(
HttpMessage httpMessage,
String path,
int depth) {
this(spiderParam, valueGenerator, null, httpMessage, path, depth);
this(spiderParam, valueGenerator, null, null, httpMessage, path, depth);
}

/**
* Constructs a {@code ParseContext} with the given values.
*
* @param spiderParam the spider options, must not be {@code null}.
* @param valueGenerator the value generator, must not be {@code null}.
* @param context the context being used by/in the current spidering scan.
* @param user the user being used by/in the current spidering scan.
* @param httpMessage the message, must not be {@code null}.
* @param path the path of the HTTP message.
Expand All @@ -78,12 +81,14 @@ public ParseContext(
public ParseContext(
SpiderParam spiderParam,
ValueGenerator valueGenerator,
Context context,
User user,
HttpMessage httpMessage,
String path,
int depth) {
this.spiderParam = Objects.requireNonNull(spiderParam);
this.valueGenerator = Objects.requireNonNull(valueGenerator);
this.context = context;
this.user = user;
this.httpMessage = Objects.requireNonNull(httpMessage);
this.path = path;
Expand All @@ -108,6 +113,16 @@ public ValueGenerator getValueGenerator() {
return valueGenerator;
}

/**
* Gets the context used by/in the spidering scan.
*
* @return the context, or {@code null}.
* @since 0.12.0
*/
public Context getContext() {
return context;
}

/**
* Gets the user used by/in the spidering scan.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.parosproxy.paros.network.HttpMessage;
import org.parosproxy.paros.network.HttpRequestHeader;
import org.zaproxy.addon.spider.SpiderParam;
import org.zaproxy.zap.model.Context;
import org.zaproxy.zap.model.ValueGenerator;
import org.zaproxy.zap.network.HttpResponseBody;
import org.zaproxy.zap.users.User;
Expand All @@ -45,6 +46,7 @@ class ParseContextUnitTest {

private SpiderParam spiderParam;
private ValueGenerator valueGenerator;
private Context context;
private User user;
private HttpMessage httpMessage;
private String responseData;
Expand All @@ -58,6 +60,7 @@ class ParseContextUnitTest {
void setup() throws Exception {
spiderParam = mock(SpiderParam.class);
valueGenerator = mock(ValueGenerator.class);
context = mock(Context.class);
user = mock(User.class);
httpMessage = mock(HttpMessage.class);
responseData = "<html></html>";
Expand Down Expand Up @@ -96,9 +99,12 @@ private void assertInitialConstructorValues() {
@Test
void shouldCreateWithGivenAdditionalValues() {
// Given / When
ctx = new ParseContext(spiderParam, valueGenerator, user, httpMessage, path, depth);
ctx =
new ParseContext(
spiderParam, valueGenerator, context, user, httpMessage, path, depth);
// Then
assertInitialConstructorValues();
assertThat(ctx.getContext(), is(sameInstance(context)));
assertThat(ctx.getUser(), is(sameInstance(user)));
}

Expand Down

0 comments on commit e700138

Please sign in to comment.