Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(static): safe decode path #1459

Merged
merged 4 commits into from
Jul 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions src/runtime/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,41 @@ export default eventHandler((event) => {
return;
}

let id = decodeURIComponent(
withLeadingSlash(
withoutTrailingSlash(parseURL(event.node.req.url).pathname)
)
);
let id: string;
let asset;
try {
id = decodeURIComponent(
withLeadingSlash(
withoutTrailingSlash(parseURL(event.node.req.url).pathname)
)
);

const encodingHeader = String(
event.node.req.headers["accept-encoding"] || ""
);
const encodings = [
...encodingHeader
.split(",")
.map((e) => EncodingMap[e.trim()])
.filter(Boolean)
.sort(),
"",
];
if (encodings.length > 1) {
event.node.res.setHeader("Vary", "Accept-Encoding");
}
const encodingHeader = String(
event.node.req.headers["accept-encoding"] || ""
);
const encodings = [
...encodingHeader
.split(",")
.map((e) => EncodingMap[e.trim()])
.filter(Boolean)
.sort(),
"",
];
if (encodings.length > 1) {
event.node.res.setHeader("Vary", "Accept-Encoding");
}

for (const encoding of encodings) {
for (const _id of [id + encoding, joinURL(id, "index.html" + encoding)]) {
const _asset = getAsset(_id);
if (_asset) {
asset = _asset;
id = _id;
break;
for (const encoding of encodings) {
for (const _id of [id + encoding, joinURL(id, "index.html" + encoding)]) {
const _asset = getAsset(_id);
if (_asset) {
asset = _asset;
id = _id;
break;
}
}
}
}
} catch {}
danielroe marked this conversation as resolved.
Show resolved Hide resolved

if (!asset) {
if (isPublicAssetURL(id)) {
Expand Down