-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add jwt nginx modules * feat: verify jwt and check if the request domain is allowed * feat: add CORS headers to requests that fail auth. * ci: add jwt public key to container * test: add jwt integration tests * feat: allow no origin header if jwt allow_list has *
- Loading branch information
Showing
8 changed files
with
195 additions
and
53 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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiVrZNLTZgPFkyBXI2MDM13e+tmKf | ||
w82SnU183R6CczlsjO4qCTp3Xni+jBUri/5Ng34GZQfljtzZfDMfo2hHRw== | ||
-----END PUBLIC KEY----- |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import crypto from "crypto"; | ||
|
||
const ipfsRegex = /^\/ipfs\/(\w+)(\/?.*)/; | ||
|
||
function isAllowedRequest(req) { | ||
const matches = req.uri.match(ipfsRegex); | ||
if (!matches) { | ||
return req.internalRedirect("@node_backend"); | ||
} | ||
const cid = matches[1]; | ||
|
||
if (isBadBitsCid(cid)) { | ||
return req.return(410); | ||
} | ||
|
||
if (!isAllowedDomain(req)) { | ||
return req.return(403); | ||
} | ||
|
||
req.internalRedirect("@node_backend"); | ||
} | ||
|
||
// TODO implement matching CID paths | ||
// TODO convert CID v0 to CID v1 | ||
// implementation ref: https://github.com/protocol/bifrost-infra/blob/af46340bd830728b38a0ea632ca517d04277f78c/ansible/roles/nginx_conf_denylist/files/lua/helpers.lua#L80 | ||
function isBadBitsCid(cid) { | ||
// check if root hash(`CID/`) is blocked via denylist.json | ||
const hashedCID = crypto | ||
.createHash("sha256") | ||
.update(cid + "/") | ||
.digest("hex"); | ||
|
||
/* eslint-disable-next-line no-undef */ | ||
return hashedCID in denylist; | ||
} | ||
|
||
function isAllowedDomain(req) { | ||
const allowListStr = req.variables.jwt_claim_allow_list; | ||
if (!allowListStr) { | ||
return false; | ||
} | ||
|
||
let allowList; | ||
try { | ||
allowList = JSON.parse(allowListStr); | ||
} catch (err) { | ||
return false; | ||
} | ||
|
||
if (allowList.includes("*")) { | ||
return true; | ||
} | ||
|
||
// Only browser requests are allowed for now. | ||
const requestOrigin = req.variables.http_origin; | ||
if (!requestOrigin) { | ||
return false; | ||
} | ||
const requestDomain = requestOrigin.replace(/^https?:\/\//, ""); | ||
|
||
const isAllowedDomain = allowList.some((domain) => domain === requestDomain); | ||
|
||
return isAllowedDomain; | ||
} | ||
|
||
function findJWT(req) { | ||
const jwtQuery = req.variables.arg_jwt; | ||
|
||
let jwtHeader = ""; | ||
const authHeader = req.variables.http_authorization; | ||
if (authHeader) { | ||
jwtHeader = authHeader.replace("Bearer ", ""); | ||
} | ||
|
||
return jwtQuery || jwtHeader; | ||
} | ||
|
||
export default { isAllowedRequest, findJWT }; |
This file was deleted.
Oops, something went wrong.
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