Skip to content

Commit

Permalink
fix: forbidHTML doesn't affect
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnily03 committed Jul 31, 2024
1 parent 588227a commit 73c9cba
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cf-worker-mirrors",
"version": "2.0.0",
"version": "2.0.1",
"private": true,
"scripts": {
"deploy": "wrangler deploy -c wrangler.deploy.toml --env production",
Expand Down
9 changes: 4 additions & 5 deletions src/mw/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { createMiddleware } from "hono/factory";
import type { Env, BlankEnv, MiddlewareHandler } from "hono/types";
import type { Context } from "hono";
import { pathStartsWith, isContentType } from "@/utils";
import { parseFunctional, pathStartsWith, isContentType } from "@/utils";

type ProxyRewrite<T extends Env> = (c: Context<T>, path: string, url: string) => string
const parseFunctional = <T>(v: T | (() => T)): T => typeof v === "function" ? (v as CallableFunction)() : v

interface ProxyOptions {
forbidHTML: Functional<boolean>
Expand All @@ -18,8 +17,8 @@ export function changeOrigin<T extends Env = BlankEnv>(origin: string | ProxyRew
export function changeOrigin<T extends Env = BlankEnv>(origin: string | ProxyRewrite<T>, mount_path?: string, options?: Partial<ProxyOptions>): MiddlewareHandler<any, string, {}>
export function changeOrigin<T extends Env = BlankEnv>(origin: string | ProxyRewrite<T>, arg: string | ProxyRewrite<T> = '', options?: Partial<ProxyOptions>) {
const opts: ProxyOptions = Object.assign({}, defaultOptions, options)
opts.forbidHTML = parseFunctional(opts.forbidHTML)
return createMiddleware<T>(async (c, next) => {
opts.forbidHTML = parseFunctional(opts.forbidHTML)
let newpath = ''
if (typeof arg === "undefined") {
newpath = c.req.path
Expand Down Expand Up @@ -58,8 +57,8 @@ export function changeOrigin<T extends Env = BlankEnv>(origin: string | ProxyRew

export function rewriteURL<T extends Env = BlankEnv>(rewrite: ProxyRewrite<T>, options?: Partial<ProxyOptions>): MiddlewareHandler<any, string, {}> {
const opts: ProxyOptions = Object.assign({}, defaultOptions, options)
opts.forbidHTML = parseFunctional(opts.forbidHTML)
return createMiddleware<T>(async (c, next) => {
opts.forbidHTML = parseFunctional(opts.forbidHTML)
let newurl = rewrite(c, c.req.path, c.req.url)
if (!newurl) return await next()
const resp = await fetch(newurl, {
Expand Down Expand Up @@ -113,8 +112,8 @@ export function _testURLProtocol(url: string): "invalid" | "miss_protocol" | "ha

export function forwardPath<T extends Env = BlankEnv>(options?: Partial<ProxyForwardOptions>): MiddlewareHandler<any, string, {}> {
const opts: ProxyForwardOptions = Object.assign({}, defaultForwardOptions, options)
opts.forbidHTML = parseFunctional(opts.forbidHTML)
return createMiddleware<T>(async (c, next) => {
opts.forbidHTML = parseFunctional(opts.forbidHTML)
// format url to standard
let next_url = opts.forwardUrl || c.req.path.substring(1)
const thisUrlObj = new URL(c.req.url, "http://localhost")
Expand Down
3 changes: 2 additions & 1 deletion src/mw/subdomain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from 'hono';
import type { Env, BlankEnv, MiddlewareHandler } from 'hono/types';
import { createMiddleware } from 'hono/factory';
import { parseFunctional } from '@/utils';

type SubdomainRouteElem<T extends Env = BlankEnv> = {
sub: string | string[],
Expand All @@ -19,7 +20,7 @@ const defaultOptions: SubdomainRouteOptions = {
export default function subdomain<T extends Env = BlankEnv>(elem: SubdomainRouteElem<T>[], options: Partial<SubdomainRouteOptions> = {}) {
const opts = Object.assign({}, defaultOptions, options)
return createMiddleware<T>(async (c, next) => {
if (typeof opts.domains === 'function') opts.domains = opts.domains()
opts.domains = parseFunctional(opts.domains)
if (typeof opts.domains === 'undefined' || opts.domains.length === 0) return await next()
for (let e of elem) {
const urlobj = new URL(c.req.raw.url, 'http://localhost')
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export function isContentType(headers: Headers, t: string): boolean {
return contentTypes.some(ct => ct.split(";")[0].trim() === t)
}

export const parseFunctional = <T>(v: T | (() => T)): T => typeof v === "function" ? (v as CallableFunction)() : v

export async function hmac_sha256(key: ArrayBuffer, data: ArrayBuffer) {
const key_1 = await crypto.subtle.importKey(
'raw',
Expand Down
4 changes: 2 additions & 2 deletions wrangler.deploy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ compatibility_date = "2024-07-25"
name = "mirrors"

[env.development.vars]
VERSION = "2.0.0"
VERSION = "2.0.1"
SERVICE_NAME = "cf-worker-mirrors"
DOMAINS = []
FORBID_HTML = true
Expand All @@ -18,7 +18,7 @@ name = "mirrors"
minify = true

[env.production.vars]
VERSION = "2.0.0"
VERSION = "2.0.1"
SERVICE_NAME = "cf-worker-mirrors"
DOMAINS = []
FORBID_HTML = true
Expand Down

0 comments on commit 73c9cba

Please sign in to comment.