[Update] nonce 파라미터 감지 범위 늘림 및 nonce 파라미터 재사용에대한 검증 로직 추가

This commit is contained in:
tv0924@icloud.com 2025-06-04 16:02:42 +09:00
commit efb89c668c
3 changed files with 315 additions and 93 deletions

View file

@ -1,3 +1,6 @@
import type { SDK } from "caido:plugin";
import { Body, RequestSpec, type Request, type Response } from "caido:utils";
let instance: HttpUtils | null = null;
export class HttpUtils {
/**
@ -11,6 +14,14 @@ export class HttpUtils {
return instance;
}
encodeAndLower(value: string): string {
try {
return encodeURIComponent(value).toLowerCase();
} catch {
return value.toLowerCase();
}
}
/**
* URI
* @param value -
@ -47,12 +58,35 @@ export class HttpUtils {
return result;
}
getPathFromURI(uri: string): string | null {
uri = uri.toLowerCase();
try {
const urlObj = new URL(uri);
const path = urlObj.pathname;
return path ? decodeURIComponent(path) : null; // 경로가 없으면 null 반환
} catch (e) {
return null; // URL 파싱 실패 시 null 반환
}
}
getQueryFromURI(uri: string): string | null {
uri = uri.toLowerCase();
try {
const urlObj = new URL(uri);
const query = urlObj.search;
return query ? decodeURIComponent(query.slice(1)) : null; // 쿼리 문자열에서 ? 제거
} catch (e) {
return null; // URL 파싱 실패 시 null 반환
}
}
getQueryParamFromURI(uri: string, key: string): string | null {
uri = this.decodeAndLower(uri);
uri = uri.toLowerCase();
key = this.decodeAndLower(key);
try {
const urlObj = new URL(uri);
return urlObj.searchParams.get(key);
const param = urlObj.searchParams.get(key);
return param ? decodeURIComponent(param) : null;
} catch (e) {
return null;
}
@ -66,11 +100,12 @@ export class HttpUtils {
* @returns - , null
*/
getQueryParam(query: string, key: string): string | null {
query = this.decodeAndLower(query);
query = query.toLowerCase();
key = this.decodeAndLower(key);
const params = new URLSearchParams(query);
return params.get(key);
const targetParam = params.get(key);
return targetParam ? decodeURIComponent(targetParam) : null;
}
/**
@ -82,12 +117,12 @@ export class HttpUtils {
* @returns - "a=1&b=2&c=3..."
*/
setQueryParam(query: string, key: string, value: string): string {
query = this.decodeAndLower(query);
query = query.toLowerCase();
key = this.decodeAndLower(key);
value = this.decodeAndLower(value);
const params = new URLSearchParams(query);
params.set(key, value);
params.set(key, this.encodeAndLower(value));
return params.toString();
}
@ -99,7 +134,7 @@ export class HttpUtils {
* @returns -
*/
removeQueryParam(query: string, key: string): string {
query = this.decodeAndLower(query);
query = query.toLowerCase();
key = this.decodeAndLower(key);
const params = new URLSearchParams(query);
@ -109,6 +144,7 @@ export class HttpUtils {
// Headers
/**
* !! request.getHeader(`${key}`) .
* name에 .
* @param headers - Response.getHeaders()
* @param name - (: "location", "Content-Type")
@ -207,4 +243,89 @@ export class HttpUtils {
}
return filtered;
}
async resend(
sdk: SDK,
request: Request,
options?: {
headers?: Record<string, string | string[]>;
body?: Body;
method?: string;
query?: string;
}
): Promise<Response | null> {
try {
const spec = new RequestSpec(request.getUrl());
spec.setMethod(options?.method || request.getMethod() || "GET");
if (options?.query) {
spec.setQuery(options.query);
} else {
spec.setQuery(request.getQuery() || "");
}
const originBody = request.getBody();
if (options?.body) {
spec.setBody(options.body);
} else if (originBody) {
spec.setBody(originBody);
}
const headers = request.getHeaders();
if (options?.headers) {
// 기존 헤더에서 options.headers로 덮어쓰기
const newHeaders = this.lowerCaseAllHeaders({
...headers,
...options.headers,
});
for (const [key, value] of Object.entries(newHeaders)) {
spec.setHeader(key, Array.isArray(value) ? value.join(", ") : value);
}
} else {
// 기존 헤더 그대로 사용
for (const [key, value] of Object.entries(headers)) {
spec.setHeader(key, Array.isArray(value) ? value.join(", ") : value);
}
}
const result = await sdk.requests.send(spec);
return result.response ?? null;
} catch (error) {
sdk.console.error(
`Error resending request to ${request.getUrl()}: ${String(error)}`
);
return null;
}
}
async customFetch(
sdk: SDK,
url: string,
method?: string,
query?: string,
headers?: Record<string, string | string[]>,
body?: Body
): Promise<Response | null> {
try {
const spec = new RequestSpec(url);
spec.setMethod(method || "GET");
if (query) {
spec.setQuery(query);
}
if (body) {
spec.setBody(body);
}
for (const [key, value] of Object.entries(headers || {})) {
spec.setHeader(key, Array.isArray(value) ? value.join(", ") : value);
}
const result = await sdk.requests.send(spec);
return result.response ?? null;
} catch {
sdk.console.error(
`Error during custom fetch to ${url}: ${String(error)}`
);
return null;
}
}
}