11import {
22 CheckedUrl ,
33 parseUrl ,
4- validatePath ,
54 validateHttpMethod ,
5+ validatePath ,
66 validateUrl ,
77} from './validate-url' ;
88import { Web2Json_Request } from '../../dtos/attestation-types/Web2Json.dto' ;
@@ -18,6 +18,7 @@ import { validateJqFilter } from './validate-jq';
1818import { ParamType } from 'ethers' ;
1919import { parseAndValidateAbiType } from './validate-abi' ;
2020import { Web2JsonValidationError } from './utils' ;
21+ import { PUBLIC_WEB2 } from '../../config/web2/web2-json-test-sources' ;
2122
2223export interface ParsedRequestBody {
2324 validSourceUrl : CheckedUrl ;
@@ -47,18 +48,6 @@ export async function parseAndValidateRequest(
4748 `Invalid source URL: ${ ( e as Error ) . message } ` ,
4849 ) ;
4950 }
50- const endpoint = source . endpoints . find ( ( e ) => e . host === parsedUrl . hostname ) ;
51- if ( ! endpoint ) {
52- throw new Web2JsonValidationError (
53- AttestationResponseStatus . INVALID_SOURCE_URL ,
54- 'Source URL host not allowed' ,
55- ) ;
56- }
57- // validate endpoint path
58- validatePath ( parsedUrl , endpoint ) ;
59- // validate HTTP method
60- const sourceMethod = requestBody . httpMethod ;
61- validateHttpMethod ( sourceMethod , endpoint . methods ) ;
6251 // validate headers
6352 const sourceHeaders =
6453 parseJsonWithDepthAndKeysValidation (
@@ -80,37 +69,58 @@ export async function parseAndValidateRequest(
8069 AttestationResponseStatus . INVALID_QUERY_PARAMS ,
8170 ) ?? { } ;
8271
83- // validate jq filter length and content
84- const jqFilter = requestBody . postProcessJq ;
85- validateJqFilter ( requestBody . postProcessJq , securityParams . maxJqFilterLength ) ;
86- // validate ABI signature
87- const abiType = parseAndValidateAbiType (
88- requestBody . abiSignature ,
89- securityParams . maxAbiSignatureLength ,
90- ) ;
72+ const sourceMethod = requestBody . httpMethod ;
9173
92- // Inject authentication token from endpoint.auth if configured
93- if ( endpoint . auth ) {
94- const token = process . env [ endpoint . auth . env ] ;
95- if ( ! token ) {
96- throw Error (
97- `Missing API key in environment variable ${ endpoint . auth . env } for host ${ endpoint . host } ` ,
74+ if ( source === PUBLIC_WEB2 ) {
75+ // validate HTTP method
76+ validateHttpMethod ( sourceMethod , [ HTTP_METHOD . GET , HTTP_METHOD . POST ] ) ;
77+ } else {
78+ const endpoint = source . endpoints . find (
79+ ( e ) => e . host === parsedUrl . hostname ,
80+ ) ;
81+ if ( ! endpoint ) {
82+ throw new Web2JsonValidationError (
83+ AttestationResponseStatus . INVALID_SOURCE_URL ,
84+ 'Source URL host not allowed' ,
9885 ) ;
9986 }
10087
101- if ( endpoint . auth . type === AuthType . BEARER ) {
102- sourceHeaders [ 'Authorization' ] = `Bearer ${ token } ` ;
103- } else if ( endpoint . auth . type === AuthType . APIKEY ) {
104- if ( endpoint . auth . header ) {
105- sourceHeaders [ endpoint . auth . header ] = token ;
88+ // validate endpoint path
89+ validatePath ( parsedUrl , endpoint ) ;
90+ // validate HTTP method
91+ validateHttpMethod ( sourceMethod , endpoint . methods ) ;
92+ // Inject authentication token from endpoint.auth if configured
93+ if ( endpoint . auth ) {
94+ const token = process . env [ endpoint . auth . env ] ;
95+ if ( ! token ) {
96+ throw Error (
97+ `Missing API key in environment variable ${ endpoint . auth . env } for host ${ endpoint . host } ` ,
98+ ) ;
99+ }
100+
101+ if ( endpoint . auth . type === AuthType . BEARER ) {
102+ sourceHeaders [ 'Authorization' ] = `Bearer ${ token } ` ;
103+ } else if ( endpoint . auth . type === AuthType . APIKEY ) {
104+ if ( endpoint . auth . header ) {
105+ sourceHeaders [ endpoint . auth . header ] = token ;
106+ } else {
107+ sourceQueryParams [ endpoint . auth . query ] = token ;
108+ }
106109 } else {
107- sourceQueryParams [ endpoint . auth . query ] = token ;
110+ throw new Error ( `Unsupported auth type for host ${ endpoint . host } ` ) ;
108111 }
109- } else {
110- throw new Error ( `Unsupported auth type for host ${ endpoint . host } ` ) ;
111112 }
112113 }
113114
115+ // validate jq filter length and content
116+ const jqFilter = requestBody . postProcessJq ;
117+ validateJqFilter ( requestBody . postProcessJq , securityParams . maxJqFilterLength ) ;
118+ // validate ABI signature
119+ const abiType = parseAndValidateAbiType (
120+ requestBody . abiSignature ,
121+ securityParams . maxAbiSignatureLength ,
122+ ) ;
123+
114124 // validate body
115125 const sourceBody = parseJsonWithDepthAndKeysValidation (
116126 requestBody . body ,
0 commit comments