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

Add example of XSS vulnerability #24

Merged
merged 26 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8609ea8
added an XSS example
dschwarzc Oct 19, 2022
7e99792
Added XSS solution example with markdown
dschwarzc Oct 23, 2022
9956429
Merge branch 'main' of github.com:upleveled/next-js-postgres-broken-s…
karlhorky Oct 24, 2022
99f374e
Revert allowing cookie access from JS
karlhorky Oct 24, 2022
552f4e0
Lock version, run `yarn`
karlhorky Oct 24, 2022
547b398
Move error to CommonContent component
karlhorky Oct 24, 2022
05a59a1
Make example 4 files easier to compare
karlhorky Oct 24, 2022
3e83c88
Add dompurify types, fix import style
karlhorky Oct 25, 2022
976cdcc
Format code, remove extra p element
karlhorky Oct 25, 2022
d3e7721
Merge branch 'main' of github.com:upleveled/next-js-postgres-broken-s…
karlhorky Oct 25, 2022
c8c0d8d
Set NODE_ENV to production
karlhorky Oct 25, 2022
3d5e89f
Move NODE_ENV to only build step
karlhorky Oct 25, 2022
2b7662b
Export null-returning component for Next.js build
karlhorky Oct 25, 2022
b6df284
Add missing ESLint dependencies, fix problems
karlhorky Oct 25, 2022
2cd00ff
Make blog_posts.user_id NOT NULL
karlhorky Oct 25, 2022
24971a9
Improve clarity of naming, prose and types
karlhorky Oct 25, 2022
4a3f632
Use tsconfig from UpLeveled ESLint config
karlhorky Oct 25, 2022
f5fea9d
Add jsx: preserve to tsconfig.json for Next.js
karlhorky Oct 25, 2022
da3dbe4
Make blog post title and content more descriptive
karlhorky Oct 25, 2022
87e7fcf
Add new screenshot and figcaptions to readme
karlhorky Oct 25, 2022
7b9d1e6
Fix paths and naming
karlhorky Oct 25, 2022
758e298
Fix naming and paths for Cross-Site Scripting
karlhorky Oct 25, 2022
2c42e6e
Fix component names
karlhorky Oct 26, 2022
c77f4a7
Fix component names
karlhorky Oct 26, 2022
1ee6254
Improve clarity of comment
karlhorky Oct 26, 2022
b89d8a8
Add type to getServerSideProps return values
karlhorky Oct 26, 2022
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
12 changes: 12 additions & 0 deletions database/blogPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ export async function getBlogPostsBySessionToken(sessionToken: string) {
`;
return blogPosts;
}

export async function getSpecialBlogPosts(id: number) {
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
const blogPosts = await sql<BlogPost[]>`
SELECT
*
FROM
blog_posts
WHERE
id = ${id}
`;
return blogPosts;
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 14 additions & 0 deletions migrations/003-create-table-blog-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ const blogPosts = [
is_published: true,
user_id: 2,
},
{
title: "Special Blogpost 2",
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
text_content:
'This is a <b>special</b> one <img src="x" onerror=alert(document.cookie) />',
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
is_published: true,
user_id: 1,
},
{
title: "Special Blogpost",
text_content:
'This is another **special one** - markdown formatted<img src="x" onerror=alert(document.cookie) />',
is_published: true,
user_id: 1,
},
];

export async function up(sql: Sql<Record<string, string>>) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"@types/dotenv-safe": "8.1.2",
"bcrypt": "5.1.0",
"cookie": "0.5.0",
"dompurify": "^2.4.0",
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
"dotenv-cli": "6.0.0",
"dotenv-safe": "8.2.0",
"ley": "0.8.1",
"next": "12.3.1",
"postgres": "3.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-markdown": "^8.0.3",
"sharp": "0.31.1",
"tsm": "2.2.2"
},
Expand Down
7 changes: 7 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export default function App({ Component, pageProps }: AppProps) {
<a style={{ marginRight: 15 }}>Example 5</a>
</LinkIfNotCurrent>

<LinkIfNotCurrent
href="/example-6-xss/vulnerable"
baseHref="/example-6-xss/"
>
<a style={{ marginRight: 15 }}>Example 6</a>
</LinkIfNotCurrent>
karlhorky marked this conversation as resolved.
Show resolved Hide resolved

<div style={{ marginLeft: 'auto' }}>
{!user && (
<>
Expand Down
53 changes: 53 additions & 0 deletions pages/example-6-xss/common.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import LinkIfNotCurrent from '../../components/LinkIfNotCurrent';
import { BlogPost } from '../../database/blogPosts';

type Props =
| {
error: string;
}
| {
blogPosts: BlogPost[];
};

export function CommonContent(props: Props) {
return (
<>
<h1>XSS - getServerSideProps</h1>
karlhorky marked this conversation as resolved.
Show resolved Hide resolved

<ul>
<li>
<LinkIfNotCurrent href="/example-6-xss/vulnerable">
Vulnerable
</LinkIfNotCurrent>
</li>
<li>
<LinkIfNotCurrent href="/example-6-xss/solution-1">
Solution 1
</LinkIfNotCurrent>
</li>
<li>
<LinkIfNotCurrent href="/example-6-xss/solution-2">
Solution 2
</LinkIfNotCurrent>
</li>
<li>
<LinkIfNotCurrent href="/example-6-xss/solution-3">
Solution 3
</LinkIfNotCurrent>
</li>
</ul>

<hr />

<div>
The following blog posts should only be visible for logged-in users.
<br />
<br />
If a user is not logged in, an error message should appear.
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
</div>

<h2>Blog Posts</h2>
</>
);
}

40 changes: 40 additions & 0 deletions pages/example-6-xss/solution-1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BlogPost, getSpecialBlogPosts } from '../../database/blogPosts';
import { CommonContent } from './common';

type Props =
| {
error: string;
}
| {
blogPosts: BlogPost[];
};

export default function MissingAuthenticationGssp(props: Props) {
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
return (
<div>
<CommonContent />

{'error' in props && <div style={{ color: 'red' }}>{props.error}</div>}

{'blogPosts' in props &&
props.blogPosts.map((blogPost) => {
return (
<div key={`blog-post-${blogPost.id}`}>
<h2>{blogPost.title}</h2>
<div>Published: {String(blogPost.isPublished)}</div>
<div>{blogPost.textContent}</div>
</div>
);
})}
</div>
);
}

export async function getServerSideProps() {
const blogPosts = await getSpecialBlogPosts(6);
return {
props: {
blogPosts: blogPosts,
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
41 changes: 41 additions & 0 deletions pages/example-6-xss/solution-2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { BlogPost, getSpecialBlogPosts } from '../../database/blogPosts';
import { CommonContent } from './common';
import DOMPurify from "dompurify"
karlhorky marked this conversation as resolved.
Show resolved Hide resolved

type Props =
| {
error: string;
}
| {
blogPosts: BlogPost[];
};

export default function MissingAuthenticationGssp(props: Props) {
return (
<div>
<CommonContent />

{'error' in props && <div style={{ color: 'red' }}>{props.error}</div>}

{'blogPosts' in props &&
props.blogPosts.map((blogPost) => {
return (
<div key={`blog-post-${blogPost.id}`}>
<h2>{blogPost.title}</h2>
<div>Published: {String(blogPost.isPublished)}</div>
<div><p dangerouslySetInnerHTML={{__html: DOMPurify.sanitize(blogPost.textContent)}}></p></div>
</div>
);
})}
</div>
);
}

export async function getServerSideProps() {
const blogPosts = await getSpecialBlogPosts(6);
return {
props: {
blogPosts: blogPosts,
},
};
}
45 changes: 45 additions & 0 deletions pages/example-6-xss/solution-3.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BlogPost, getSpecialBlogPosts } from '../../database/blogPosts';
import { CommonContent } from './common';
import ReactMarkdown from "react-markdown"

type Props =
| {
error: string;
}
| {
blogPosts: BlogPost[];
};

export default function MissingAuthenticationGssp(props: Props) {
return (
<div>
<CommonContent />

{'error' in props && <div style={{ color: 'red' }}>{props.error}</div>}

{'blogPosts' in props &&
props.blogPosts.map((blogPost) => {
// be careful which markdown library you use and how you use it
// by default the markdown standard supports html tags too
// so never assign markdown directly to innerHTML
// but the default usage of "ReactMarkdown" is safe
return (
<div key={`blog-post-${blogPost.id}`}>
<h2>{blogPost.title}</h2>
<div>Published: {String(blogPost.isPublished)}</div>
<ReactMarkdown children={blogPost.textContent}/>
</div>
);
})}
</div>
);
}

export async function getServerSideProps() {
const blogPosts = await getSpecialBlogPosts(7);
return {
props: {
blogPosts: blogPosts,
},
};
}
40 changes: 40 additions & 0 deletions pages/example-6-xss/vulnerable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { BlogPost, getSpecialBlogPosts } from '../../database/blogPosts';
import {CommonContent} from './common';

type Props =
| {
error: string;
}
| {
blogPosts: BlogPost[];
};

export default function MissingAuthenticationGssp(props: Props) {
return (
<div>
<CommonContent />

{'error' in props && <div style={{ color: 'red' }}>{props.error}</div>}
karlhorky marked this conversation as resolved.
Show resolved Hide resolved

{'blogPosts' in props &&
props.blogPosts.map((blogPost) => {
return (
<div key={`blog-post-${blogPost.id}`}>
<h2>{blogPost.title} - Vulnerable</h2>
<div>Published: {String(blogPost.isPublished)}</div>
<div><p dangerouslySetInnerHTML={{__html: blogPost.textContent}}></p></div>
karlhorky marked this conversation as resolved.
Show resolved Hide resolved
</div>
);
})}
</div>
);
}

export async function getServerSideProps() {
const blogPosts = await getSpecialBlogPosts(6);
return {
props: {
blogPosts: blogPosts,
},
};
}
5 changes: 5 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default function Home() {
Example 5: Data Exposure
</Link>
</li>
<li>
<Link href="/example-6-xss/vulnerable">
Example 6: XSS
</Link>
</li>
</ol>
</div>
);
Expand Down