Skip to content

Commit

Permalink
Update ghes deprecation code (#53910)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachmari authored Jan 17, 2025
1 parent a3b00ef commit 8ee5cd7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/archives/middleware/archived-enterprise-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ export default async function archivedEnterpriseVersions(
// Releases 3.2 and higher contain image asset paths with the
// old Azure Blob Storage URL. These need to be rewritten to
// the new archived enterprise repo URL.
if (versionSatisfiesRange(requestedVersion, `>=${firstReleaseStoredInBlobStorage}`)) {
if (
versionSatisfiesRange(requestedVersion, `>=${firstReleaseStoredInBlobStorage}`) &&
versionSatisfiesRange(requestedVersion, `<=3.9`)
) {
// `x-host` is a custom header set by Fastly.
// GLB automatically deletes the `x-forwarded-host` header.
const host = req.get('x-host') || req.get('x-forwarded-host') || req.get('host')
Expand Down
6 changes: 4 additions & 2 deletions src/frame/middleware/helmet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const DEFAULT_OPTIONS = {
// For use during development only!
// `unsafe-eval` allows us to use a performant webpack devtool setting (eval)
// https://webpack.js.org/configuration/devtool/#devtool
scriptSrc: ["'self'", 'data:', isDev && "'unsafe-eval'"].filter(Boolean) as string[],
scriptSrc: [...GITHUB_DOMAINS, "'self'", 'data:', isDev && "'unsafe-eval'"].filter(
Boolean,
) as string[],
scriptSrcAttr: ["'self'"],
frameSrc: [
...GITHUB_DOMAINS,
Expand All @@ -49,7 +51,7 @@ const DEFAULT_OPTIONS = {
'https://www.youtube-nocookie.com',
].filter(Boolean) as string[],
frameAncestors: isDev ? ['*'] : [...GITHUB_DOMAINS],
styleSrc: ["'self'", "'unsafe-inline'", 'data:'],
styleSrc: [...GITHUB_DOMAINS, "'self'", "'unsafe-inline'", 'data:'],
childSrc: ["'self'"], // exception for search in deprecated GHE versions
manifestSrc: ["'self'"],
upgradeInsecureRequests: isDev ? null : [],
Expand Down
11 changes: 2 additions & 9 deletions src/ghes-releases/scripts/archive-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { RewriteAssetPathsPlugin } from '@/ghes-releases/scripts/rewrite-asset-p
const port = '4001'
const host = `http://localhost:${port}`
const version = EnterpriseServerReleases.oldestSupported
const REMOTE_ENTERPRISE_STORAGE_URL = 'https://githubdocs.azureedge.net/enterprise'
const GH_PAGES_URL = `https://github.github.com/docs-ghes-${version}`

// Once page-data.js is converted to TS,
// we can import the more comprehesive type
Expand Down Expand Up @@ -108,14 +108,7 @@ async function main() {
directory: tmpArchivalDirectory,
filenameGenerator: 'bySiteStructure',
requestConcurrency: 6,
plugins: [
new RewriteAssetPathsPlugin(
version,
tmpArchivalDirectory,
localDev,
REMOTE_ENTERPRISE_STORAGE_URL,
),
],
plugins: [new RewriteAssetPathsPlugin(tmpArchivalDirectory, localDev, GH_PAGES_URL)],
}).catch((err: Error) => {
console.error('scraping error')
console.error(err)
Expand Down
18 changes: 8 additions & 10 deletions src/ghes-releases/scripts/rewrite-asset-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import fs from 'fs'
import path from 'path'

export class RewriteAssetPathsPlugin {
version: string
tempDirectory: string
localDev: boolean
replaceUrl: string

constructor(version: string, tempDirectory: string, localDev: boolean, replaceUrl: string) {
this.version = version
constructor(tempDirectory: string, localDev: boolean, replaceUrl: string) {
this.tempDirectory = tempDirectory
this.localDev = localDev
this.replaceUrl = replaceUrl
Expand All @@ -28,7 +26,7 @@ export class RewriteAssetPathsPlugin {

// Rewrite HTML asset paths. Example:
// ../assets/images/foo/bar.png ->
// https://githubdocs.azureedge.net/github-images/enterprise/2.17/assets/images/foo/bar.png
// https://github.github.com/docs-ghes-3.10/assets/images/foo/bar.png

if (resource.isHtml()) {
// Remove nextjs scripts and manifest.json link
Expand All @@ -42,8 +40,8 @@ export class RewriteAssetPathsPlugin {
// Rewrite asset paths
newBody = newBody.replace(
/(?<attribute>src|href)="(?:\.\.\/|\/)*(?<basepath>_next\/static|javascripts|stylesheets|assets\/fonts|assets\/cb-\d+\/images|node_modules)/g,
(attribute: string, basepath: string) => {
const replaced = `${this.replaceUrl}/${this.version}/${basepath}`
(match: string, attribute: string, basepath: string) => {
const replaced = `${this.replaceUrl}/${basepath}`
return `${attribute}="${replaced}`
},
)
Expand All @@ -52,15 +50,15 @@ export class RewriteAssetPathsPlugin {

// Rewrite CSS asset paths. Example
// url("../assets/fonts/alliance/alliance-no-1-regular.woff") ->
// url("https://githubdocs.azureedge.net/github-images/enterprise/2.20/assets/fonts/alliance/alliance-no-1-regular.woff")
// url("https://github.github.com/docs-ghes-3.10/assets/fonts/alliance/alliance-no-1-regular.woff")
// url(../../../assets/cb-303/images/octicons/search-24.svg) ->
// url(https://githubdocs.azureedge.net/github-images/enterprise/2.20/assets/cb-303/images/octicons/search-24.svg)
// url(https://github.github.com/docs-ghes-3.10/assets/cb-303/images/octicons/search-24.svg)
if (resource.isCss()) {
if (!this.localDev) {
newBody = newBody.replace(
/(?<attribute>url)(?<paren>\("|\()(?:\.\.\/)*(?<basepath>_next\/static|assets\/fonts|assets\/images|assets\/cb-\d+\/images)/g,
(attribute: string, paren: string, basepath: string) => {
const replaced = `${this.replaceUrl}/${this.version}/${basepath}`
(match: string, attribute: string, paren: string, basepath: string) => {
const replaced = `${this.replaceUrl}/${basepath}`
return `${attribute}${paren}${replaced}`
},
)
Expand Down

0 comments on commit 8ee5cd7

Please sign in to comment.