Skip to content

Commit 35134aa

Browse files
authored
feat: restrict access to private packages based on user session (#1170)
* feat: restrict access to private packages based on user session * feat: enhance private package access control with NotFound page redirection * fix * wtf * fkd * final try * chore
1 parent f0a29d8 commit 35134aa

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

fake-snippets-api/routes/api/packages/get.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ export default withRouteSpec({
3434
})
3535
}
3636

37+
if (
38+
foundPackage.is_private &&
39+
auth?.github_username !== foundPackage.owner_github_username
40+
) {
41+
return ctx.error(404, {
42+
error_code: "package_not_found",
43+
message: `Package not found (searched using ${JSON.stringify(
44+
req.commonParams,
45+
)})`,
46+
})
47+
}
48+
3749
return ctx.json({
3850
ok: true,
3951
package: {

src/pages/package-editor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import { useGetFsMapHashForPackage } from "@/hooks/use-get-fsmap-hash-for-packag
1010

1111
export const EditorPage = () => {
1212
const { packageId } = useCurrentPackageId()
13-
const { data: pkg, isLoading, error } = usePackage(packageId)
13+
const { data: pkg, error } = usePackage(packageId)
1414
const fsMapHash = useGetFsMapHashForPackage(
1515
pkg?.latest_package_release_id ?? "",
1616
)
1717
const uuid4RegExp = new RegExp(
1818
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/,
1919
)
20+
2021
return (
2122
<div className="overflow-x-hidden">
2223
<Helmet>

src/pages/view-package.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import RepoPageContent from "@/components/ViewPackagePage/components/repo-page-content"
2-
import { useCurrentPackageInfo } from "@/hooks/use-current-package-info"
32
import { usePackageFiles } from "@/hooks/use-package-files"
43
import { usePackageRelease } from "@/hooks/use-package-release"
54
import { useLocation, useParams } from "wouter"
65
import { Helmet } from "react-helmet-async"
76
import { useEffect, useState } from "react"
87
import NotFoundPage from "./404"
8+
import { useCurrentPackageId } from "@/hooks/use-current-package-id"
9+
import { usePackage } from "@/hooks/use-package"
910

1011
export const ViewPackagePage = () => {
11-
const { packageInfo } = useCurrentPackageInfo()
12+
const {
13+
packageId,
14+
error: packageIdError,
15+
isLoading: isLoadingPackageId,
16+
} = useCurrentPackageId()
17+
const { data: packageInfo } = usePackage(packageId)
1218
const { author, packageName } = useParams()
1319
const [, setLocation] = useLocation()
14-
const [isNotFound, setIsNotFound] = useState(false)
15-
1620
const {
1721
data: packageRelease,
1822
error: packageReleaseError,
@@ -25,14 +29,12 @@ export const ViewPackagePage = () => {
2529
const { data: packageFiles } = usePackageFiles(
2630
packageRelease?.package_release_id,
2731
)
28-
useEffect(() => {
29-
if (isLoadingPackageRelease) return
30-
if (packageReleaseError?.status == 404) {
31-
setIsNotFound(true)
32-
}
33-
}, [isLoadingPackageRelease, packageReleaseError])
3432

35-
if (isNotFound) {
33+
if (!isLoadingPackageId && packageIdError) {
34+
return <NotFoundPage heading="Package Not Found" />
35+
}
36+
37+
if (!isLoadingPackageRelease && packageReleaseError?.status == 404) {
3638
return <NotFoundPage heading="Package Not Found" />
3739
}
3840

0 commit comments

Comments
 (0)