-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(options): add allowMetaIPAddress option
- Loading branch information
Showing
3 changed files
with
75 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import * as assert from "assert"; | ||
import fetch from "node-fetch"; | ||
import { globalHttpAgent, RequestFilteringHttpAgent, useAgent, applyRequestFilter } from "../src/request-filtering-agent"; | ||
import { | ||
globalHttpAgent, | ||
RequestFilteringHttpAgent, | ||
useAgent, | ||
applyRequestFilter | ||
} from "../src/request-filtering-agent"; | ||
import * as http from "http"; | ||
|
||
const TEST_PORT = 12456; | ||
describe("request-filtering-agent", function () { | ||
describe("request-filtering-agent", function() { | ||
let close = () => { | ||
return Promise.resolve(); | ||
}; | ||
|
@@ -38,7 +43,7 @@ describe("request-filtering-agent", function () { | |
}); | ||
it("should request local ip address with allowPrivateIP: true", async () => { | ||
const agent = new RequestFilteringHttpAgent({ | ||
allowPrivateIP: true | ||
allowPrivateIPAddress: true | ||
}); | ||
const privateIPs = [ | ||
`http://127.0.0.1:${TEST_PORT}` | ||
|
@@ -59,7 +64,7 @@ describe("request-filtering-agent", function () { | |
keepAlive: true | ||
}); | ||
const agentWithFiltering = applyRequestFilter(agent, { | ||
allowPrivateIP: true | ||
allowPrivateIPAddress: true | ||
}); | ||
const privateIPs = [ | ||
`http://127.0.0.1:${TEST_PORT}` | ||
|
@@ -71,17 +76,39 @@ describe("request-filtering-agent", function () { | |
timeout: 2000 | ||
}); | ||
} catch (error) { | ||
assert.fail(new Error("should fetch, because it is allow")); | ||
assert.fail(new Error("should fetch, because it is allow, error" + error)); | ||
} | ||
} | ||
}); | ||
it("0.0.0.0 and :: is metaAddress, it is disabled by default", async () => { | ||
const agent = new RequestFilteringHttpAgent(); | ||
const disAllowedIPs = [ | ||
`http://0.0.0.0:${TEST_PORT}`, | ||
`http://[::]:${TEST_PORT}` | ||
]; | ||
for (const ipAddress of disAllowedIPs) { | ||
try { | ||
await fetch(ipAddress, { | ||
agent, | ||
timeout: 2000 | ||
}); | ||
throw new ReferenceError("SHOULD NOT BE CALLED:" + ipAddress); | ||
} catch (error) { | ||
if (error instanceof ReferenceError) { | ||
assert.fail(error); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
it("should allow http://127.0.0.1, but other private ip is disallowed", async () => { | ||
const agent = new RequestFilteringHttpAgent({ | ||
allowIPAddressList: ["127.0.0.1"], | ||
allowPrivateIP: false | ||
allowPrivateIPAddress: false | ||
}); | ||
const privateIPs = [ | ||
`http://127.0.0.1:${TEST_PORT}` | ||
`http://127.0.0.1:${TEST_PORT}`, | ||
`http://localhost:${TEST_PORT}` | ||
]; | ||
for (const ipAddress of privateIPs) { | ||
try { | ||
|
@@ -90,7 +117,7 @@ describe("request-filtering-agent", function () { | |
timeout: 2000 | ||
}); | ||
} catch (error) { | ||
assert.fail(new Error("should fetch, because it is allow")); | ||
assert.fail(new Error("should fetch, because it is allow, error" + error)); | ||
} | ||
} | ||
const disAllowedPrivateIPs = [ | ||
|
@@ -112,6 +139,7 @@ describe("request-filtering-agent", function () { | |
}); | ||
it("should not request because Socket is closed", async () => { | ||
const privateIPs = [ | ||
`http://0.0.0.0:${TEST_PORT}`, // 0.0.0.0 is special | ||
`http://127.0.0.1:${TEST_PORT}`, // | ||
`http://[email protected]:${TEST_PORT}` // | ||
]; | ||
|