Skip to content

Commit 96a851e

Browse files
allow for handling of redirects in toml resolver (#1053)
* allow for handling of redirects * correct unit tests for resolver * add maxRedirects to http client request config --------- Signed-off-by: KyleSmith19091 <[email protected]> Co-authored-by: George <[email protected]>
1 parent 3a4253a commit 96a851e

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ A breaking change will get clearly marked in this log.
66

77
## Unreleased
88

9+
### Added
10+
- `stellartoml-Resolver.resolve` now has a `allowedRedirects` option to configure the number of allowed redirects to follow when resolving a stellar toml file.
11+
912

1013
## [v13.0.0-rc.1](https://github.com/stellar/js-stellar-sdk/compare/v12.3.0...v13.0.0-rc.1)
1114

src/http-client/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface HttpClientRequestConfig<D = any> {
3232
headers?: HeadersInit;
3333
params?: Record<string, any>;
3434
maxContentLength?: number;
35+
maxRedirects?: number;
3536
cancelToken?: CancelToken;
3637
adapter?: (config: HttpClientRequestConfig) => Promise<HttpClientResponse>;
3738
}

src/stellartoml/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class Resolver {
5959

6060
return httpClient
6161
.get(`${protocol}://${domain}/.well-known/stellar.toml`, {
62+
maxRedirects: opts.allowedRedirects ?? 0,
6263
maxContentLength: STELLAR_TOML_MAX_SIZE,
6364
cancelToken: timeout
6465
? new CancelToken((cancel) =>
@@ -99,6 +100,7 @@ export namespace Api {
99100
export interface StellarTomlResolveOptions {
100101
allowHttp?: boolean;
101102
timeout?: number;
103+
allowedRedirects?: number;
102104
}
103105
export type Url = string;
104106
export type PublicKey = string;

test/unit/stellar_toml_resolver_test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,65 @@ FEDERATION_SERVER="https://api.stellar.org/federation"
184184
.then(() => tempServer.close());
185185
});
186186
});
187+
188+
it("rejects redirect response when allowedRedirects is not specified", function (done) {
189+
// Unable to create temp server in a browser
190+
if (typeof window != "undefined") {
191+
return done();
192+
}
193+
194+
let tempServer = http
195+
.createServer((req, res) => {
196+
res.writeHead(302, { location: "/redirect" });
197+
return res.end();
198+
})
199+
.listen(4444, () => {
200+
Resolver.resolve("localhost:4444", {
201+
allowHttp: true,
202+
})
203+
.then((response) => {
204+
should.fail();
205+
})
206+
.catch((e) => {
207+
expect(e).to.match(/Maximum number of redirects exceeded/);
208+
})
209+
.finally(() => {
210+
tempServer.close();
211+
done();
212+
});
213+
});
214+
});
215+
216+
it("returns handled redirect when allowedRedirects is specified", function (done) {
217+
if (typeof window != "undefined") {
218+
return done();
219+
}
220+
221+
let tempServer = http
222+
.createServer((req, res) => {
223+
if (req.url !== "/redirect") {
224+
res.writeHead(302, { location: "/redirect" });
225+
return res.end();
226+
}
227+
res.setHeader("Content-Type", "text/x-toml; charset=UTF-8");
228+
res.writeHead(200);
229+
res.end(`
230+
FEDERATION_SERVER="https://api.stellar.org/federation"
231+
`);
232+
})
233+
.listen(4444, () => {
234+
Resolver.resolve("localhost:4444", {
235+
allowHttp: true,
236+
allowedRedirects: 1,
237+
}).then((response) => {
238+
expect(response.FEDERATION_SERVER).equals(
239+
"https://api.stellar.org/federation",
240+
);
241+
}).finally(() => {
242+
tempServer.close();
243+
done();
244+
});
245+
});
246+
});
187247
});
188248
});

0 commit comments

Comments
 (0)