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: escape only few chars #236

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
75 changes: 45 additions & 30 deletions packages/backend/server/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ export function generateTemplateHtml(
<form id="mainForm" method="post" action="https://stackblitz.com/run" target="_self">
${Object.entries(files)
.map(
([
file,
contentOrUrl,
]) => `<input type="hidden" name="project[files][${encodeURIComponent(file)}]" value="${escapeHtml(contentOrUrl)}">`,
([file, contentOrUrl]) =>
`<input type="hidden" name="project[files][${escapeFilename(file)}]" value="${escapeHtml(contentOrUrl)}">`,
)
.join("\n")}
<input type="hidden" name="project[description]" value="generated by https://pkg.pr.new">
Expand All @@ -24,6 +22,25 @@ ${Object.entries(files)
</body></html>`;
}

const escapableChars = "[]";

function escapeFilename(file: string): string {
let encodedStr = "";

for (let i = 0; i < file.length; i++) {
const char = file[i];

// Only encode [ and ]
if (escapableChars.includes(char)) {
encodedStr += encodeURIComponent(char);
} else {
encodedStr += char;
}
}

return encodedStr;
}

// https://github.com/component/escape-html/blob/master/LICENSE

// (The MIT License)
Expand Down Expand Up @@ -51,51 +68,49 @@ ${Object.entries(files)
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var matchHtmlRegExp = /["'&<>]/
var matchHtmlRegExp = /["'&<>]/;

function escapeHtml (string: string) {
var str = '' + string
var match = matchHtmlRegExp.exec(str)
function escapeHtml(string: string) {
var str = "" + string;
var match = matchHtmlRegExp.exec(str);

if (!match) {
return str
return str;
}

var escape
var html = ''
var index = 0
var lastIndex = 0
var escape;
var html = "";
var index = 0;
var lastIndex = 0;

for (index = match.index; index < str.length; index++) {
switch (str.charCodeAt(index)) {
case 34: // "
escape = '&quot;'
break
escape = "&quot;";
break;
case 38: // &
escape = '&amp;'
break
escape = "&amp;";
break;
case 39: // '
escape = '&#39;'
break
escape = "&#39;";
break;
case 60: // <
escape = '&lt;'
break
escape = "&lt;";
break;
case 62: // >
escape = '&gt;'
break
escape = "&gt;";
break;
default:
continue
continue;
}

if (lastIndex !== index) {
html += str.substring(lastIndex, index)
html += str.substring(lastIndex, index);
}

lastIndex = index + 1
html += escape
lastIndex = index + 1;
html += escape;
}

return lastIndex !== index
? html + str.substring(lastIndex, index)
: html
return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
}
1 change: 1 addition & 0 deletions templates/example-2/src/+page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// nothing
1 change: 1 addition & 0 deletions templates/example-2/src/[route].jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// nothing
1 change: 1 addition & 0 deletions templates/example-2/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// nothing