-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web-server): シンプルログ出力でユーザー名と書き込み日時を含めない設定を追加
- Loading branch information
Showing
4 changed files
with
135 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { div, generateHtml, HtmlObject } from './generateHtml'; | ||
|
||
describe('generateHtml', () => { | ||
it('tests XSS', () => { | ||
const htmlObject: HtmlObject = { type: div, children: '<script>alert("!")</script>' }; | ||
const htmlString = generateHtml(htmlObject); | ||
expect(htmlString).not.toContain('<script>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { escape } from 'html-escaper'; | ||
|
||
export const div = 'div'; | ||
export const span = 'span'; | ||
type Type = typeof div | typeof span; | ||
|
||
export type HtmlObject = { | ||
type: Type; | ||
className?: string; | ||
style?: string; | ||
children?: string | HtmlObject[]; | ||
}; | ||
|
||
export const generateHtml = ({ type, className, style, children }: HtmlObject) => { | ||
let childrenAsHtml: string; | ||
if (children == null || typeof children === 'string') { | ||
childrenAsHtml = children == null ? '' : escape(children); | ||
} else { | ||
childrenAsHtml = ''; | ||
children.forEach(c => { | ||
childrenAsHtml = `${childrenAsHtml}${generateHtml(c)}`; | ||
}); | ||
} | ||
let startTag = type; | ||
if (className != null) { | ||
startTag += ` class="${className}"`; | ||
} | ||
if (style != null) { | ||
startTag += ` style="${style}"`; | ||
} | ||
return `<${startTag}>${childrenAsHtml}</${type}>`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters